After a Kafka topic has been created by a producer or an administrator, how would you change the number of replicas of this topic?
To increase the number of replicas for a given topic you have to:
1. Specify the extra partitions to the existing topic with below command(let us say increase from 2 to 3)
bin/kafktopics.sh --zookeeper localhost:2181 --alter --topic topic-to-increase --partitions 3
2. Specify the extra replicas in a custom reassignment json file
For example, you could create increase-replication-factor.json and put this content in it:
{"version":1,
"partitions":[
{"topic":"topic-to-increase","partition":0,"replicas":[0,1,2]},
{"topic":"topic-to-increase","partition":1,"replicas":[0,1,2]},
{"topic":"topic-to-increase","partition":2,"replicas":[0,1,2]}
]}
3. Use the file with the --execute option of the kafka-reassign-partitions tool
bin/kafka-reassign-partitions --zookeeper localhost:2181 --reassignment-json-file increase-replication-factor.json --execute
4. Verify the replication factor with the kafka-topics tool
bin/kafka-topics --zookeeper localhost:2181 --topic topic-to-increase --describe