Consider this Kafka producer code in Java that sends a message to a topic named test-topic. What will be printed on the console?
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Producer<String, String> producer = new KafkaProducer<>(props); ProducerRecord<String, String> record = new ProducerRecord<>("test-topic", "key1", "Hello Kafka"); producer.send(record, (metadata, exception) -> { if (exception == null) { System.out.println("Sent to partition " + metadata.partition()); } else { System.out.println("Error sending message"); } }); producer.close();
Assume the topic test-topic has only one partition.
The message is sent successfully to the only partition (partition 0) of the topic, so the callback prints "Sent to partition 0".
In Kafka, what is the main purpose of a consumer group?
Think about how Kafka balances workload among consumers.
A consumer group allows Kafka to assign partitions to consumers so they can process messages in parallel without duplication.
Look at this Kafka consumer code snippet. It never prints any consumed messages. What is the cause?
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("group.id", "group1"); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); consumer.subscribe(Collections.singletonList("my-topic")); while (true) { ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord<String, String> record : records) { System.out.println("Received: " + record.value()); } }
Check the poll method usage and its parameters.
The poll method requires a timeout duration. If the argument is incorrect or missing, the consumer will not fetch messages.
Which of these Kafka Streams Java code snippets correctly creates a stream from topic input-topic?
Check for correct Java syntax and string literals.
Option A uses correct Java syntax with semicolons and double quotes for string literals. Others have syntax errors.
A Kafka topic has 6 partitions. There are 3 consumers in the same consumer group subscribed to this topic. How many partitions will each consumer get assigned?
Partitions are divided evenly among consumers in a group.
Kafka assigns partitions evenly among consumers in a group. 6 partitions divided by 3 consumers means each gets 2 partitions.