不使用TopicExistsException安全地删除和重新创建Kafka主题。
创始人
2024-12-29 12:00:15
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方法来等待其完成。这样可以确保在进行后续操作之前,删除和创建操作已经成功完成。

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

相关内容

热门资讯

黑科技科技(wepoke智能a... 黑科技科技(wepoke智能ai)wepower使用说明书(wepOkE)素来是真的有挂(确实有挂)...
黑科技模拟器(wepoke智能... WePoker透视辅助工具核心要点解析‌,黑科技模拟器(wepoke智能ai)wepoke是机器发牌...
黑科技脚本(wepokeai代... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游戏中打牌都...
黑科技辅助挂(wepoke真的... 黑科技辅助挂(wepoke真的有挂)wepoke 软件(wEPOKE)固有真的有挂(有挂猫腻)-哔哩...
黑科技辅助挂(wepoke黑科... 黑科技辅助挂(wepoke黑科技)wepoke软件透明挂演示(wEpOke)一直真的有挂(真的有挂)...
黑科技app(wepoke真的... 此外,数据分析德州()辅助神器app还具备辅助透视行为开挂功能,通过对客户透明挂的深入研究,你可以了...
黑科技智能ai(wepoke辅... 黑科技智能ai(wepoke辅助插件)wepoke软件透明(WEPOke)先前是有挂(果真有挂)-哔...
黑科技模拟器(wepoke透明... 黑科技模拟器(wepoke透明黑科技)wepoke德州扑克用ai代打(WEPOKE)最初存在有挂(有...
黑科技插件(wepoke辅助插... 黑科技插件(wepoke辅助插件)wepoke软件透明(WEPOke)从来是真的有挂(有挂解惑)-哔...
黑科技美元局(wepoke智能... 黑科技美元局(wepoke智能ai)wepoke辅助真的假的(wepoKE)原先真的有挂(竟然有挂)...