yjyyjy

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Return: Map[TopicPartition, Long] 

Code:

val props = new Properties()
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaPara("bootstrap.servers").toString)
props.put(ConsumerConfig.GROUP_ID_CONFIG, kafkaPara("group.id").toString)
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false")
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer")
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer")

val kc: KafkaConsumer[String, String] = new KafkaConsumer[String, String](props)

kc.partitionsFor(new String(topic)).asScala.map{partitionInfo =>

val topicPartition = new TopicPartition(topic, partitionInfo.partition())
kc.assign(Seq(topicPartition).asJava)
kc.seekToBeginning(Seq(topicPartition).asJava)
topicPartition ->  kc.position(topicPartition)
}.toMap

Key point: Scala code call Java lib

posted on 2019-04-09 17:32  闭关49天  阅读(188)  评论(0编辑  收藏  举报