Python KafkaConsumer 从时间戳开始消费消息 [英] Python KafkaConsumer start consuming messages from a timestamp
问题描述
我打算跳过主题的开头,只读取从某个时间戳到结尾的消息.关于如何实现这一目标的任何提示?
I'm planning to skip the start of the topic and only read messages from a certain timestamp to the end. Any hints on how to achieve this?
推荐答案
我猜你在使用 kafka-python
(https://github.com/dpkp/kafka-python) 正如你提到的KafkaConsumer".
I'm guessing you are using kafka-python
(https://github.com/dpkp/kafka-python) as you mentioned "KafkaConsumer".
您可以使用 offsets_for_times()
方法来检索与时间戳匹配的偏移量.https://kafka-python.readthedocs.io/en/master/apidoc/KafkaConsumer.html#kafka.KafkaConsumer.offsets_for_times
You can use the offsets_for_times()
method to retrieve the offset that matches a timestamp. https://kafka-python.readthedocs.io/en/master/apidoc/KafkaConsumer.html#kafka.KafkaConsumer.offsets_for_times
接下来只是使用 seek()
寻找该偏移量.https://kafka-python.readthedocs.io/en/master/apidoc/KafkaConsumer.html#kafka.KafkaConsumer.seek
Following that just seek to that offset using seek()
. https://kafka-python.readthedocs.io/en/master/apidoc/KafkaConsumer.html#kafka.KafkaConsumer.seek
希望这会有所帮助!
这篇关于Python KafkaConsumer 从时间戳开始消费消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!