Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a Kafka producer with the correct property for the bootstrap servers.
Kafka
Properties props = new Properties(); props.put("bootstrap.servers", [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using only the host name without port.
Using only the port number.
Using an incorrect format without colon.
✗ Incorrect
The bootstrap.servers property requires the host and port, like "localhost:9092".
2fill in blank
mediumComplete the code to set the key serializer class for the Kafka producer.
Kafka
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, [1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using deserializer instead of serializer.
Using serializer for wrong data type.
✗ Incorrect
The producer needs a serializer, so use StringSerializer for keys of type String.
3fill in blank
hardFix the error in the code to send a message to the Kafka topic.
Kafka
ProducerRecord<String, String> record = new ProducerRecord<>("my-topic", [1]); producer.send(record);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an integer instead of a string.
Passing null which causes NullPointerException.
✗ Incorrect
The message value must be a String, so "Hello Kafka" is correct.
4fill in blank
hardFill both blanks to create a Kafka producer and send a message with a key.
Kafka
Properties props = new Properties(); props.put("bootstrap.servers", [1]); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, [2]); KafkaProducer<String, String> producer = new KafkaProducer<>(props);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using deserializer instead of serializer.
Using incomplete bootstrap server address.
✗ Incorrect
Use "localhost:9092" for bootstrap.servers and StringSerializer for key serializer.
5fill in blank
hardFill all three blanks to create a producer, send a keyed message, and close the producer.
Kafka
Properties props = new Properties(); props.put("bootstrap.servers", [1]); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, [2]); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, [3]); KafkaProducer<String, String> producer = new KafkaProducer<>(props); ProducerRecord<String, String> record = new ProducerRecord<>("my-topic", "key1", "value1"); producer.send(record); producer.close();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using deserializer instead of serializer for producer.
Forgetting to close the producer.
✗ Incorrect
Use "localhost:9092" for bootstrap.servers and StringSerializer for both key and value serializers.