❮ Previous Lecture
Next Lecture ❯
Welcome to Spring Boot + Apache Kafka Tutorial series. In this lecture, we will create a Kafka topic in our Spring boot application.
❮ Previous Lecture
Next Lecture ❯
Welcome to Spring Boot + Apache Kafka Tutorial series. In this lecture, we will create a Kafka topic in our Spring boot application.
Lecture - #6 - Create Kafka Topic
Transcript:
Hi. Welcome back. In this lecture, we'll see how to create a topic in a Kafka in our Spring Boot application. Well, in order to produce the messages and send them to the topic, first we need to have a topic in our Kafka cluster Right. So if we can see Kafka's architecture. We have a Kafka cluster within a Kafka cluster, We can create any number of topics that we want. Okay there's a no limitation basically, whatever the topics that you want, you can go ahead and create in a Kafka cluster. Well, in our case, we have only one Kafka broker service which is running locally, so it acts as a Kafka cluster. So within a Kafka cluster, we can create the topic and if we can see our topic can be again split into multiple partitions. All right. So we are not going to create the partitions in a topic. So whatever the Kafka provides, the default partitions that we are going to use in our Spring Boot application. All right. So in order to produce the messages by Producer and send them to the topic, we need to have a topic in our Kafka cluster right. Let's head over to IntelliJ IDEA and let's create a topic in a Kafka cluster using the Spring Boot application. Well let's head over to the IntelliJ idea here and let me minimize this and let me go to main package and I want to create a new package over here, let's call it as config. All the configuration classes that we are going to keep within this config package. So right-click on the config folder, new and choose Java class and let's give a class name as Kafka TopicConfig something like this. Okay. So basically we're going to create a spring bean to configure the Kafka topic. So let's go ahead and let's annotate this class with @Configuration annotation so that this class act as a Java Spring configuration class. So within this class, we're going to create a bean to create the Kafka topic. So just type public and then NewTopic. So make sure that you chose a NewTopic from the Kafka admin package. Let's go method name as javaguides Topic() and within this method, we want to create instance of topic class. So let's have a return statement over here and let's use the TopicBuilder class from Spring Framework. Well, here you can see org.springframework.kafka.config okay make sure that you chose this Topic Builder a class from this spring framework Kafka Library and then call the name() API and let's provide a name to the topic in our case let's say javaguides Okay, so you can also split this topic into partitions. In order to do that, you can use the partitions() method and you can pass like any number. Let's say you want to split this topic into ten partitions, then you can go ahead and pass 10 all right. So basically we're going to use a default partitions provided by a Kafka, so we're not going to create a partitions in this topic. So after that, just call the build() method in order to create the topic instance. Okay, that's it. Now let's make this method as a spring bean by using @Bean annotation. Okay, that is pretty much it. Now we have created a Kafka topic in a Kafka cluster using the Spring Boot application. Well, if we can run the Spring Boot application and you can see there are no errors which means whatever the code we have written to configure Kafka topic is working as expected. All right, I will see you in the next lecture.
Comments
Post a Comment
Leave Comment