Last active
March 28, 2017 11:41
-
-
Save beastawakens/b370bbc45094f6a772895918e0111210 to your computer and use it in GitHub Desktop.
Simple script to update replicating brokers per topic for a Kafka cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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