-1

Using the Bash shell, is there a way to delete and then recreate all the Kafka topics shown from running the command

kafka-topics --zookeeper localhost:2181
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Athena Wisdom
  • 6,101
  • 9
  • 36
  • 60

1 Answers1

0

There isn't, since that command alone doesn't include topic settings or partition count.

You'd need to take the output of that script, loop it over all results, pass to kafka-topics --describe, then parse that output, and finally build a command to kafka-topics --delete and --create.
Keep in mind that the --zookeeper option is deprecated.

Another option would be to use zookeeper-shell to backup and iterate the /topics Znode information, but I think this will be missing topic configurations.


If the goal is to easily restore topic information, one suggestion would be to centrally manage how topics actually get created, such as via Terraform or Kubernetes Kafka Operators along with proper GitOps/DevOps processes. This would also be a good way to discover and create topic access-controls or schemas.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Here's a github gist that loops over a text file for creating topics https://gist.github.com/polster/a0f7c012a481ebca9a4e9d4e56e6eb39 – OneCricketeer Jan 05 '22 at 19:26