Complete the code to create a Kafka producer that sends messages to a topic.
producer = KafkaProducer(bootstrap_servers='localhost:9092') producer.send('[1]', b'Hello Kafka') producer.flush()
The send method requires the topic name as the first argument. Here, my_topic is the correct topic name.
Complete the code to consume messages from a Kafka topic.
consumer = KafkaConsumer('[1]', bootstrap_servers='localhost:9092') for message in consumer: print(message.value)
The consumer needs the topic name to subscribe to messages. my_topic is the correct topic name.
Fix the error in the code to ensure the Kafka consumer joins the correct consumer group.
consumer = KafkaConsumer('my_topic', bootstrap_servers='localhost:9092', group_id=[1]) for message in consumer: print(message.value)
The group_id must be a string identifying the consumer group, like 'my_group'.
Fill both blanks to create a Kafka topic with 3 partitions and a replication factor of 2.
topic_config = NewTopic(name='my_topic', num_partitions=[1], replication_factor=[2])
The topic needs 3 partitions and a replication factor of 2 to ensure reliability through distribution.
Fill all three blanks to configure a Kafka producer with retries, acks, and a timeout for reliability.
producer = KafkaProducer(bootstrap_servers='localhost:9092', retries=[1], acks=[2], request_timeout_ms=[3])
acks=1 or acks=0 reduces reliability.Retries set to 5 allow resending messages on failure, acks='all' waits for all replicas to confirm, and request_timeout_ms=30000 sets a 30-second timeout for requests.