不使用TopicExistsException安全地删除和重新创建Kafka主题。
创始人
2024-12-29 12:00:27
0

要安全地删除和重新创建Kafka主题,我们可以使用AdminClient API来检查主题是否存在,并在删除和创建之前执行必要的操作。下面是一个示例代码:

import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.DeleteTopicsResult;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.common.KafkaFuture;
import org.apache.kafka.common.errors.TopicExistsException;

import java.util.Collections;
import java.util.Properties;
import java.util.concurrent.ExecutionException;

public class KafkaTopicManagement {

    private static final String BOOTSTRAP_SERVERS = "localhost:9092";
    private static final String TOPIC_NAME = "my-topic";
    private static final int NUM_PARTITIONS = 3;
    private static final short REPLICATION_FACTOR = 1;

    public static void main(String[] args) {
        Properties config = new Properties();
        config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVERS);

        AdminClient adminClient = AdminClient.create(config);

        // Check if topic exists
        boolean topicExists = doesTopicExist(adminClient, TOPIC_NAME);
        if (topicExists) {
            // Delete topic
            deleteTopic(adminClient, TOPIC_NAME);
        }

        // Create topic
        createTopic(adminClient, TOPIC_NAME, NUM_PARTITIONS, REPLICATION_FACTOR);

        adminClient.close();
    }

    private static boolean doesTopicExist(AdminClient adminClient, String topicName) {
        KafkaFuture topicExistsFuture = adminClient.listTopics().names().thenApply(names -> names.contains(topicName));
        try {
            return topicExistsFuture.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        return false;
    }

    private static void deleteTopic(AdminClient adminClient, String topicName) {
        DeleteTopicsResult deleteTopicsResult = adminClient.deleteTopics(Collections.singleton(topicName));
        try {
            deleteTopicsResult.all().get();
            System.out.println("Topic deleted successfully");
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

    private static void createTopic(AdminClient adminClient, String topicName, int numPartitions, short replicationFactor) {
        NewTopic newTopic = new NewTopic(topicName, numPartitions, replicationFactor);
        adminClient.createTopics(Collections.singleton(newTopic));
        System.out.println("Topic created successfully");
    }
}

在上面的示例中,我们首先创建一个AdminClient实例来进行管理操作。然后,我们使用doesTopicExist方法来检查主题是否存在。如果主题存在,我们使用deleteTopic方法删除它。最后,我们使用createTopic方法创建一个新的主题。

请注意,上述代码中的删除和创建操作都是异步的,并且在实际执行操作之前调用了get方法来等待其完成。这样可以确保在进行后续操作之前,删除和创建操作已经成功完成。

另外,我们还捕获了可能的异常并打印了错误消息。在实际应用中,您可以根据需要进行适当的错误处理。

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:哪种网络模式具有... 使用AWS ECS中的awsvpc网络模式来获得最佳性能。awsvpc网络模式允许ECS任务直接在V...