0
0
Kafkadevops~10 mins

Kafka vs RabbitMQ vs Redis Pub/Sub - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Kafka producer that sends a message.

Kafka
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers='localhost:9092')
producer.send('my_topic', [1])
Drag options to blanks, or click blank then click option'
AHello, Kafka!
B'Hello, Kafka!'
Cb'Hello, Kafka!'
Dbytes('Hello, Kafka!', 'utf-8')
Attempts:
3 left
💡 Hint
Common Mistakes
Sending the message as a plain string without converting to bytes.
2fill in blank
medium

Complete the code to consume messages from a Kafka topic.

Kafka
from kafka import KafkaConsumer
consumer = KafkaConsumer('my_topic', bootstrap_servers='localhost:9092')
for message in [1]:
    print(message.value)
Drag options to blanks, or click blank then click option'
Aconsumer.poll()
Bconsumer.consume()
Cconsumer.read()
Dconsumer
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call non-existent methods like consume() or read().
3fill in blank
hard

Fix the error in the Kafka producer code to send a JSON message.

Kafka
import json
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers='localhost:9092', value_serializer=lambda v: [1])
producer.send('my_topic', {'key': 'value'})
Drag options to blanks, or click blank then click option'
Ajson.dumps(v).encode('utf-8')
Bjson.loads(v).encode('utf-8')
Cstr(v).encode('utf-8')
Djson.dumps(v)
Attempts:
3 left
💡 Hint
Common Mistakes
Using json.loads which parses JSON instead of serializing.
4fill in blank
hard

Fill both blanks to create a Kafka consumer that reads from the beginning and auto-commits offsets.

Kafka
from kafka import KafkaConsumer
consumer = KafkaConsumer('my_topic', bootstrap_servers='localhost:9092', auto_offset_reset=[1], enable_auto_commit=[2])
Drag options to blanks, or click blank then click option'
A'earliest'
BTrue
C'latest'
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'latest' which reads only new messages.
5fill in blank
hard

Fill all three blanks to create a Kafka producer with a custom serializer and send a message.

Kafka
import json
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers='localhost:9092', value_serializer=[1])
message = {'event': 'login', 'user': 'alice'}
producer.send('events', [2])
producer.flush()  # [3]
Drag options to blanks, or click blank then click option'
Alambda v: json.dumps(v).encode('utf-8')
Bmessage
CEnsure all messages are sent before exiting
Dmessage.encode('utf-8')
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to encode the message twice or forgetting to flush.