To manage Apache Kafka in projects using Spring, you can use the KafkaAdmin class in the Spring for Apache Kafka module of Spring. This KafkaAdmin class is a wrapper of the AdminClient class in the kafka-client library! Using the object of the KafkaAdmin class, you can get information about all Topics in Apache Kafka, add, delete, and edit these Topics. How is it in detail? Let’s find out together in this tutorial!
First, I will create a new Maven project as an example:
Spring for Apache Kafka is declared as follows:
1 2 3 4 5 |
<dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>3.3.0</version> </dependency> |
You can initialize the bean of the KafkaAdmin object in the Spring container as follows:
1 2 3 4 5 6 7 |
@Bean public KafkaAdmin kafkaAdmin() { Map<String, Object> configs = new HashMap<>(); configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); return new KafkaAdmin(configs); } |
Create New Topic
With the bean declaration for the KafkaAdmin class above, now you only need to declare the bean of the NewTopic class, for example as follows:
1 2 3 4 |
@Bean public NewTopic topic1() { return new NewTopic("huongdanjava", 1, (short) 1); } |
then when running the application:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.huongdanjava.springapachekafka; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Application { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( AppConfiguration.class); } } |
you will see a new topic created in Apache Kafka, for example, mine as follows:
If you want to create a topic manually, you can use the createOrModifyTopics() method with the parameter being the NewTopic object of the KafkaAdmin class, for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.huongdanjava.springapachekafka; import org.apache.kafka.clients.admin.NewTopic; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.kafka.core.KafkaAdmin; public class Application { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( AppConfiguration.class); KafkaAdmin kafkaAdmin = context.getBean(KafkaAdmin.class); kafkaAdmin.createOrModifyTopics(new NewTopic("huongdanjava1", 1, (short) 1)); } } |
The result when running the example again, you will also see the topic “huongdanjava1” will be created.
View information of one or more Topics
You can use the describeTopics() method of the KafkaAdmin class to view information of one or more Topics, for example as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.huongdanjava.springapachekafka; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.kafka.core.KafkaAdmin; public class Application { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( AppConfiguration.class); KafkaAdmin kafkaAdmin = context.getBean(KafkaAdmin.class); System.out.println(kafkaAdmin.describeTopics("huongdanjava", "huongdanjava1")); } } |
The parameter of this describeTopics() method is a list of Topic names that you want to see information about!
My result when running this example is as follows: