0
0
Kafkadevops~10 mins

Producer API basics in Kafka - Interactive Code 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.

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", [1]);
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
Drag options to blanks, or click blank then click option'
A"org.apache.kafka.common.serialization.StringSerializer"
B"org.apache.kafka.common.serialization.IntegerSerializer"
C"org.apache.kafka.common.serialization.ByteArraySerializer"
D"org.apache.kafka.common.serialization.LongSerializer"
Attempts:
3 left
💡 Hint
Common Mistakes
Using IntegerSerializer when sending strings.
Forgetting to set value.serializer property.
2fill in blank
medium

Complete the code to send a message to topic "my-topic".

Kafka
ProducerRecord<String, String> record = new ProducerRecord<>("my-topic", [1], "Hello Kafka");
producer.send(record);
Drag options to blanks, or click blank then click option'
A"key1"
Bnull
C"value1"
D"my-key"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value instead of a key in the ProducerRecord constructor.
Passing an undefined variable as key.
3fill in blank
hard

Fix the error in the code to properly close the producer.

Kafka
producer.[1]();
Drag options to blanks, or click blank then click option'
Aclose
Bstop
Cflush
Dshutdown
Attempts:
3 left
💡 Hint
Common Mistakes
Using flush() instead of close(), which does not close the producer.
Using shutdown() or stop(), which are not valid methods.
4fill in blank
hard

Fill both blanks to create a ProducerRecord with a key and value.

Kafka
ProducerRecord<String, String> record = new ProducerRecord<>("my-topic", [1], [2]);
Drag options to blanks, or click blank then click option'
A"user1"
B"Hello"
C"World"
D"key1"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping key and value positions.
Using invalid strings for key or value.
5fill in blank
hard

Fill all three blanks to send a message and flush the producer.

Kafka
ProducerRecord<String, String> record = new ProducerRecord<>("my-topic", [1], [2]);
producer.send(record);
producer.[3]();
Drag options to blanks, or click blank then click option'
A"key123"
B"Kafka Rocks"
Cflush
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using close() instead of flush() before sending more messages.
Mixing up key and value positions.