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

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

提交回复
热议问题