Kafka
Querying
Data Retrieval
Record Search
Topic Management

Query Kafka topic for specific record

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 an open-source distributed event streaming platform used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications. Among its most common uses is processing and storing streams of data records. At times, users may need to query a Kafka topic for specific records, which can involve various approaches depending on the requirement. Below, we explore methods to achieve this.

Understanding Kafka Topics

A Kafka topic is a category or feed name to which records are published. Topics in Kafka are always multi-subscriber; that is, a topic can have zero, one, or many consumers that subscribe to the data written to it. Each record in a topic is stored in a data structure called a Kafka partition, which allows the topic to scale by distributing data across multiple nodes in a Kafka cluster.

Prerequisites for Querying Kafka Topics

  • Kafka Setup: A working Kafka environment.
  • Producer and Consumer API Knowledge: Basic understanding of Kafka Producers and Consumers.
  • Key-based Filtering: Ensures records are partitioned by specific keys (e.g., user IDs).

Methods to Query Specific Records

1. Direct Consumer API Usage

Using the Kafka Consumer API directly is the most straightforward approach. You can subscribe to a topic and filter messages in the client application based on certain conditions.

java
1import org.apache.kafka.clients.consumer.KafkaConsumer;
2import org.apache.kafka.clients.consumer.ConsumerRecord;
3import org.apache.kafka.clients.consumer.ConsumerRecords;
4
5import java.util.Arrays;
6import java.util.Properties;
7
8public class KafkaExampleConsumer {
9    public static void main(String[] args) {
10        Properties props = new Properties();
11        props.put("bootstrap.servers", "localhost:9092");
12        props.put("group.id", "test");
13        props.put("enable.auto.commit", "true");
14        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
15        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
16
17        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
18        consumer.subscribe(Arrays.asList("my-topic"));
19        
20        try {
21            while (true) {
22                ConsumerRecords<String, String> records = consumer.poll(100);
23                for (ConsumerRecord<String, String> record : records) {
24                    if (record.key().equals("specificKey")) {
25                        System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
26                    }
27                }
28            }
29        } finally {
30            consumer.close();
31        }
32    }
33}

This method involves reading through all the messages and filtering out unwanted ones, which might not be efficient if the dataset is large.

2. Interactive Queries in Kafka Streams

Kafka Streams API provides a way to perform real-time processing on stream data. It supports interactive queries, which allow you to retrieve data from a state store in a point-in-time fashion.

java
1import org.apache.kafka.streams.KafkaStreams;
2import org.apache.kafka.streams.state.QueryableStoreTypes;
3import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
4
5public class KafkaStreamInteractiveQuery {
6    public static void main(String[] args) {
7        KafkaStreams streams = // initialize your Kafka Streams application
8        streams.start();
9
10        ReadOnlyKeyValueStore<String, String> keyValueStore =
11                streams.store("yourStoreName", QueryableStoreTypes.keyValueStore());
12
13        String value = keyValueStore.get("specificKey");
14        System.out.println("Value for 'specificKey': " + value);
15    }
16}

This approach is efficient for frequent queries as it maintains a local store and is limited to data already processed by the Kafka Streams application.

3. Kafka Connect and External Systems

Sometimes, using external systems like databases for indexing and querying Kafka data can be beneficial. Kafka Connect can be configured to sink data into systems like Elasticsearch, where records can be queried using powerful search capabilities.

Summary Table

MethodUse CaseEfficiencyComplexity
Direct Consumer APISmall datasets or low query frequencyLow (Full scan required)Low
Kafka Streams Interactive QueriesMedium datasets with high query needsHigh (Stateful local store)Medium
Kafka Connect to External SystemsLarge datasets, complex queriesVery High (Distributed stores)High

Conclusion

Querying specific records from a Kafka topic consists of various approaches, each suitable based on data size, query frequency, and complexity. While direct consumption is simpler, leveraging Kafka Streams or external data systems can provide more efficient and powerful query 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.