2.4 Kafka Admin API之删除主题

松霖
2023-12-01

1.代码示例

public class AdminSample {
    private static final String topicName = "steven";

    private static final int partitionNumber = 1;

    private static final Short replicationFactor = 1;

    /**
     * 创建AdminClient
     */
    public static AdminClient adminClient() {
        Properties properties = new Properties();
        properties.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
        AdminClient adminClient = AdminClient.create(properties);
        return adminClient;
    }

    /**
     * 删除topic
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        AdminClient adminClient = AdminSample.adminClient();
        DeleteTopicsResult deleteTopicsResult = adminClient.deleteTopics(Arrays.asList(topicName));
        //删除是一个异步过程,但是如果主线程退出了,创建还没执行完就会失败,所以下面的语句是防止主线程退出
        deleteTopicsResult.all().get();
    }
}

代码执行完毕后,通过2.3的代码查询topic列表信息,可以发现名为steven的topic被删除。

 类似资料: