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

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

    This script may help you, if you want change replication factor for all topics:

    #!/bin/bash
    
    topics=`kafka-topics --list --zookeeper zookeeper:2181`
    
    while read -r line; do lines+=("$line"); done <<<"$topics"
    echo '{"version":1,
      "partitions":[' > tmp.json
    for t in $topics; do 
        if [ "${t}" == "${lines[-1]}" ]; then
            echo "    {\"topic\":\"${t}\",\"partition\":0,\"replicas\":[0,1,2]}" >> tmp.json
        else
            echo "    {\"topic\":\"${t}\",\"partition\":0,\"replicas\":[0,1,2]}," >> tmp.json
        fi
    done
    
    echo '  ]
    }' >> tmp.json
    
    kafka-reassign-partitions --zookeeper zookeeper:2181 --reassignment-json-file tmp.json --execute
    

提交回复
热议问题