Skip to content

Instantly share code, notes, and snippets.

@tromika
Created January 6, 2017 14:42
Show Gist options
  • Save tromika/0336415a37b42759fbc328456b0ea42c to your computer and use it in GitHub Desktop.
Save tromika/0336415a37b42759fbc328456b0ea42c to your computer and use it in GitHub Desktop.
Python script to get Kafka Brokers from Zookeeper
from kazoo.client import KazooClient
import json
####
# A quick function to get Kafka brokers from Zookeeper
###
# Probably you need only the first one because the broker will advertise the other brokers
# This is need only for producers due there you can only use bootstrap servers
# Arguments
# - zk: Zookeeper conn string
# - path: The path what used for Kafka in Zookeeper
#
def getKafkaBroker(zk, path):
zk = KazooClient(hosts=zk,read_only=True)
zk.start()
for node in zk.get_children(path+'/brokers/ids'):
data, stats = zk.get(path+'/brokers/ids/'+node)
props = json.loads(data)
yield props['host']+':'+str(props['port'])
zk.stop()
print next(getKafkaBroker('localhost:2171', 'kafka'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment