简体   繁体   中英

delete topic not working for kafka java client 2.1.0

I want to delete a kafka topic programatically and struggling a lot to make it work. Am using below maven dependency.

  <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>2.1.0</version>
    </dependency>

Am using KafkaAdminClient. Here is my code,

AdminClient admin = KafkaAdminClient.create(getProperties(configuration.getKafkaConnectionString())));

private static Properties getProperties(String kafkaConnectionString) {
    Properties config = new Properties();
    config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaConnectionString);
    return config;
}

Code to delete topic:

public void deleteTopic(final String... topicNames) {
    admin.deleteTopics(Arrays.asList(topicNames));
    log.info("Topics '{}' deleted.", topicNames);
}

I have delete.topic.enable=true property in server.properties for kafka. There are no exception. Not sure why its not working. Any help is appreciated.

  • Make sure producer is not using that topic.

  • Since deleteTopics() method returns a future , wait for future task to complete ( In case if you are using on-demand tasks, Since main thread will not wait for future to complete if we don't do it manually.).

     public void deleteKafkaTopics(List<String> kafkaTopics) { DeleteTopicsResult deleteTopicsResult = kafkaAdminClient.deleteTopics(kafkaTopics); while (!deleteTopicsResult.all().isDone()) { // Wait for future task to complete } }

Note: If you are using windows machine you may face AccessDeniedException. Already ticket open in kafka issue board (Iam using kafka 2.1) for that fix.So I prefer to use Linux machines which is working fine with Kafka. Also don't forget to set delete.topic.enable = true in config/server.properties of Kafka brokers . For newer versions of kafka it is enabled by default.

public static void delete(String name) { AdminClient client = AdminClient.create(properties()); DeleteTopicsResult deleteTopicsResult = client.deleteTopics(Collections.singletonList(name)); while (!deleteTopicsResult.all().isDone()) { } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM