Confluent Kafka
Python
Message Consumption
Big Data
Programming Tips

How to consume messages in last N days using confluent-kafka-python?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. Confluent Kafka, built on Apache Kafka, extends these capabilities with additional features aimed at enterprise needs. The confluent-kafka-python library provides Python bindings for the Confluent Kafka client, which can be utilized to produce and consume messages effectively.

Consuming Messages from the Last N Days

When consuming messages from Kafka, you might often want to read messages that were produced in the last N days. This is particularly useful for applications that need to process data from a specific time window due to operational requirements or for batch processing activities.

Setting Up confluent-kafka-python

Before diving into the specifics of consuming messages based on timestamp, ensure your environment is set up with confluent-kafka-python. You can install it via pip:

bash
pip install confluent-kafka

Configuring the Consumer

To start consuming messages, you need to configure and initialize a Kafka consumer. Here is how you can set it up:

python
1from confluent_kafka import Consumer, KafkaError
2
3config = {
4    'bootstrap.servers': 'localhost:9092',
5    'group.id': 'mygroup',
6    'auto.offset.reset': 'earliest'
7}
8
9consumer = Consumer(config)

Consuming Messages based on Timestamp

Kafka messages are immutable and have their timestamp either set by the producer or by the Kafka broker at the time they are appended to the log. To fetch messages from the last N days, you utilize the timestamp to filter messages directly.

Here's how you can calculate the timestamp for the past N days and use it to assign partitions and start offsets:

python
1import time
2from datetime import datetime, timedelta
3
4# Define the topic
5topic = 'your-topic-name'
6
7# Calculate N days ago timestamp
8N = 3  # Change N to your required days
9days_ago = datetime.now() - timedelta(days=N)
10timestamp_n_days_ago = int(time.mktime(days_ago.timetuple()) * 1000)  # Kafka uses milliseconds
11
12# Get the partitions for the topic
13partitions = consumer.list_topics(topic).topics[topic].partitions
14
15# Calculate the offsets for each partition since N days ago
16offsets_for_time = consumer.offsets_for_times({topic_partition: timestamp_n_days_ago for topic_partition in partitions})
17
18# Assign partitions and seek to the calculated offsets
19for partition, offset in offsets_for_time.items():
20    if offset.offset >= 0:
21        consumer.assign([partition])
22        consumer.seek(partition, offset.offset)

Consuming and Processing Messages

Now that you have assigned the consumer to the right offsets, you can start consuming messages:

python
1try:
2    while True:
3        msg = consumer.poll(timeout=1.0)
4        if msg is None:
5            continue
6        if msg.error():
7            if msg.error().code() == KafkaError._PARTITION_EOF:
8                # End of partition event
9                print(f'{msg.topic()} {msg.partition()} reached end at offset {msg.offset()}')
10            elif msg.error():
11                raise KafkaException(msg.error())
12        else:
13            print(f"Received message: {msg.value().decode('utf-8')}")
14finally:
15    # Clean up on exit
16    consumer.close()

Key Points Summary

FunctionDescriptionRelevance
list_topics()Fetch metadata about topicsUsed to get partitions of a topic
offsets_for_times()Fetch offsets based on timestampsUtilized to find offsets from N days ago
assign() and seek()Manually assign partitions and offsetsEssential for starting consumption from a specific timestamp
poll()Fetch data from KafkaThe core function to receive messages based on set criteria

Conclusion

Consuming messages from the last N days in Kafka using confluent-kafka-python involves setting the right configuration for your consumer, determining the correct offsets based on timestamps, and properly assigning partitions. This setup is especially beneficial for applications that need to process historical data within a given timeframe.

Always ensure to handle errors and clean up the consumer properly to avoid memory leaks or other potential issues. By leveraging the capabilities of Confluent Kafka and Python, you can efficiently process large volumes of data in a scalable manner.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.