shell 脚本:
1)列出只有一个副本的topic,保存到一个文件中:
[root@hdp05 src]# cat fush.sh #!/bin/bash # topics=`/usr/hdp/2.6.4.0-91/kafka/bin/kafka-topics.sh --zookeeper 192.168.50.101:2181 -list` for i in $topics;do replicsNum=`/usr/hdp/2.6.4.0-91/kafka/bin/kafka-topics.sh --zookeeper 192.168.50.101:2181 -describe --topic $i|grep ReplicationFactor|awk '{print $3}'|awk -F: '{print $2}'` if [ $replicsNum == 1 ];then echo $i >> /usr/local/src/topic.txt fi done
2)执行扩容副本操作
[root@hdp05 src]# vim exce.sh #!/bin/bash # #topics=`cat /usr/local/src/topic.txt` topics=`cat /usr/local/src/topic01.txt` for i in $topics;do leader=`/usr/hdp/2.6.4.0-91/kafka/bin/kafka-topics.sh --zookeeper 192.168.50.101:2181 -describe --topic $i|grep Leader|awk '{prin t $6}'` leader0=`echo $leader |awk '{print $1}'` leader1=`echo $leader |awk '{print $2}'` echo $leader0 > /usr/local/src/leadering.txt echo $leader1 >> /usr/local/src/leadering.txt leadercha=`grep -vxFf /usr/local/src/leadering.txt /usr/local/src/leaderNum.txt` echo $leadercha echo $i echo '{"version":1,"partitions":[{"topic":"'$i'","partition":0,"replicas":['$leader0','$leadercha']},{"topic":"'$i'","partition" :1,"replicas":['$leader1','$leadercha']}]}' > /usr/local/src/topic-reassignment.json /usr/hdp/2.6.4.0-91/kafka/bin/kafka-reassign-partitions.sh --zookeeper 192.168.50.101:2181 --reassignment-json-file /usr/local/sr c/topic-reassignment.json --execute sleep 5 done