'get count of partitions in a kafka topic with scala 2.12

With scala 2.11 and spark-streaming-kafka-0-8_2.11 I could do

import org.apache.spark.streaming.kafka.KafkaCluster

val params = Map[String, Object](
  "bootstrap.servers" -> "localhost:9092",
  "key.deserializer" -> classOf[StringDeserializer],
  "value.deserializer" -> classOf[StringDeserializer],
  "group.id" -> "my_group_id",
  "auto.offset.reset" -> "latest",
  "enable.auto.commit" -> (false: java.lang.Boolean)
)
val topicsSet = Array("topicA", "topicB")
val kafkaCluster = new KafkaCluster(params)
val kafkaPartitions = kafkaCluster.getPartitions(topicsSet).right.get
val kafkaPartitionsCount = kafkaPartitons.size

Now with scala 2.12 and spark-streaming-kafka-0-10_2.12 there is no KafkaCluster. How do I migrate this piece of code to find number of topic partitions?



Solution 1:[1]

You don't need Spark for this. You can use KafkaConsumer.listTopics() to get a Map of topic names to a list of partitions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 OneCricketeer