问题
I want to read all the messages starting from a specific time in kafka. Say I want to read all messages between 0600 to 0800
Request messages between two timestamps from Kafka suggests the solution as the usage of offsetsForTimes.
Problem with that solution is : If say my consumer is switched on everyday at 1300. The consumer would not have read any messages that day, which effectively means no offset was committed at/after 0600, which means offsetsForTimes(< partitionname > , <0600 for that day in millis>) will return null.
Is there any way I can read a message which was published to kafka queue at a certain time, irrespective of offsets?
回答1:
offsetsForTimes() returns offsets of messages that were produced for the requested time. It works regardless if offsets were committed or not because the offsets are directly fetched from the partition logs.
So yes you should be using this method to find the first offset produced after 0600, seek to that position and consume messages until you reach 0800.
来源:https://stackoverflow.com/questions/55095004/reading-messages-for-specific-timestamp-in-kafka