0
0
Kafkadevops~10 mins

Subscribing to topics in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Subscribing to topics
Create Kafka Consumer
Configure Consumer Settings
Subscribe to Topic(s)
Poll for Messages
Process Messages
Repeat Polling or Exit
This flow shows how a Kafka consumer is created, configured, subscribes to topics, polls messages, and processes them repeatedly.
Execution Sample
Kafka
from kafka import KafkaConsumer
consumer = KafkaConsumer('my_topic', bootstrap_servers=['localhost:9092'])
for message in consumer:
    print(message.value.decode('utf-8'))
This code creates a Kafka consumer that subscribes to 'my_topic' and prints each message received.
Process Table
StepActionEvaluationResult
1Create KafkaConsumer objectKafkaConsumer('my_topic', ...)Consumer created, ready to subscribe
2Subscribe to 'my_topic'Subscription setConsumer listens to 'my_topic'
3Poll for messagesCheck if messages availableNo messages yet, wait
4Receive messageMessage received from 'my_topic'Message object with value bytes
5Decode message.valuemessage.value.decode('utf-8')String message content
6Print message contentprint outputMessage content shown on screen
7Repeat pollingLoop continuesWait for next message or exit
💡 Consumer stops when program ends or interrupted
Status Tracker
VariableStartAfter Step 1After Step 4After Step 5Final
consumerNoneKafkaConsumer objectKafkaConsumer with message readyKafkaConsumer objectKafkaConsumer looping for messages
messageNoneNoneMessage object with bytesMessage object with bytesNext message or None
Key Moments - 3 Insights
Why do we decode message.value before printing?
Kafka messages are bytes, not strings. Decoding converts bytes to readable text, as shown in step 5 of the execution_table.
What happens if no messages are available when polling?
The consumer waits (blocks) until messages arrive, as seen in step 3 where polling checks but finds no messages yet.
How does the consumer know which topic to listen to?
The topic is specified when creating the consumer or by calling subscribe(), as in step 2 where 'my_topic' is set.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'message' after step 4?
ADecoded string message
BMessage object with bytes
CNone
DKafkaConsumer object
💡 Hint
Check the 'message' variable in variable_tracker after step 4 and execution_table row 4
At which step does the consumer start listening to 'my_topic'?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look at execution_table row 2 where subscription is set
If the topic name changes, which step in the execution_table would be affected?
AStep 3
BStep 5
CStep 1
DStep 6
💡 Hint
Topic is specified when creating the consumer object in step 1
Concept Snapshot
Kafka Consumer Subscription:
- Create KafkaConsumer with topic(s)
- Consumer subscribes to topics automatically
- Poll messages in a loop
- Messages are bytes, decode before use
- Process messages as they arrive
- Loop continues until stopped
Full Transcript
This visual execution shows how a Kafka consumer subscribes to topics and processes messages. First, a KafkaConsumer object is created with the topic name. Then the consumer subscribes to that topic and waits for messages. When messages arrive, they are received as bytes and decoded to strings before printing. The consumer keeps polling and processing messages until the program stops. Key points include decoding bytes to strings and understanding the subscription step.