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
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.
cluster.availablePartitionsForTopic(topicName).size()
@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.