kafka get partition count for a topic

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

    In the 0.82 Producer API and 0.9 Consumer api you can use something like

    Properties configProperties = new Properties();
    configProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");
    configProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.ByteArraySerializer");
    configProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
    
    org.apache.kafka.clients.producer.Producer producer = new KafkaProducer(configProperties);
    producer.partitionsFor("test")
    

提交回复
热议问题