kafka get partition count for a topic

前端 未结 15 1082
萌比男神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());
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-13 00:42

    You can get kafka partition list from zookeeper like this. It is real kafka server side partition number.

    [zk: zk.kafka:2181(CONNECTED) 43] ls /users/test_account/test_kafka_name/brokers/topics/test_kafka_topic_name/partitions
    [35, 36, 159, 33, 34, 158, 157, 39, 156, 37, 155, 38, 154, 152, 153, 150, 151, 43, 42, 41, 40, 202, 203, 204, 205, 200, 201, 22, 23, 169, 24, 25, 26, 166, 206, 165, 27, 207, 168, 208, 28, 29, 167, 209, 161, 3, 2, 162, 1, 163, 0, 164, 7, 30, 6, 32, 5, 160, 31, 4, 9, 8, 211, 212, 210, 215, 216, 213, 19, 214, 17, 179, 219, 18, 178, 177, 15, 217, 218, 16, 176, 13, 14, 11, 12, 21, 170, 20, 171, 174, 175, 172, 173, 220, 221, 222, 223, 224, 225, 226, 227, 188, 228, 187, 229, 189, 180, 10, 181, 182, 183, 184, 185, 186, 116, 117, 79, 114, 78, 77, 115, 112, 113, 110, 111, 118, 119, 82, 83, 80, 81, 86, 87, 84, 85, 67, 125, 66, 126, 69, 127, 128, 68, 121, 122, 123, 124, 129, 70, 71, 120, 72, 73, 74, 75, 76, 134, 135, 132, 133, 59, 138, 58, 57, 139, 136, 56, 137, 55, 64, 65, 62, 63, 60, 131, 130, 61, 49, 143, 48, 144, 145, 146, 45, 147, 44, 148, 47, 149, 46, 51, 52, 53, 54, 140, 142, 141, 50, 109, 108, 107, 106, 105, 104, 103, 99, 102, 101, 100, 98, 97, 96, 95, 94, 93, 92, 91, 90, 88, 89, 195, 194, 197, 196, 191, 190, 193, 192, 198, 199, 230, 239, 232, 231, 234, 233, 236, 235, 238, 237]
    

    And you can use partition count at consumer code.

      def getNumPartitions(topic: String): Int = {
        val zk = CuratorFrameworkFactory.newClient(zkHostList, new RetryNTimes(5, 1000))
    
        zk.start()
        var numPartitions: Int = 0
        val topicPartitionsPath = zkPath + "/brokers/topics/" + topic + "/partitions"
    
        if (zk.checkExists().forPath(topicPartitionsPath) != null) {
            try {
                val brokerIdList = zk.getChildren().forPath(topicPartitionsPath).asScala
                numPartitions = brokerIdList.length.toInt
            } catch {
                case e: Exception => {
                    e.printStackTrace()
                }  
            }  
        }  
        zk.close()
    
        numPartitions
      }
    
    0 讨论(0)
  • 2020-12-13 00:43

    The number of partitions can be retrieved from zookeeper-shell

    Syntax: ls /brokers/topics/<topic_name>/partitions
    

    Below is the example:

    root@zookeeper-01:/opt/kafka_2.11-2.0.0# bin/zookeeper-shell.sh zookeeper-01:2181
    Connecting to zookeeper-01:2181
    Welcome to ZooKeeper!
    JLine support is disabled
    
    WATCHER::
    
    WatchedEvent state:SyncConnected type:None path:null
    ls /brokers/topics/test/partitions
    [0, 1, 2, 3, 4]
    
    0 讨论(0)
  • 2020-12-13 00:48

    Go to your kafka/bin directory.

    Then run this:

    ./kafka-topics.sh --describe --zookeeper localhost:2181 --topic topic_name

    You should see what you need under PartitionCount.

    Topic:topic_name        PartitionCount:5        ReplicationFactor:1     Configs:
            Topic: topic_name       Partition: 0    Leader: 1001    Replicas: 1001  Isr: 1001
            Topic: topic_name       Partition: 1    Leader: 1001    Replicas: 1001  Isr: 1001
            Topic: topic_name       Partition: 2    Leader: 1001    Replicas: 1001  Isr: 1001
            Topic: topic_name       Partition: 3    Leader: 1001    Replicas: 1001  Isr: 1001
            Topic: topic_name       Partition: 4    Leader: 1001    Replicas: 1001  Isr: 1001
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题