kafka get partition count for a topic

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

    To get the list of partitions the ideal/actual way is to use the AdminClients API

        Properties properties=new Properties();
        properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");
        AdminClient adminClient=KafkaAdminClient.create(properties);
        Map<String, TopicDescription> jension = adminClient.describeTopics(Collections.singletonList("jenison")).all().get();
        System.out.println(jension.get("jenison").partitions().size());
    

    This can be run as a standalone java method with no producer/consumer dependencies.

    0 讨论(0)
  • 2020-12-13 00:58
    cluster.availablePartitionsForTopic(topicName).size()
    
    0 讨论(0)
  • 2020-12-13 00:59

    @Sunil-patil answer stopped short of answering the count piece of it. You have to get the size of the List

    producer.partitionsFor("test").size()

    @vish4071 no point butting Sunil, you did not mention that you are using ConsumerConnector in the question.

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