Consider a Kafka consumer configured to read from a topic with 3 partitions. The consumer group has 2 consumers. What will be the number of partitions assigned to each consumer?
from kafka import KafkaConsumer consumer = KafkaConsumer( 'my_topic', group_id='my_group', bootstrap_servers=['localhost:9092'] ) consumer.poll(timeout_ms=1000) partitions = consumer.assignment() print(len(partitions))
Partitions are distributed among consumers in a group as evenly as possible.
With 3 partitions and 2 consumers, partitions are split as 2 and 1. So one consumer gets 2 partitions, the other gets 1. The code prints the number of partitions assigned to the consumer, which is 2 in this case.
Choose the best description of what a Kafka broker does in the message broker architecture.
Think about where messages are stored and how clients interact with Kafka.
A Kafka broker is a server that stores messages in topics and partitions. It handles requests from producers to write messages and from consumers to read messages.
Examine the Kafka producer code below. It raises a serialization error when sending a message. What is the cause?
from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers='localhost:9092') message = {'key': 'value'} producer.send('my_topic', message) producer.flush()
Kafka producers require messages in a specific format before sending.
The KafkaProducer expects message values as bytes. Sending a Python dict without converting it to bytes causes a serialization error.
Identify the code snippet that enables automatic offset commits in a Kafka consumer.
Check the exact parameter name for enabling auto commit in KafkaConsumer.
The correct parameter to enable automatic offset commits is enable_auto_commit. Other options are invalid and cause errors.
You run the following command to create a Kafka topic:
kafka-topics.sh --create --topic orders --partitions 5 --replication-factor 3 --bootstrap-server localhost:9092
How many partitions will the topic orders have?
The --partitions flag sets the number of partitions.
The command explicitly sets the topic to have 5 partitions. The replication factor controls copies, not partitions.