kafka get partition count for a topic

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

    Use PartitionList from KafkaConsumer

         //create consumer then loop through topics
        KafkaConsumer consumer = new KafkaConsumer(props);
        List partitions = consumer.partitionsFor(topic);
    
        ArrayList partitionList = new ArrayList<>();
        System.out.println(partitions.get(0).partition());
    
        for(int i = 0; i < partitions.size(); i++){
            partitionList.add(partitions.get(i).partition());
        }
    
        Collections.sort(partitionList);
    

    Should work like a charm. Let me know if there's a simpler way to access Partition List from Topic.

提交回复
热议问题