본문 바로가기

Programming/Scala

scala 로 kafka topic, partition 정보 가져오는 방법

아래 링크에 코드가 정리 되어 있다.


import kafka.utils.ZkUtils
object KafkaTopicTest {
def createZkUtils(): ZkUtils = {
val zookeepers = "localhost:2181"
val timeout = 10000
ZkUtils(zookeepers, timeout, timeout, isZkSecurityEnabled = false)
}
def getPartitionsForTopics(topics: Seq[String]): Map[String, Seq[Int]] = {
zkUtils.getPartitionsForTopics(topics).toMap
}
def getPartitionsForTopic(topic: String): Option[Seq[Int]] = {
zkUtils.getPartitionsForTopics(Seq(topic)).get(topic)
}
def main(args: Array[String]): Unit = {
val topicName = args(0)
val topicAndPartitionMap = getPartitionsForTopic(topicName)
println(topicAndPartitionMap)
}
}


https://gist.github.com/starblood/c35a4f70345cbbb5ad20d74d9fbe3f21