After a Kafka topic has been created by a producer or an administrator, how would you change the number of replicas of this topic?
1. Copy all topics to json file
#!/bin/bash
topics=`kafka-topics.sh --zookeeper localhost:2181 --list`
while read -r line; do lines+=("$line"); done <<<"$topics"
echo '{"version":1,
"topics":['
for t in $topics; do
echo -e ' { "topic":' \"$t\" '},'
done
echo ' ]
}'
bash alltopics.sh > alltopics.json
2. Run kafka-reassign-partitions.sh to generate rebalanced file
kafka-reassign-partitions.sh --zookeeper localhost:2181 --broker-list "0,1,2" --generate --topics-to-move-json-file alltopics.json > reassign.json
3. Cleanup reassign.json file it contains existing and proposed values
4. Run kafka-reassign-partitions.sh to rebalance topics
kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file reassign.json --execute