📖 Scenario: You are working with Apache Kafka to read messages from a topic. Kafka is like a messaging system where producers send messages and consumers read them. In this project, you will create a simple Kafka consumer that reads messages from a topic called test-topic.
🎯 Goal: Build a Kafka consumer that connects to a Kafka server, subscribes to the test-topic, reads messages, and prints them to the console.
📋 What You'll Learn
Create a Kafka consumer configuration with the correct server and group ID
Subscribe the consumer to the test-topic
Poll messages from the topic
Print the received messages to the console
💡 Why This Matters
🌍 Real World
Kafka consumers are used in real systems to process streams of data like logs, user activity, or sensor data in real time.
💼 Career
Understanding Kafka consumer basics is essential for roles in data engineering, backend development, and DevOps where streaming data processing is common.
Progress0 / 4 steps
1
Create Kafka consumer configuration
Create a dictionary called consumer_config with these exact entries: 'bootstrap.servers': 'localhost:9092', 'group.id': 'my-group', 'auto.offset.reset': 'earliest'.
Kafka
Hint
Use a Python dictionary with the exact keys and values as shown.
2
Create Kafka consumer instance
Import Consumer from confluent_kafka and create a variable called consumer by passing consumer_config to Consumer().
Kafka
Hint
Use from confluent_kafka import Consumer and then create consumer = Consumer(consumer_config).
3
Subscribe to topic and poll messages
Use consumer.subscribe(['test-topic']) to subscribe to the topic. Then write a for loop that runs 5 times, inside the loop call consumer.poll(1.0) to get a message and store it in msg. Check if msg is not null and not an error, then print msg.value().decode('utf-8').
Kafka
Hint
Subscribe with consumer.subscribe(['test-topic']). Use a for loop to poll 5 messages. Check for msg not null and no error before printing.
4
Close the consumer and print completion message
After the loop, call consumer.close() to close the consumer. Then write print("Finished reading messages") to display the completion message.
Kafka
Hint
Call consumer.close() after the loop. Then print the exact message "Finished reading messages".
Practice
(1/5)
1. What is the primary purpose of the Kafka Consumer API?
easy
A. To send messages to Kafka topics
B. To delete Kafka topics
C. To create Kafka topics
D. To read messages from Kafka topics
Solution
Step 1: Understand Kafka Consumer API role
The Consumer API is designed to read or consume messages from Kafka topics.
Step 2: Differentiate from Producer API
The Producer API sends messages, while Consumer API reads them.
Final Answer:
To read messages from Kafka topics -> Option D
Quick Check:
Consumer API = Read messages [OK]
Hint: Consumer API reads messages, Producer API sends messages [OK]
Common Mistakes:
Confusing Consumer API with Producer API
Thinking Consumer API creates or deletes topics
Mixing up reading and writing operations
2. Which of the following is the correct way to create a Kafka consumer in Java?
5. You want to process messages from multiple topics and commit offsets manually after processing. Which code snippet correctly sets up the Kafka consumer for this use case?
hard
A. props.put("enable.auto.commit", "false");
consumer.subscribe(Arrays.asList("topicA", "topicB"));
while (true) {
ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord record : records) {
process(record.value());
}
consumer.commitSync();
}
B. props.put("enable.auto.commit", "true");
consumer.subscribe(Collections.singletonList("topicA"));
while (true) {
ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord record : records) {
process(record.value());
}
}
C. props.put("enable.auto.commit", "false");
consumer.assign(Arrays.asList(new TopicPartition("topicA", 0)));
consumer.subscribe(Arrays.asList("topicB"));
while (true) {
ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord record : records) {
process(record.value());
}
consumer.commitAsync();
}
D. props.put("enable.auto.commit", "false");
consumer.subscribe(Arrays.asList("topicA", "topicB"));
while (true) {
ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord record : records) {
process(record.value());
}
}
Solution
Step 1: Disable auto commit for manual offset control
Setting "enable.auto.commit" to "false" allows manual offset commits.
Step 2: Subscribe to multiple topics correctly
Use consumer.subscribe(Arrays.asList("topicA", "topicB")) to listen to multiple topics.
Step 3: Commit offsets after processing
Calling consumer.commitSync() after processing ensures offsets are saved manually.
Final Answer:
Code snippet with manual commit, multiple topic subscription, and commitSync() -> Option A
Quick Check:
Manual commit + multiple topics = props.put("enable.auto.commit", "false");
consumer.subscribe(Arrays.asList("topicA", "topicB"));
while (true) {
ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord record : records) {
process(record.value());
}
consumer.commitSync();
} [OK]
Hint: Disable auto commit and call commitSync() after processing [OK]
Common Mistakes:
Using auto commit when manual commit is needed
Mixing assign() and subscribe() incorrectly
Forgetting to commit offsets after processing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.