In Kafka how to get the exact offset according producing time

后端 未结 5 2113
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 07:58

I need to get the message produced in Kafka hour by hour in a day. Every one hour I will launch a job to consume the message produced 1 hour ago. e.g., if current time is 20:12,

5条回答
  •  清歌不尽
    2021-02-04 08:12

    As the other replies note, older versions of Kafka had only an approximate way of mapping times to offsets. However, since Kafka 0.10.0 (released in May 2016), Kafka maintains a time index for each topic. This will allow you to efficiently get from times to exact offsets. You can use the KafkaConsumer#offsetsForTimes method to access this information.

    There are more details about how the time-based index is implemented on the KIP-33 design discussion page.

提交回复
热议问题