How to change the number of replicas of a Kafka topic?

后端 未结 8 798
离开以前
离开以前 2020-11-30 21:31

After a Kafka topic has been created by a producer or an administrator, how would you change the number of replicas of this topic?

8条回答
  •  爱一瞬间的悲伤
    2020-11-30 21:57

    If you have a lot of partitions, using kafka-reassign-partitions to generate the json file required by Łukasz Dumiszewski's answer (and the official documentation) can be a timesaver. Here is an example of replicating a 64 partition topic from 1 to 2 servers without having to specify all the partitions:

    expand_topic=TestTopic
    current_server=111
    new_servers=111,222
    echo '{"topics": [{"topic":"'${expand_topic}'"}], "version":1}' > /tmp/topics-to-expand.json
    /bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --topics-to-move-json-file /tmp/topics-to-expand.json --broker-list "${current_server}" --generate | tail -1 | sed s/\\[${current_server}\\]/\[${new_servers}\]/g | tee /tmp/topic-expand-plan.json
    /bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file /tmp/topic-expand-plan.json --execute
    /bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic ${expand_topic}
    

    Outputs:

    Topic:TestTopic PartitionCount:64   ReplicationFactor:2 Configs:retention.ms=6048000
        Topic: TestTopic    Partition: 0    Leader: 111 Replicas: 111,222   Isr: 111,222
        Topic: TestTopic    Partition: 1    Leader: 111 Replicas: 111,222   Isr: 111,222
        ....
    

提交回复
热议问题