Kafka 读出指定 partition 指定位置数据

Q:Using kafka-python, is it possible to read a specific range of offsets for given partition(s)?
I looked through the documentation of the available consumers at http://kafka-python.readthedocs.org/en/1.0.0/apidoc/kafka.consumer.html but could not find anything that would let me do that.

A: How about:

consumer = KafkaConsumer()
partition = TopicPartition('foo', 0)
start = 1234
end = 2345
consumer.assign([partition])
consumer.seek(partition, start)
for msg in consumer:
    if msg.offset > end:
        break
    else:
        print msg

Reference:

https://github.com/dpkp/kafka-python/issues/648


原文地址:https://www.cnblogs.com/piperck/p/12698302.html