Kafka python consumer reading all the messages when started
Asked Answered
G

1

12

I am using the below code to read messages from a topic. I am facing two issues. Whenever i start consumer, it is reading all the messages in the queue? How do read only the unread messages?

from kafka import KafkaConsumer


consumer = KafkaConsumer('my-topic',
                         group_id='my-group',
                         bootstrap_servers=['localhost:9092'])
for message in consumer:
    consumer.commit() 
    # message value and key are raw bytes -- decode if necessary!
    # e.g., for unicode: `message.value.decode('utf-8')`
    print ("%s:%d:%d: key=%s value=%s" % (message.topic, message.partition,
                                          message.offset, message.key,
                                          message.value))
Gallican answered 9/1, 2016 at 7:8 Comment(2)
I think you have to consumer.commit() after reading out.Gen
thanks @KenjiNoguchi, i tried with consumer.commit() and still not working. any hintsGallican
G
11

As @Kenji said you have to commit the offsets with consumer.commit(). If you don't want to commit manually you can enable autocommit by passing enable_auto_commit=True to your KafkaConsumer. You may also want to tune auto_commit_interval_ms which is the interval in milliseconds between each automatic commit. See here: http://kafka-python.readthedocs.org/en/master/apidoc/KafkaConsumer.html.

Glowworm answered 9/1, 2016 at 12:31 Comment(2)
thanks @Glowworm i tried with consumer.commit() and still not working. any hintsGallican
@user3570620, maybe this is helpful: #36580315Malignancy

© 2022 - 2025 — McMap. All rights reserved.