How to calculate median in Hive?
August 27, 2021How to fix Incompatible clusterIDs error during DataNode startup?
September 1, 2021Changing the number of replicas on an existing topic is a 3 step process.
- Get the current information about the topic
- Configure new replica assignment in a JSON file
- Execute kafka-reassign-partitions to reassign/change replicas of partitions
Get the current information about the topic
Here is the describe command to get the information about the topic named sales
kafka-topics --zookeeper <zookeeper-host>:2181 --topic sales --describe Topic:sales PartitionCount:3 ReplicationFactor:1 Configs:retention.ms=1000000000 Topic: sales Partition: 0 Leader: 0 Replicas: 0 Isr: 0 Topic: sales Partition: 1 Leader: 1 Replicas: 1 Isr: 1 Topic: sales Partition: 2 Leader: 2 Replicas: 2 Isr: 2
The output describes that sales has 3 partitions with a replication factor of 1. Partition 0 is stored in broker 0, partition 1 is stored in broker 1 and so on. ISR lists the brokers where the replicas are in sync. Since replication factor is one for this topic we only see the broker where the partition is stored.
Configure new replica assignment in a JSON file
In this example we are increasing the replication factor of the partitions of the sales topic to 3.
In the below JSON file we don’t specify the new replication factor, instead we specify partition 0 will be stored in broker 0, 1 and 2 there by increasing the replication factor to 3.
{ "version":1, "partitions":[ {"topic":"sales","partition":0,"replicas":[0,1,2]}, {"topic":"sales","partition":1,"replicas":[0,1,2]}, {"topic":"sales","partition":2,"replicas":[0,1,2]} ] }
Save the JSON file as increase-replication-factor.json
Execute kafka-reassign-partitions to reassign/change replicas of partitions
Use the kafka-reassign-partitions command passing in the increase-replication-factor.json as input. Note that the name of the topic and new partition setup is in the JSON file.
kafka-reassign-partitions --zookeeper <zookeeper-host>:2181 --increase-replication-factor.json --execute
Once done, the replication factor will be changed and you can run the below command to describe the sales topic to confirm the new settings.
kafka-topics --zookeeper <zookeeper-host>:2181 --topic sales --describe