kafka get partition count for a topic

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

    Below shell cmd can print the number of partitions. You should be in kafka bin directory before executing the cmd:

    sh kafka-topics.sh --describe --zookeeper localhost:2181 --topic **TopicName** | awk '{print $2}' | uniq -c |awk 'NR==2{print "count of partitions=" $1}'
    

    Note that you have to change the topic name according to your need. You can further validate this using if condition as well:

    sh kafka-topics.sh --describe --zookeeper localhost:2181 --topic **TopicName** | awk '{print $2}' | uniq -c |awk 'NR==2{if ($1=="16") print "valid partitions"}'
    

    The above cmd command prints valid partitions if count is 16. You can change count depending on your requirement.

提交回复
热议问题