Kafka Consumer: How to start consuming from the last message in Python

后端 未结 5 1300
闹比i
闹比i 2020-12-28 22:08

I am using Kafka 0.8.1 and Kafka python-0.9.0. In my setup, I have 2 kafka brokers setup. When I run my kafka consumer, I can see it retrieving messages from the queue and k

5条回答
  •  难免孤独
    2020-12-28 23:13

    Take care with the kafka-python library. It has a few minor issues.

    If speed is not really a problem for your consumer you can set the auto-commit in every message. It should works.

    SimpleConsumer provides a seek method (https://github.com/mumrah/kafka-python/blob/master/kafka/consumer/simple.py#L174-L185) that allows you to start consuming messages in whatever point you want.

    The most usual calls are:

    • consumer.seek(0, 0) to start reading from the beginning of the queue.
    • consumer.seek(0, 1) to start reading from current offset.
    • consumer.seek(0, 2) to skip all the pending messages and start reading only new messages.

    The first argument is an offset to those positions. In that way, if you call consumer.seek(5, 0) you will skip the first 5 messages from the queue.

    Also, don't forget, the offset is stored for consumer groups. Be sure you are using the same one all the time.

提交回复
热议问题