How to get topic list from kafka server in Java

后端 未结 6 503

I am using kafka 0.8 version and very much new to it.

I want to know the list of topics created in kafka server along with it\'s metadata.

6条回答
  •  有刺的猬
    2020-12-29 07:25

    If you want to pull broker or other-kafka information from Zookeeper then kafka.utils.ZkUtils provides a nice interface. Here is the code I have to list all zookeeper brokers (there are a ton of other methods there):

    List listBrokers() {
    
            final ZkConnection zkConnection = new ZkConnection(connectionString);
            final int sessionTimeoutMs = 10 * 1000;
            final int connectionTimeoutMs = 20 * 1000;
            final ZkClient zkClient = new ZkClient(connectionString,
                                                   sessionTimeoutMs,
                                                   connectionTimeoutMs,
                                                   ZKStringSerializer$.MODULE$);
    
            final ZkUtils zkUtils = new ZkUtils(zkClient, zkConnection, false);
    
            scala.collection.JavaConversions.seqAsJavaList(zkUtils.getAllBrokersInCluster());
    }
    

提交回复
热议问题