To deserialize and process messages received by the Kafka consumer in Java, you need to specify a deserializer for the message key and value. The deserializer is responsible for converting the bytes received from Kafka to Java objects that can be processed by your application. Here’s an example of how to deserialize and process messages received by the Kafka consumer in Java:
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("group.id", "my_group"); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); Consumerconsumer = new KafkaConsumer<>(props); String topicName = "my_topic"; consumer.subscribe(Collections.singletonList(topicName)); while (true) { ConsumerRecords records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord record : records) { String key = record.key(); String value = record.value(); System.out.println("Received message: key=" + key + ", value=" + value); // Process the message here } } consumer.close();
In this example, we have defined the properties for the Kafka consumer, including the group ID and the key and value deserializers. We have specified the “StringDeserializer” class as the deserializer for both the key and value.
Next, we have created a Kafka consumer instance and subscribed to the specified topic using the subscribe method. We have used a while loop to continuously poll for messages from the Kafka cluster using the poll method.
When messages are received, the deserializers are used to convert the bytes to Java objects that can be processed by your application. In this example, we have simply printed the key and value of each message to the console, but you can process the message in any way that is appropriate for your application.
Overall, deserializing and processing messages received by the Kafka consumer in Java involves specifying a deserializer for the message key and value, polling for messages using the KafkaConsumer.poll method, and processing the messages using the deserialized key and value. By using deserializers, Java developers can easily integrate Kafka messaging capabilities into their applications and build powerful data processing pipelines that can handle large volumes of data with high efficiency and reliability.