Python KafkaConsumer 从时间戳开始消费消息

Sil*_*hus 2 python-3.x apache-kafka kafka-consumer-api kafka-python

我打算跳过主题的开头,只读取从某个时间戳到结尾的消息。关于如何实现这一目标的任何提示?

Mic*_*son 7

我猜你正在使用kafka-pythonhttps://github.com/dpkp/kafka-python),正如你提到的“KafkaConsumer”。

您可以使用该offsets_for_times()方法来检索与时间戳匹配的偏移量。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

希望这可以帮助!