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

后端 未结 8 795
离开以前
离开以前 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:55

    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
    
    0 讨论(0)
  • 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
        ....
    
    0 讨论(0)
提交回复
热议问题