Console Consumer ยท Python Consumer ยท Two Producers, One Stream
| Time | Segment | Format |
|---|---|---|
| 0 โ 10 | Recap โ TCS + INFY producers from Lecture 6 | Q&A |
| 10 โ 20 | What is a consumer? How it connects to a topic | Lecture |
| 20 โ 40 | Console consumer โ verify stream is live | Demo + Lab |
| 40 โ 70 | Python consumer โ read from stock-topic | Lab |
| 70 โ 85 | One consumer reading both TCS & INFY data | Lab |
| 85 โ 90 | Takeaways + Lecture 8 preview | Wrap-up |
In Lecture 6 you built two producers that both write to stock-topic. Today we read from it.
Key insight: One consumer subscribing to stock-topic automatically sees messages from all producers writing to it โ TCS and INFY arrive in the same stream.
Before writing Python โ verify your stream is live with zero code. Run a producer in one terminal, this in another.
docker exec -it sda-kafka-1 kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic stock-topic \
--from-beginning
docker exec -it sda-kafka-1 kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic stock-topic
Subscribe to stock-topic and print every message. Open a terminal alongside your running producer.
from kafka import KafkaConsumer
import json
consumer = KafkaConsumer(
'stock-topic',
bootstrap_servers=['localhost:9092'],
auto_offset_reset='earliest', # read ALL past messages first
value_deserializer=lambda x: json.loads(x.decode('utf-8'))
)
print("Listening on stock-topic...")
for msg in consumer:
data = msg.value
print(data)
Try it: Start the consumer first, then run python tcs_producer.py in another terminal. Watch messages arrive live.
pip install kafka-python
Run tcs_producer.py and infy_producer.py simultaneously. One consumer reads both โ differentiate by the symbol field.
from kafka import KafkaConsumer
import json
consumer = KafkaConsumer(
'stock-topic',
bootstrap_servers=['localhost:9092'],
auto_offset_reset='earliest',
value_deserializer=lambda x: json.loads(x.decode('utf-8'))
)
tcs_count = infy_count = 0
print("๐ก Listening to stock-topic (TCS + INFY)...")
for msg in consumer:
data = msg.value
sym = data.get('symbol', '?')
price = data.get('price', data.get('Close', '?'))
if sym == 'TCS.NS':
tcs_count += 1
print(f"๐ต TCS โน{price} (msg #{tcs_count})")
elif sym == 'INFY.NS':
infy_count += 1
print(f"๐ข INFY โน{price} (msg #{infy_count})")
else:
print(f"โช {sym} โ {data}")
๐ก Listening to stock-topic (TCS + INFY)...
๐ต TCS โน3842.5 (msg #1)
๐ต TCS โน3851.0 (msg #2)
๐ข INFY โน1724.3 (msg #1)
๐ต TCS โน3860.2 (msg #3)
๐ข INFY โน1731.8 (msg #2)
Open 4 terminal windows. Follow the sequence below.
| Terminal | Command | What it does |
|---|---|---|
| T1 | docker-compose up -d |
Start Kafka + Zookeeper |
| T2 | python tcs_producer.py |
Stream TCS prices โ stock-topic |
| T3 | python infy_producer.py |
Stream INFY prices โ stock-topic |
| T4 | python consumer_stock.py |
Read all messages, tag by symbol |
Challenge: Modify consumer_stock.py to also print the count of messages received for each symbol every 5 messages. Hint: use a counter variable and if (tcs_count + infy_count) % 5 == 0.
Next โ Lecture 8: Save this streaming data to MongoDB Atlas so it persists beyond the terminal session.
earliest reads all past messages; latest reads only new onessymbol, source or any field to separate streams in codeNext โ Lecture 8: Save streaming data to MongoDB Atlas โ free cluster setup + persistent storage