❮ Previous Lecture
Next Lecture ❯
Welcome to Spring Boot + Apache Kafka Tutorial series. In this lecture, we will create Kafka Consumer to Consume JSON Messages from Kafka's topic.
Welcome to Spring Boot + Apache Kafka Tutorial series. In this lecture, we will create Kafka Consumer to Consume JSON Messages from Kafka's topic.
Lecture - #13 - Create Kafka Consumer to Consume JSON Message
Transcript:
Hi. Welcome back. In this lecture, we'll create a KafkaConsumer to consume a JSON message. Well, let's go to IntelliJ idea and go to kafka package. Right-click on it new and then choose Java class and let's give class name is JsonKafkaConsumer and hit enter and let's annotate this class with @Service annotation and let's have a Logger object here private static final and then logger from slf4j and then LOGGER LogerFactory and getLogger API and then pass class name as JsonKafkaConsumer.class. Now, we have LOGGER instance. Next, let's go ahead and let's create a method over here. Well, let's say public void and let's give method name as consume and in this case we need to pass the user object as a method parameter okay, perfect. In order to subscribe this method to the topic, we have to use annotation that is @KafkaListener annotation. So make sure that you choose @KafkaListener annotation from org.springframework.kafka.annotation package and then we need to pass the topic name. So in this case we want to use topic that is javaguides_json and along with that we need to also pass the group-id. So we have a group-id myGroup right perfect. So what we can do is we can simply log this user object. So in order to do that, let's say logger.info this should be a logger object and then call info() method and then let's say string and then call format method and then pass message something like JSON message received and then placeholder and then pass user.toString() perfect. Now we have created KafkaConsumer to consume the JSON message all right. So basically this, you know, JsonSerializer which is provided by Spring Kafka, it will basically convert Json into user object. And that user object we have simply printed using this log statement right. Now, what we can do is we can run the Spring Boot application and will verify how this KafkaConsumer will consume JSON and it will converting from JSON into user object. So let me run the Spring boot project and there we go. Spring Boot application is up and running and go to the console and you can able to see JSON message received. It means the KafkaConsumer that we have written to consume JSON message is working as expected right. Now, let's go ahead and let's call REST API to see how the JSON message send to the Kafka topic and how it should be consumed here. Let's change the JSON object, let's say first name is something like Tom and then last name let's say cruise and hit send button and go to the console here and let's see the log statements. So just go to the bottom here, scroll down, scroll down and go to the bottom. and we got JSON message received firstName Tom, last name Cruise okay. So let's see one more message let's say John, last name Cena and hit send button and let's go to terminal and there we go in a terminal you can see there are three JSON messages in the javaguides_json topic and you can also see that message printed in the console there we go. User first and last name John Cena all right. So this is how basically we use Spring of Kafka provided JsonSerializer and JsonDeserializer classes to send and receive java object as JSON to and from Apache Kafka. Okay, so the important configuration is like this in Application.Properties file. We need to provide JsonDeserializer class from the Spring Kafka library as a value-deserializer okay. And JsonSerializer class from spring Kafka library to value-serializer property. Well, I hope you understood how to send and receive java object as JSON byte array to and from Apache Kafka. All right, I will see you in the next lecture.
❮ Previous Lecture
Next Lecture ❯
Comments
Post a Comment
Leave Comment