Apache kafka - consumer delay option

2019-08-27 13:20发布

问题:

I want to start a consumer in kafka for a particular topic in a small delay. In detail , I want the consumer to start consuming the messages from the topic after a particular time delay from the time of producing the messages . Can anyone say is there any property or option in kafka to enable it . Thanks in advance.

回答1:

We did the same stuff for spark-streaming. I hope, the approach can suits you also.

The idea is very simple - use Thread.sleep. When you receive new message from kafka, you can calculate how long do you need to sleep before process it.

pseudocode for idea:

message = getNextMessageFromKafka()
sleepMs = Math.max(0, currentTime - message.timestamp)
Thread.sleep(speepMs)
do processing