0
0
Kafkadevops~10 mins

Java producer client 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 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'
A"localhost"
B"9092"
C"localhost:9092"
D"kafka:9092"
Attempts:
3 left
💡 Hint
Common Mistakes
Using only the host name without port.
Using only the port number.
Using an incorrect format without colon.
2fill in blank
medium

Complete 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'
A"org.apache.kafka.common.serialization.StringSerializer"
B"org.apache.kafka.common.serialization.IntegerSerializer"
C"org.apache.kafka.common.serialization.StringDeserializer"
D"org.apache.kafka.common.serialization.ByteArraySerializer"
Attempts:
3 left
💡 Hint
Common Mistakes
Using deserializer instead of serializer.
Using serializer for wrong data type.
3fill in blank
hard

Fix 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'
A12345
Bnew String()
Cnull
D"Hello Kafka"
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an integer instead of a string.
Passing null which causes NullPointerException.
4fill in blank
hard

Fill 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'
A"localhost:9092"
B"org.apache.kafka.common.serialization.StringDeserializer"
C"org.apache.kafka.common.serialization.StringSerializer"
D"localhost"
Attempts:
3 left
💡 Hint
Common Mistakes
Using deserializer instead of serializer.
Using incomplete bootstrap server address.
5fill in blank
hard

Fill 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'
A"localhost:9092"
B"org.apache.kafka.common.serialization.StringSerializer"
D"org.apache.kafka.common.serialization.StringDeserializer"
Attempts:
3 left
💡 Hint
Common Mistakes
Using deserializer instead of serializer for producer.
Forgetting to close the producer.