Kafka python consumer reading all the messages when started

喜欢而已 提交于 2019-12-07 01:34:10

问题


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))

回答1:


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.



来源:https://stackoverflow.com/questions/34690749/kafka-python-consumer-reading-all-the-messages-when-started

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!