0
0
Kafkadevops~10 mins

Event streaming concept 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 causes errors.
Forgetting to set the value serializer.
2fill in blank
medium

Complete the code to send a message to a Kafka 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"value1"
Bnull
C"key1"
D"my-key"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the message value as the key.
Passing null when a key is expected.
3fill in blank
hard

Fix the error in the consumer configuration to read string messages.

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", [1]);
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
Drag options to blanks, or click blank then click option'
A"org.apache.kafka.common.serialization.StringDeserializer"
B"org.apache.kafka.common.serialization.IntegerDeserializer"
C"org.apache.kafka.common.serialization.ByteArrayDeserializer"
D"org.apache.kafka.common.serialization.LongDeserializer"
Attempts:
3 left
💡 Hint
Common Mistakes
Using IntegerDeserializer for string messages causes errors.
Not setting the value deserializer.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters events with value length greater than 5.

Kafka
Map<String, String> filteredEvents = events.entrySet().stream()
    .filter(e -> e.getValue().[1]() > 5)
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Drag options to blanks, or click blank then click option'
Alength
Bcount
Csize
Dlength()
Attempts:
3 left
💡 Hint
Common Mistakes
Using length without parentheses causes errors.
Using size or count which are not string methods.
5fill in blank
hard

Fill all three blanks to create a Kafka consumer poll loop that processes records.

Kafka
while (true) {
    ConsumerRecords<String, String> records = consumer.[1](Duration.ofMillis(100));
    for (ConsumerRecord<String, String> record : records) {
        System.out.println("Received message: " + record.[2]() + ": " + record.[3]());
    }
}
Drag options to blanks, or click blank then click option'
Apoll
Bkey
Cvalue
Dsubscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Using subscribe instead of poll to fetch messages.
Accessing record fields directly instead of using methods.