kafka get partition count for a topic

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

    //create the kafka producer
    def getKafkaProducer: KafkaProducer[String, String] = {
    val kafkaProps: Properties = new Properties()
    kafkaProps.put("bootstrap.servers", "localhost:9092")
    kafkaProps.put("key.serializer",
    "org.apache.kafka.common.serialization.StringSerializer")
    kafkaProps.put("value.serializer", 
    "org.apache.kafka.common.serialization.StringSerializer")
    
    new KafkaProducer[String, String](kafkaProps)
    }
    val kafkaProducer = getKafkaProducer
    val noOfPartition = kafkaProducer.partitionsFor("TopicName") 
    println(noOfPartition) //it will print the number of partiton for the given 
    //topic
    

提交回复
热议问题