localhost:9092test-topiclocalhost:9092test-topicconf with these exact entries: 'bootstrap.servers': 'localhost:9092', 'group.id': 'mygroup', and 'auto.offset.reset': 'earliest'.This dictionary holds the settings needed to connect to Kafka.
Consumer from confluent_kafka and create a variable called consumer by passing the conf dictionary to Consumer(conf).Use the Consumer class to create a consumer object with the configuration.
consumer.subscribe() method to subscribe to a list containing the single topic 'test-topic'.Subscribe to the topic by passing a list with the topic name.
while True loop that polls messages from consumer.poll(1.0). If a message is received and has no error, print its value decoded as UTF-8. Break the loop after printing one message.This loop waits for a message, checks for errors, prints the message text, then stops.
What is the main purpose of a Python Kafka consumer?
Which of the following is the correct way to create a KafkaConsumer in Python to read from topic my_topic on localhost?
from kafka import KafkaConsumer consumer = KafkaConsumer(_____)
What will be the output of this code snippet if the Kafka topic test has one message with value b'hello'?
from kafka import KafkaConsumer
consumer = KafkaConsumer("test", bootstrap_servers='localhost:9092', auto_offset_reset='earliest', group_id='group1')
for msg in consumer:
print(msg.value.decode('utf-8'))
breakIdentify the error in this Kafka consumer code snippet:
from kafka import KafkaConsumer
consumer = KafkaConsumer('topic1', bootstrap_servers='localhost:9092')
for message in consumer:
print(message.value.decode())
consumer.close()You want to consume messages from two Kafka topics topicA and topicB using one Python KafkaConsumer instance. Which code snippet correctly sets this up?
from kafka import KafkaConsumer
consumer = KafkaConsumer(_____)
for msg in consumer:
print(f"Topic: {msg.topic}, Message: {msg.value.decode()}")Pass the test and this pattern moves unknown