Complete the code to create a Kafka producer in Confluent Cloud.
from confluent_kafka import Producer conf = {'bootstrap.servers': '[1]', 'sasl.mechanism': 'PLAIN'} producer = Producer(conf)
The correct bootstrap server for Confluent Cloud includes the cluster endpoint with port 9092.
Complete the code to send a message to a topic in Confluent Cloud.
producer.produce('[1]', key='key1', value='Hello, Confluent!') producer.flush()
The topic name must match the one created in Confluent Cloud; here, 'test_topic' is the example topic.
Fix the error in the configuration to connect securely to Confluent Cloud.
conf = {
'bootstrap.servers': 'pkc-xyz.us-west1.gcp.confluent.cloud:9092',
'security.protocol': '[1]',
'sasl.mechanism': 'PLAIN',
'sasl.username': 'API_KEY',
'sasl.password': 'API_SECRET'
}Confluent Cloud requires SASL_SSL for secure authentication and encryption.
Fill both blanks to create a consumer that subscribes to a topic and polls messages.
from confluent_kafka import Consumer conf = { 'bootstrap.servers': 'pkc-xyz.us-west1.gcp.confluent.cloud:9092', 'group.id': '[1]', 'auto.offset.reset': '[2]', 'security.protocol': 'SASL_SSL', 'sasl.mechanism': 'PLAIN', 'sasl.username': 'API_KEY', 'sasl.password': 'API_SECRET' } consumer = Consumer(conf) consumer.subscribe(['test_topic'])
The consumer group id is 'my_group' and 'auto.offset.reset' is set to 'earliest' to read from the beginning if no offset is found.
Fill all three blanks to create a dictionary comprehension that maps topic names to their partition counts if partitions are more than 1.
topics = {'topic1': 1, 'topic2': 3, 'topic3': 2}
partition_map = { [1]: [2] for [3] in topics if topics[[3]] > 1 }The comprehension iterates over topic names as 'topic', maps each to its partition count 'topics[topic]', and filters where partitions > 1.