我有一个需求,我需要从Kafka主题中找到最近插入的消息。我该如何实现?
我试图先获取偏移量,然后尝试从该偏移量获取消息? 是有效的解决方案吗?
val config = KafkaConfig()
val props = new Properties()
// ConsumerConfig
props.put("bootstrap.servers", config.bootstrapServers)
props.put("group.id", "stream-latest-consumer")
props.put(
"key.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer"
)
props.put(
"value.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer"
)
val kafkaConsumer = new KafkaConsumer[String, String](props)
val p = new TopicPartition(config.topic, 0)
val cl: util.Collection[TopicPartition] = List(p).asJava
val offsetsMap: java.util.Map[TopicPartition, java.lang.Long] =
kafkaConsumer.endOffsets(cl)
val offsetCount = offsetsMap.get(p)
答案 0 :(得分:1)
您也可以使用
rr = {
id: 215043,
"Official Name": "Google, Inc.",
}
rr.keys.each {|key| p [key, key.class] }
# => [:id, Symbol]
# [:"Official Name", Symbol]
以获得给定分区的最新偏移量。