使用AdminClient的deleteTopics删除主题却失败

the prayer 发表于: 2021-05-20   最后更新时间: 2021-05-25 00:24:28   1,328 游览

使用AdminClient的api调用AdminClient的实例对象的deleteTopics方法删除一个主题,却失败了。有去看官网的API,api也没什么解释。我上网搜了下别人怎么用的,跟我的一样,也是一句调用就OK了,但我这失败了,也不知道为什么。

删除代码如下:

 public static void main(String[] args) {
//        createTopic("localhost:9092","testTopic2");
        deleteAndShowTopics("bar");
}

//主题删除
public static void deleteAndShowTopics(String topicName){
    Properties pros = configPros("localhost:9092");
    try(AdminClient client = AdminClient.create(pros)){
        client.deleteTopics(Arrays.asList(topicName));  //删除主题

        //下面是遍历所有的主题
        ListTopicsResult listResult = client.listTopics();
        try {
            listResult.listings().get().forEach(topic -> {
                System.out.println(topic);
            });
        } catch (InterruptedException | ExecutionException e) {
            throw new IllegalStateException(e);
        }
    }
}
//pros对象
public static Properties configPros(String bootStrapServers){
    Properties properties = new Properties();
    properties.put("bootstrap.servers", bootStrapServers);
    properties.put("connections.max.idle.ms", 10000);   //AdminClient最长的闲置时间,超出这个时间就会自动关闭这个客户端和broker之间的连接
    properties.put("request.timeout.ms", 5000); //等待响应的最长时间
    return properties;
}

日志打印在下面

15:49:09.959 [kafka-admin-client-thread | adminclient-1] DEBUG org.apache.kafka.clients.admin.KafkaAdminClient - [AdminClient clientId=adminclient-1] Sending DeleteTopicsRequestData(topics=[], topicNames=[bar], timeoutMs=5000) to windows10.microdone.cn:9092 (id: 0 rack: null). correlationId=5
15:49:09.960 [kafka-admin-client-thread | adminclient-1] DEBUG org.apache.kafka.clients.NetworkClient - [AdminClient clientId=adminclient-1] Sending DELETE_TOPICS request with header RequestHeader(apiKey=DELETE_TOPICS, apiVersion=1, clientId=adminclient-1, correlationId=5) and timeout 5000 to node 0: DeleteTopicsRequestData(topics=[], topicNames=[bar], timeoutMs=5000) 这里是请求超时的意思吗?
15:49:15.092 [kafka-admin-client-thread | adminclient-1] DEBUG org.apache.kafka.clients.NetworkClient - [AdminClient clientId=adminclient-1] Received DELETE_TOPICS response from node 0 for request with header RequestHeader(apiKey=DELETE_TOPICS, apiVersion=1, clientId=adminclient-1, correlationId=5): DeleteTopicsResponseData(throttleTimeMs=0, responses=[DeletableTopicResult(name='bar', topicId=AAAAAAAAAAAAAAAAAAAAAA, errorCode=7, errorMessage=null)])
发表于 2021-05-20
添加评论
  • 这里显示errorCode=7是time out了,不妨把request.timeout.ms调高一点

  • topic被订阅时是删除不了的,检查一下是否被订阅了

你的答案

查看kafka相关的其他问题或提一个您自己的问题