Kafka
High Level API
Message Offset
Kafka Message Reading
Programming

read kafka message starting from a specific offset using high level API

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 powerful distributed streaming platform capable of handling large volumes of data. One of its core features includes the ability to read messages starting from a specific offset, which allows for precise control and flexibility in data processing. This article will delve into how to use Kafka's high-level consumer API to start reading messages from a specified offset, with technical explanations and examples.

Understanding Kafka Offsets

In Kafka, every message within a partition of a topic has a unique sequence identifier called an 'offset'. Kafka maintains these offsets to track which messages have been consumed and to ensure the ability to replay or reprocess messages. This feature allows consumers to start reading from any given offset, making the system very flexible.

High-Level Consumer API

The high-level consumer API abstracts many of the complexities involved in managing offsets and broker connections. However, it has limited capabilities when it comes to controlling offsets directly as compared to the low-level API (SimpleConsumer API). With high-level API (KafkaConsumer in newer versions), you can specify the starting offset on a per-partition basis during initialization or reset.

Steps to Read from a Specific Offset

Below are the detailed steps and code samples using Java that illustrate how to specify offsets with the Kafka high-level API.

  1. Configure Kafka Consumer: Set up the basic consumer configurations.
  2. Assign Topic and Partition: Subscribe to a specific topic and partition.
  3. Seek to Specific Offset: Before polling for data, set the offset from which the consumer should start reading.
java
1import org.apache.kafka.clients.consumer.KafkaConsumer;
2import org.apache.kafka.clients.consumer.ConsumerRecords;
3import org.apache.kafka.common.TopicPartition;
4
5import java.util.Arrays;
6import java.util.Properties;
7
8public class KafkaExampleConsumer {
9    public static void main(String[] args) {
10        Properties properties = new Properties();
11        properties.put("bootstrap.servers", "localhost:9092");
12        properties.put("group.id", "test-group");
13        properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
14        properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
15        
16        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);
17        
18        TopicPartition topicPartition = new TopicPartition("your-topic-name", 0);
19        consumer.assign(Arrays.asList(topicPartition));
20        
21        // Specify the offset to start reading from
22        long startOffset = 1234L;
23        consumer.seek(topicPartition, startOffset);
24        
25        try {
26            while (true) {
27                ConsumerRecords<String, String> records = consumer.poll(100);
28                records.forEach(record -> {
29                    System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
30                });
31            }
32        } finally {
33            consumer.close();
34        }
35    }
36}

Key Points Summary Table

Key FeatureDescription
Offset ManagementKafka manages unique sequence identifiers for messages, which can be leveraged to start reading from any position.
High-Level APIProvides a simpler interface for consuming data but tools like manual offset control are more limited compared to the low-level API.
Seek MethodThis method is used to specify the exact offset from which the consumer should start or resume reading.
KafkaConsumerModern class in Kafka API replacing the older Consumer classes, providing methods such as seek(), assign(), and poll() for efficient message consumption.
FlexibilityKafka Consumers can read messages in real-time or from a specified point in history, enhancing the flexibility of message processing applications.

Considerations

  • Consumer Groups and Offset Committing: If your application is part of a consumer group and does not manage offset committing manually, unexpected re-balances could affect where your consumer starts reading. Always ensure that offset committing is handled consistently to prevent data loss or duplication.
  • Topic and Partition Scalability: Always be aware that topic partitioning and scalability should be considered in the design of your Kafka infrastructure to accommodate varying offsets and consumer groups.

By following these steps and considerations, you can precisely control how you consume messages from Kafka using offsets, enhancing both fault tolerance and data processing capabilities.


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.