Skip to content

Instantly share code, notes, and snippets.

@JensRantil
Last active October 23, 2020 14:01
Show Gist options
  • Save JensRantil/810caee0e0ad62ab69ee74ec520b8e56 to your computer and use it in GitHub Desktop.
Save JensRantil/810caee0e0ad62ab69ee74ec520b8e56 to your computer and use it in GitHub Desktop.
Kafka script to generate Bash commands that will recreate Kafka topics. Possibly useful for backups of topics.
./bin/kafka-topics.sh --if-not-exists --zookeeper $(grep -E zookeeper.connect= ./config/server.properties | cut -d= -f2) --create --partitions 12 --replication-factor 3 --topic my-topic-1 --config retention.ms=3600000 --config min.insync.replicas=2
./bin/kafka-topics.sh --if-not-exists --zookeeper $(grep -E zookeeper.connect= ./config/server.properties | cut -d= -f2) --create --partitions 1 --replication-factor 3 --topic my-topic-2 --config min.insync.replicas=2
#!/bin/bash
./bin/kafka-topics.sh --zookeeper $(grep -E "zookeeper.connect=" ./config/server.properties | cut -d= -f2) --describe \
| grep PartitionCount \
| awk -F '\t' 'BEGIN {OFS=":";} {print $1, $2, $3, $4}' \
| sed 's/,/:/g' \
| awk -F: '{printf "./bin/kafka-topics.sh --if-not-exists --zookeeper $(grep -E zookeeper.connect= ./config/server.properties | cut -d= -f2) --create --partitions %d --replication-factor %d --topic %s", $4, $6, $2;for(i=8; i<=NF; i++) {printf " --config %s", $i;};printf "\n";}'
@Olivia5k
Copy link

Needs more awk!

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