-
-
Save kelein/090db895074fd6f74a9f033d00fc81fd to your computer and use it in GitHub Desktop.
Python script to get Kafka Brokers from Zookeeper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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