Skip to content

Instantly share code, notes, and snippets.

@ralph-tice
Last active April 1, 2020 10:03
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ralph-tice/bc8f66c4fe1d4238ce54c54272fc4ba2 to your computer and use it in GitHub Desktop.
Save ralph-tice/bc8f66c4fe1d4238ce54c54272fc4ba2 to your computer and use it in GitHub Desktop.
Resizing a Kafka cluster via bash
#!/bin/bash -xe
# requires jq and 3 brokers to start
which jq || echo 'no jq found, bye!' && exit 1
ZK_HOST=zkhost1:2181/kafka
# record all topics
/usr/local/kafka/bin/kafka-topics.sh --zookeeper $ZK_HOST --list > all_topics
# generate json for listing which topics to reassign
grep -v deletion all_topics | tr '\n' ' ' | head --bytes -1 | jq -R 'split(" ") | reduce .[] as $topic ([]; . + [{"topic": $topic }]) | {"topics": . , "version": 1}' > all_topics.json
# generate the json for reassigning topics, the tool outputs current state then proposed state so take only the last line. For a real cluster resize, you'd include all the brokers, but in this case I'm retiring brokers 0, 1 and 2.
/usr/local/kafka/bin/kafka-reassign-partitions.sh --zookeeper $ZK_HOST --topics-to-move-json-file all_topics.json --broker-list "4,5,6" --generate | tail -n 1 > reassignment.json
# actually do the reassignment
/usr/local/kafka/bin/kafka-reassign-partitions.sh --zookeeper $ZK_HOST --topics-to-move-json-file all_topics.json --reassignment-json-file reassignment.json --execute
# watch to see when reassignment is completed
watch -n 5 /usr/local/kafka/bin/kafka-reassign-partitions.sh --zookeeper $ZK_HOST --topics-to-move-json-file all_topics.json --reassignment-json-file reassignment.json --verify
@maverickagm
Copy link

Thanks.
Tip: Use the -s option with jq for large lists. It prevents in from splitting in between a line when a buffer fills. Ex

    ...
    {
      "topic": "this.is.actually."
    }
  ],
  "version": 1
}
{
  "topics": [
    {
      "topic": "one.topic"
    },
    ...

@peetasan
Copy link

in line 17 you don't need this part --topics-to-move-json-file all_topics.json

@ranjeet-floyd
Copy link

in line 20 , we don't need this part --topics-to-move-json-file all_topics.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment