Kafka Console Consumer
Message Consumption
Timestamps
Data Streaming
Kafka Tutorial

How to consume messages between two timestamps using Kafka Console Consumer

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka is a powerful distributed event streaming platform capable of handling trillions of events a day. One common task when working with Kafka is consuming messages that fall between two specific timestamps. This capability is very useful for various scenarios such as data recovery, analysis, and auditing.

Understanding Time-Based Consumption in Kafka

Kafka utilizes two primary time concepts: event time and ingestion time. Event time is the actual time when the event occurred, as recorded by the producing application, whereas ingestion time is the time when the event was appended to the Kafka log.

To consume messages between two timestamps, it's important to have an understanding of Kafka's offset and how it relates to time. Each message in Kafka is assigned a unique sequential ID called an offset. Offsets allow Kafka consumers to keep track of which messages have been consumed. Kafka also stores a timestamp for each message.

Pre-Requisites

Before diving into the specifics of consuming messages between two timestamps, you will need:

  • Apache Kafka (including the Kafka server and the Kafka CLI tools) installed and running.
  • Knowledge of the topic from which you want to consume messages.

Using Kafka Console Consumer

The Kafka Console Consumer is a tool that allows you to read messages from Kafka topics from the command line. To specify the consumption between two timestamps, you can leverage the --offsets-for-times parameter.

Fetching Offsets for Times

The first step is to determine the offsets corresponding to your desired start and end timestamps.

bash
kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 \
  --topic your-topic-name --time <timestamp>

Replace <timestamp> with the milliseconds since epoch of your target time. This command will return the earliest offset whose timestamp is greater than or equal to the given timestamp. Perform this for both your start and end timestamps.

Consuming Messages

Once you have the starting and ending offsets, you can consume messages as follows:

bash
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic your-topic-name \
  --offset <start-offset> --partition 0 --max-messages <number-of-messages>

Here, <start-offset> is the starting offset corresponding to your start timestamp, and <number-of-messages> should be calculated as (end-offset - start-offset + 1).

Example

Imagine you want to consume messages from a topic called "sample-topic" between January 1, 2021, and January 2, 2021.

  1. Fetching Start Offset (January 1, 2021):
bash
   kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 \
     --topic sample-topic --time 1609459200000

This might return an offset of 25.

  1. Fetching End Offset (January 2, 2021):
bash
   kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 \
     --topic sample-topic --time 1609545600000

This might return an offset of 75.

  1. Consume Messages:
bash
   kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic sample-topic \
     --offset 25 --partition 0 --max-messages 51

Summary Table

ItemDescription
TopicThe Kafka topic from which to consume messages.
Start TimestampTimestamp (in milliseconds) to start consuming messages.
End TimestampTimestamp (in milliseconds) to stop consuming messages.
Start OffsetOffset corresponding to the start timestamp.
End OffsetOffset corresponding to the end timestamp.
Number of MessagesCalculated as (end-offset - start-offset + 1).

Conclusion

Consuming messages between two specific timestamps in Kafka involves calculating offsets for those timestamps and then using those offsets to limit message consumption. This operation can be invaluable for scenarios that require precision and specific time-sliced data analysis.


Course illustration
Course illustration

All Rights Reserved.