kafka get partition count for a topic

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

提交回复
热议问题