kafka get partition count for a topic

前端 未结 15 1136
萌比男神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

    I have had the same issue, where I needed to get the partitions for a topic.

    With the help of the answer here I was able to get the information from Zookeeper.

    Here is my code in Scala (but could be easily translated into Java)

    import org.apache.zookeeper.ZooKeeper
    
    def extractPartitionNumberForTopic(topicName: String, zookeeperQurom: String): Int = {
      val zk = new ZooKeeper(zookeeperQurom, 10000, null);
      val zkNodeName = s"/brokers/topics/$topicName/partitions"
      val numPartitions = zk.getChildren(zkNodeName, false).size
      zk.close()
      numPartitions
    }
    

    Using this approach allowed me to access the information about Kafka topics as well as other information about Kafka brokers ...

    From Zookeeper you could check for the number of partitions for a topic by browsing to /brokers/topics/MY_TOPIC_NAME/partitions

    Using zookeeper-client.sh to connect to your zookeeper:

    [zk: ZkServer:2181(CONNECTED) 5] ls /brokers/topics/MY_TOPIC_NAME/partitions
    [0, 1, 2]
    

    That shows us that there are 3 partitions for the topic MY_TOPIC_NAME

提交回复
热议问题