5
votes

Is it possible to consume messages from Kafka based on a time period in which the messages were ingested?

Example: I want all messages ingested to a topic between 0900-1000 today (and now it's 1200).

If there is only a way to specify a start time, that's fine - my consumer can stop processing messages once it reaches the end time.

I can see methods for requesting messages from a given offset, and for getting the first available offset, and for the earliest available offset, but not all messages after a given time.

1

1 Answers

9
votes

You could use the offsetsForTimes method which returns you offset whose timestamp is greater or equal to the give timestamp. More information on the official doc here :

https://kafka.apache.org/0101/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html#offsetsForTimes(java.util.Map)

After getting the offset you can seek using it and starting to read from there.