Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using IntegerSerializer when sending strings.
Forgetting to set value.serializer property.
✗ Incorrect
The value.serializer must be set to StringSerializer to send string messages.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value instead of a key in the ProducerRecord constructor.
Passing an undefined variable as key.
✗ Incorrect
You can send a message without a key by passing null.
3fill in blank
hardFix the error in the code to properly close the producer.
Kafka
producer.[1](); Drag options to blanks, or click blank then click option'
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.
✗ Incorrect
The close() method properly closes the producer and releases resources.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping key and value positions.
Using invalid strings for key or value.
✗ Incorrect
The first blank is the key "user1", the second blank is the value "World".
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using close() instead of flush() before sending more messages.
Mixing up key and value positions.
✗ Incorrect
The key is "key123", the value is "Kafka Rocks", and flush() sends all buffered records.