0

Do we have a support for partitionsFor method in the producer in kafka version 0.8.0? I want to use this method to get the number of partitions given a kafka topic.

If this method is not available in kafka 0.8.0, what is the easiest way to get the number of partitions in the producer in this specific version of kafka?

ppatierno
  • 9,431
  • 1
  • 30
  • 45
Gayatri Mahesh
  • 327
  • 1
  • 3
  • 12

2 Answers2

0

Why don't you try following approach https://stackoverflow.com/a/35458605/5922904. Also ZkUtils have the method getPartitionsForTopics which can also be used. Although I have not tried and tested it myself

Swapnil
  • 121
  • 1
  • 4
0

You can use listTopics() method also

    ArrayList<Topics> topicList = new ArrayList<Topics>();
    Properties props = new Properties();
    Map<String, List<PartitionInfo>> topics;
    Topics topic;
    InputStream input = 
 getClass().getClassLoader().getResourceAsStream("kafkaCluster.properties");
    try {
        props.load(input);
        props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, 
 props.getProperty("BOOTSTRAP_SERVERS_CONFIG"));
        props.put("key.deserializer", 
 "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", 
 "org.apache.kafka.common.serialization.StringDeserializer");
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, 
   String>(props);
        topics = consumer.listTopics();
        // System.out.println(topics.get(topics));
        for (Map.Entry<String, List<PartitionInfo>> entry : 
    topics.entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = " + 
     entry.getValue());
            topic = new Topics();
            topic.setTopic_name(entry.getKey());
            topic.setPartitions(Integer.toString(entry.getValue().size()));
            topicList.add(topic);
        }
     } catch (IOException e) {
        e.printStackTrace();
     }
Megha
  • 188
  • 2
  • 10