Skip to content
Advertisement

kafka configuration + zookeeper cli + get the right info for kafka host

As all know when is need to print the kafka broker id’s we can use the following cli

zookeeper-shell.sh zoo_server1:2181 <<< "ls /brokers/ids"

this cli print the following

WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[1018, 1017, 1016]

Its means that we have kafka with id’s

1018 
1017
1016

But our kafka names are

Kafka_confluent01
Kafka_confluent02
Kafka_confluent03

So how to know which kafka broker id ( 1018 , 1017 , 1016 ) is belong to the real host ( Kafka_confluent01 / Kafka_confluent02 / Kafka_confluent03 )

Advertisement

Answer

You can get the list of brokers dynamically, using the following code.

public class KafkaBrokerInfoFetcher {

    public static void main(String[] args) throws Exception {
        ZooKeeper zk = new ZooKeeper("localhost:2181", 10000, null);
        List<String> ids = zk.getChildren("/brokers/ids", false);
        for (String id : ids) {
            String brokerInfo = new String(zk.getData("/brokers/ids/" + id, false, null));
            System.out.println(id + ": " + brokerInfo);
        }
    }
}

After running the code, you will get the broker id and corresponding host.

1: {"jmx_port":-1,"timestamp":"1428512949385","host":"192.168.0.11","version":1,"port":9093}
2: {"jmx_port":-1,"timestamp":"1428512955512","host":"192.168.0.11","version":1,"port":9094}
3: {"jmx_port":-1,"timestamp":"1428512961043","host":"192.168.0.11","version":1,"port":9095}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement