kafka get partition count for a topic

前端 未结 15 1079
萌比男神i
萌比男神i 2020-12-13 00:05

How can I get number of partitions for any kafka topic from the code. I have researched many links but none seem to work.

Mentioning a few:

http://grokbase.c

15条回答
  •  有刺的猬
    2020-12-13 00:40

    So the following approach works for kafka 0.10 and it does not use any producer or consumer APIs. It uses some classes from the scala API in kafka such as ZkConnection and ZkUtils.

        ZkConnection zkConnection = new ZkConnection(zkConnect);
        ZkUtils zkUtils = new ZkUtils(zkClient,zkConnection,false);
        System.out.println(JavaConversions.mapAsJavaMap(zkUtils.getPartitionAssignmentForTopics(
             JavaConversions.asScalaBuffer(topicList))).get("bidlogs_kafka10").size());
    

提交回复
热议问题