Skip to content

Instantly share code, notes, and snippets.

@beastawakens
Last active March 28, 2017 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beastawakens/b370bbc45094f6a772895918e0111210 to your computer and use it in GitHub Desktop.
Save beastawakens/b370bbc45094f6a772895918e0111210 to your computer and use it in GitHub Desktop.
Simple script to update replicating brokers per topic for a Kafka cluster
#!/bin/bash
# usage like ./updateKafkaReplication.sh test-replica-update 0,1,2
TOPIC_NAME=$1
REPLICAS=$2
echo "****************************************"
echo "describe $TOPIC_NAME"
bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic $TOPIC_NAME
PARTITION_COUNT=$(eval "bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic $TOPIC_NAME | awk -F '[:\t]' 'NR==1 {print \$4}'")
echo "Partitions: ${PARTITION_COUNT}"
JSON_FILE=~/configChanges/${TOPIC_NAME}-update.json
echo $JSON_FILE
[ -e $JSON_FILE ] && rm $JSON_FILE
touch $JSON_FILE
((PARTITION_COUNT--))
printf "{\"version\":1, \"partitions\":[" >> $JSON_FILE
for i in $(eval echo "{0..$PARTITION_COUNT}");
do
if [ $i == $PARTITION_COUNT ]; then
printf "{\"topic\":\"%s\",\"partition\":%s,\"replicas\":[%s]}" "$TOPIC_NAME" "$i" "$REPLICAS" >> $JSON_FILE
else
printf "{\"topic\":\"%s\",\"partition\":%s,\"replicas\":[%s]}," "$TOPIC_NAME" "$i" "$REPLICAS" >> $JSON_FILE
fi
done
printf "]}" >> $JSON_FILE
echo "****************************************"
echo "updating $TOPIC_NAME"
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file $JSON_FILE --execute
echo "****************************************"
echo "describe $TOPIC_NAME"
bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic $TOPIC_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment