Skip to content

Instantly share code, notes, and snippets.

@turtlemonvh
Created April 20, 2017 05:08
Show Gist options
  • Save turtlemonvh/19f1426007970db8e348a0de157b1f0b to your computer and use it in GitHub Desktop.
Save turtlemonvh/19f1426007970db8e348a0de157b1f0b to your computer and use it in GitHub Desktop.
Get kafka topic configuration as json
#!/usr/bin/env python
# Returns kafka topic configuration settings as json
# Needs to be run from the kafka directory; something like `/opt/kafka/0.10.2.0/`
import commands
import json
o = commands.getoutput('bin/kafka-topics.sh --describe --zookeeper localhost:2181 | grep "^Topic"')
topics = {}
for l in o.split("\n"):
topic = {}
for p in l.split("\t"):
config_id, config_val = p.split(":")
if config_id == "Configs":
topic["config"] = {}
for subp in config_val.split(","):
subp_parts = subp.split("=")
if len(subp_parts) == 2:
subp_id, subp_val = subp_parts
topic["config"][subp_id] = subp_val
else:
topic[config_id] = config_val
topics[topic.get('Topic')] = topic
print(json.dumps(topics))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment