Kafka
Topic Modification
Message Partitioning
Data Streaming
Kafka API

Kafka How to get last modified time for a topic i.e. last message added to any partition of the topic

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 distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. Since it deals with streams of data, understanding when data (messages) are added is crucial for many applications, especially those dealing with real-time processing or monitoring changes. However, unlike traditional file systems or databases, Kafka does not explicitly store the timestamp of the last modification on a topic. Instead, timestamps can be associated with individual messages (records) when they are produced.

Understanding Kafka Timestamps

Kafka messages contain timestamps that serve various purposes such as event-time processing or log compaction policies. There are two types of timestamps in Kafka messages:

  • Creation Time: The timestamp when the message was produced.
  • Log Append Time: The timestamp when the message was appended to the log on the server.

By default, Kafka uses the Creation Time, which is set when the message is produced. However, it can be configured at the broker (server) level to use the Log Append Time instead.

Fetching Last Modified Time of a Kafka Topic

To determine the last modified time of a topic, one approach is to identify the last message added across all its partitions. Here's how you can achieve this using Kafka’s command-line tools and consumer APIs.

Using Kafka Command Line Tools

Kafka ships with a command-line tool called kafka-console-consumer. However, this tool doesn’t directly provide the last modified time but can help in consuming the latest messages where you can manually check the timestamp.

To get the latest messages and their timestamps:

bash
kafka-console-consumer --bootstrap-server localhost:9092 --topic my-topic --from-beginning --max-messages 1 --property print.timestamp=true

Replace localhost:9092 with your Kafka cluster's bootstrap server address and my-topic with your topic name. The output will display the timestamp of the message depending on your broker configurations (Creation Time or Log Append Time).

Using Kafka Consumer API

For more granular control or automation, you can use Kafka’s Consumer API. Below is a simple example in Java:

java
1import org.apache.kafka.clients.consumer.KafkaConsumer;
2import org.apache.kafka.clients.consumer.ConsumerRecord;
3import org.apache.kafka.clients.consumer.ConsumerRecords;
4import org.apache.kafka.common.TopicPartition;
5
6import java.util.Arrays;
7import java.util.Properties;
8
9public class LastModifiedTime {
10    public static void main(String[] args) {
11        String topicName = "my-topic";
12        Properties props = new Properties();
13        props.put("bootstrap.servers", "localhost:9092");
14        props.put("group.id", "test");
15        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
16        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
17
18        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
19        TopicPartition partition = new TopicPartition(topicName, 0);  // Example with one partition
20        consumer.assign(Arrays.asList(partition));
21
22        // Seek to the end to get the last message
23        consumer.seekToEnd(Arrays.asList(partition));
24        long lastOffset = consumer.position(partition) - 1;
25        consumer.seek(partition, lastOffset);
26
27        // Poll to get the last message
28        ConsumerRecords<String, String> records = consumer.poll(100);
29        for (ConsumerRecord<String, String> record : records) {
30            System.out.println("Last Modified Time: " + record.timestamp());
31        }
32        consumer.close();
33    }
34}

This example demonstrates how to connect to a Kafka topic, navigate to the last message of a specified partition, and print its timestamp. To fully determine the last modified time across all partitions of a topic, you would need to iterate across all the topic’s partitions and compare the timestamps.

Key Points Summary

Here is a summary table of the key methods to determine the last modified time in Kafka:

MethodProsCons
Kafka Console ConsumerEasy to use; Quick setupManual; Less control; Not real-time
Kafka Consumer APIProgrammatic control; Real-time accessRequires programming; More setup

Conclusion

Kafka does not directly provide a last modified timestamp for topics, but the timestamp metadata on individual messages can be utilized to infer this information. Depending on the requirements—whether it's a one-time check or continuous monitoring—the Kafka command-line tools or Consumer API are both viable methods to get the data needed. Advanced users can leverage the Consumer API for more precise and real-time applications.


Course illustration
Course illustration

All Rights Reserved.