Manage Apache Kafka using KafkaAdmin class of Spring for Apache Kafka

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:

You can initialize the bean of the KafkaAdmin object in the Spring container as follows:

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:

then when running the application:

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:

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:

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:

Add Comment