Kafka - How to commit offset after every message using High-Level consumer?

我是研究僧i 提交于 2019-11-27 10:20:58

问题


I'm using Kafka's high-level consumer. Because I'm using Kafka as a 'queue of transactions' for my application, I need to make absolutely sure I don't miss or re-read any messages. I have 2 questions regarding this:

  1. How do I commit the offset to zookeeper? I will turn off auto-commit and commit offset after every message successfully consumed. I can't seem to find actual code examples of how to do this using high-level consumer. Can anyone help me with this?

  2. On the other hand, I've heard committing to zookeeper might be slow, so another way may be to locally keep track of the offsets? Is this alternative method advisable? If yes, how would you approach it?


回答1:


There are two relevant settings from http://kafka.apache.org/documentation.html#consumerconfigs.

auto.commit.enable

and

auto.commit.interval.ms

If you want to set it such that the consumer commits the offset after each message, that will be difficult since the only setting is after a timer interval, not after each message. You will have to do some rate prediction of the incoming messages and accordingly set the time.

In general, it is not recommended to keep this interval too small because it vastly increases the read/write rates in zookeeper and zookeeper gets slowed down because it's strongly consistent across its quorum.




回答2:


You could first disable auto commit: auto.commit.enable=false

Then commit after fetching the message: consumer.commitOffsets(true)



来源:https://stackoverflow.com/questions/25293622/kafka-how-to-commit-offset-after-every-message-using-high-level-consumer

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