使用javaAPI创建topic、分区数、副本数、数据保留时间

文章介绍了如何在Java中使用KafkaAPI进行基本信息查询(如topic存在性)、集群节点数获取以及创建新主题,包括设置分区数、副本数和数据保留时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下:

类一:
public class BasicInfo {
    public TopicDescription topicExistence(String ipPort, String topicName){
        BasicInfo basicInfo = new BasicInfo();
        AdminClient adminClient = basicInfo.adminClient(ipPort);
        DescribeTopicsResult topicsResult = adminClient.describeTopics(Collections.singleton(topicName));
        TopicDescription topicDescription;
        try {
            topicDescription = topicsResult.values().get(topicName).get();
        }catch (InterruptedException | ExecutionException e){
            topicDescription=null;
        }
        return topicDescription;
    }

	public AdminClient adminClient(String ipPort) {
        AdminClient adminClient;
        Properties props = new Properties();
        props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,ipPort);
        //超时限制10秒
        props.put(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG,10000);
        //设置重试次数
        props.put(AdminClientConfig.RETRIES_CONFIG,3);
        //设置重试间隔时间
        props.put(AdminClientConfig.RETRY_BACKOFF_MS_CONFIG,1000);
        adminClient = AdminClient.create(props);
        return adminClient;
    }
}

类二:
public class TopicApply {
    public void applyTopic(String ipPort,String topicName,int numPartitions,short replicationFactor,String retentionMS) {

        BasicInfo basicInfo = new BasicInfo();
        AdminClient adminClient = basicInfo.adminClient(ipPort);

        short numBroker=0;
        try{
            numBroker = (short) adminClient.describeCluster().nodes().get().size();
        }catch (InterruptedException|ExecutionException e){
            System.out.println("获取集群broker个数失败!");
        }

        TopicDescription topicDescription = basicInfo.topicExistence(ipPort, topicName);

        if (topicDescription != null) {
            System.out.println("topic " + topicName + " Existed");
        } else
        if (numPartitions < 3) {
            System.out.println("分区数必须 >=3!");
        } else if (replicationFactor < 1) {
            System.out.println("副本数必须 >=1!");
        } else if (replicationFactor > numBroker){
            System.out.println("副本数不能大于集群broker数量");
        } else {
            NewTopic newTopic = new NewTopic(topicName, numPartitions, replicationFactor); //创建主题的配置
            HashMap<String, String> retentionMs = new HashMap<>();
            //数据保留时间
            retentionMs.put(TopicConfig.RETENTION_MS_CONFIG,retentionMS);
            //设置清理策略为删除retentionMs.put(TopicConfig.CLEANUP_POLICY_CONFIG,TopicConfig.CLEANUP_POLICY_DELETE);
            newTopic.configs(retentionMs);
            adminClient.createTopics(Collections.singleton(newTopic));
            //等待主题创建完成
            try {
                adminClient.listTopics().names().thenApply(topicNames -> {
                    if (topicNames.contains(topicName)) {
                        System.out.println("topic " + topicName + " created successfully");
                    } else {
                        System.out.println("Failed to create topic " + topicName);
                    }
                    return null;
                }).get();//等待任务完成
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String ipPort="IP:port";
        String topic = scanner.next();
        int numPartitions=3;
        short replicationFactor=1;
        String retentionMS="7200000";
        TopicApply topicApply = new TopicApply();
        topicApply.applyTopic(ipPort, topic, numPartitions, replicationFactor, retentionMS);
        scanner.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_49457994

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值