Use the Kafka client (kafka-clients) Java API to operate Kafka Topic

Records : 460

Scenario : Integrate the Kafka client kafka-clients-3.0.0 in the Spring Boot microservice to create and delete Kafka Topic.

Versions : JDK 1.8, Spring Boot 2.6.3, kafka_2.12-2.8.0, kafka-clients-3.0.0.

Kafka installation : https://blog.csdn.net/zhangbeizhen18/article/details/129071395

1. Configure Kafka information in microservices

1.1 Add dependencies in pom.xml

pom.xml file:

<dependency>
  <groupId>org.apache.kafka</groupId>
  <artifactId>kafka-clients</artifactId>
  <version>3.0.0</version>
</dependency>

Analysis: Use native kafka-clients, version: 3.0.0. Operate Kafka's producers, consumers, and topics.

2. Use AdminClient to create Kafka Topic

AdminClient全称:org.apache.kafka.clients.admin.AdminClient

(1) sample code

@RestController
@RequestMapping("/hub/example/topic")
@Slf4j
public class UseKafkaTopicController {
  private String topicName = "hub-topic-city-02";
  @GetMapping("/f01_1")
  public Object f01_1() {
      try {
          //1.获取Kafka配置信息
          Map<String, Object> configs = new HashMap<>();
          configs.put("bootstrap.servers", "192.168.19.203:29001");
          //2.创建客户端AdminClient
          AdminClient adminClient = KafkaAdminClient.create(configs);
          //3.获取Topic清单
          Set<String> topicSet = adminClient.listTopics().names().get();
          log.info("在Kafka已建Topic数量: {} ,清单:", topicSet.size());
          topicSet.forEach(System.out::println);
          //4.创建Topic
          if (!topicSet.contains(topicName)) {
              log.info("新建Topic: {}", topicName);
              // Topic名称,分区Partition数目,复制因子(replication Factor)
              NewTopic newTopic = new NewTopic(topicName, 1, (short) 1);
              Collection<NewTopic> newTopics = Lists.newArrayList(newTopic);
              adminClient.createTopics(newTopics);
          }
      } catch (Exception e) {
          log.info("创建Topic异常.");
          e.printStackTrace();
      }
      return "创建成功";
  }
}

(2) Analysis code

To operate the topic of Kafka, you need to create an AdminClient first, and use the API of AdminClient to create a topic.

To create a topic, you only need to specify the topic name, the number of partitions, and the replication factor.

3. Use AdminClient to delete Kafka Topic

AdminClient全称:org.apache.kafka.clients.admin.AdminClient

(1) sample code

@RestController
@RequestMapping("/hub/example/topic")
@Slf4j
public class UseKafkaTopicController {
  private String topicName = "hub-topic-city-02";
  @GetMapping("/f01_2")
  public Object f01_2() {
      try {
          //1.获取Kafka配置信息
          Map<String, Object> configs = new HashMap<>();
          configs.put("bootstrap.servers", "192.168.19.203:29001");
          //2.创建客户端AdminClient
          AdminClient adminClient = KafkaAdminClient.create(configs);
          //3.获取Topic清单
          Set<String> topicSet = adminClient.listTopics().names().get();
          //4.删除Topic
          if (topicSet.contains(topicName)) {
              log.info("删除Topic: {}", topicName);
              Collection<String> topics = Lists.newArrayList(topicName);
              DeleteTopicsResult deleteTopicsResult = adminClient.deleteTopics(topics);
              deleteTopicsResult.all().get();
          }
      } catch (Exception e) {
          log.info("删除Topic异常.");
          e.printStackTrace();
      }
      return "删除成功";
  }
}

(2) Analysis code

To operate the topic of Kafka, you need to create an AdminClient first, and use the API of AdminClient to delete the topic.

To create a topic, you only need to specify the topic name.

4. Test

Create request RUL: http://127.0.0.1:18209/hub-209-kafka/hub/example/topic/f01_1

Delete request RUL: http://127.0.0.1:18209/hub-209-kafka/hub/example/topic/f01_2

Above, thanks.

June 17, 2023

Guess you like

Origin blog.csdn.net/zhangbeizhen18/article/details/131265519