关于springboot创建kafkaTopic

工具类提供,方法名见名知意。使用kafka admin

java 复制代码
import org.apache.kafka.clients.admin.*;
import org.apache.kafka.common.KafkaFuture;

import java.util.*;
import java.util.concurrent.ExecutionException;



import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.ListTopicsOptions;
import org.apache.kafka.clients.admin.TopicDescription;
import org.apache.kafka.common.KafkaFuture;
import org.apache.kafka.common.TopicPartitionInfo;

import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutionException;
/**
 * @author: zhoumo
 * @data: 2024/6/24 16:37
 * @descriptions:
 */
public class KafkaTopicInfo {

    final static String ip="127.0.0.1:9090";


    public static void main(String[] args) {
        getListDetail();
    }


    
    public static void createTopic(String topicName) throws ExecutionException, InterruptedException {
        // Kafka 配置
        Properties props = new Properties();
        // Kafka 服务器地址和端口
        props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, ip);

        // 创建 AdminClient 实例
        try (AdminClient adminClient = AdminClient.create(props)) {
            // 创建一个新的主题
            // 指定分区数量
            // 指定复制因子
            int numPartitions = 2;
            short replicationFactor = 1;

            NewTopic newTopic = new NewTopic(topicName, numPartitions, replicationFactor);

            // 创建主题
            adminClient.createTopics(Collections.singletonList(newTopic)).all().get();
            System.out.println("Topic created successfully: " + topicName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void deleteTopic(String topicName) {
        // Kafka 配置
        Properties props = new Properties();
        props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, ip);

        try (AdminClient adminClient = AdminClient.create(props)) {
            // 要删除的主题名称
         //   String topicName = "myTopic";

            // 删除主题
            DeleteTopicsResult deleteResult = adminClient.deleteTopics(Collections.singletonList(topicName));
            deleteResult.all().get();

            System.out.println("Topic deleted successfully: " + topicName);

        } catch (ExecutionException | InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void getList() {
        // Kafka 配置
        Properties props = new Properties();
        props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, ip);

        try (AdminClient adminClient = AdminClient.create(props)) {
            // 列出所有主题
            ListTopicsOptions options = new ListTopicsOptions();
            // 是否包括内部主题,默认为 false
            options.listInternal(true);
            ListTopicsResult topicsResult = adminClient.listTopics(options);
            Set<String> topics = topicsResult.names().get();

            System.out.println("Existing topics:");
            for (String topic : topics) {
                System.out.println(topic);
            }

        } catch (ExecutionException | InterruptedException e) {
            e.printStackTrace();
        }
    }



    public static void getListDetail() {
        // Kafka 配置
        Properties props = new Properties();
        props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, ip);

        try (AdminClient adminClient = AdminClient.create(props)) {
            // 列出所有主题
            ListTopicsOptions options = new ListTopicsOptions();
            // 是否包括内部主题,默认为 false
            options.listInternal(true);
            KafkaFuture<Set<String>> topics = adminClient.listTopics(options).names();

            System.out.println("Existing topics:");
            for (String topic : topics.get()) {
                System.out.println(topic);
                // 获取主题的详细信息(包括分区情况)
                /*
                回退jdk1.8 版本
                KafkaFuture<TopicDescription> topicDescription = adminClient.describeTopics(Set.of(topic)).values().get(topic);
                printTopicDetails(topicDescription.get());
                */
                Set<String> topicSet = new HashSet<>();
                topicSet.add(topic);
                KafkaFuture<TopicDescription> topicDescriptionFuture = adminClient.describeTopics(topicSet).values().get(topic);
                TopicDescription topicDescription = topicDescriptionFuture.get();
                printTopicDetails(topicDescription);
            }

        } catch (ExecutionException | InterruptedException e) {
            e.printStackTrace();
        }
    }
    private static void printTopicDetails(TopicDescription topicDescription) {
        System.out.println("Topic: " + topicDescription.name());
        System.out.println("Partitions:");

        for (TopicPartitionInfo partition : topicDescription.partitions()) {
            System.out.printf("  Partition %d, Leader: %d, Replicas: %s, Isrs: %s%n",
                    partition.partition(),
                    partition.leader().id(),
                    partition.replicas(),
                    partition.isr());
        }
        System.out.println();
    }
}
相关推荐
waicsdn_haha几秒前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
飞的肖8 分钟前
前端使用 Element Plus架构vue3.0实现图片拖拉拽,后等比压缩,上传到Spring Boot后端
前端·spring boot·架构
Q_192849990611 分钟前
基于Spring Boot的摄影器材租赁回收系统
java·spring boot·后端
良许Linux15 分钟前
0.96寸OLED显示屏详解
linux·服务器·后端·互联网
求知若饥27 分钟前
NestJS 项目实战-权限管理系统开发(六)
后端·node.js·nestjs
gb42152871 小时前
springboot中Jackson库和jsonpath库的区别和联系。
java·spring boot·后端
程序猿进阶1 小时前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot
颜淡慕潇2 小时前
【K8S问题系列 |19 】如何解决 Pod 无法挂载 PVC问题
后端·云原生·容器·kubernetes
向前看-9 小时前
验证码机制
前端·后端