How to get detailed error messages from Java Kafka AdminClient when creating topics

Viewed 216

I need to create some Kafka topics from a Java program.

I am using the AdminClient class method createTopics to which I pass an array of NewTopic.

Let's say I want to create 3 topics: T1, T2 and T3. Let's say that T1 and T2 already exist, while T3 doesn't.

My code is

NewTopic t1 = new NewTopic("T1", 3, (short)3);
NewTopic t2 = new NewTopic("T2", 3, (short)3);
NewTopic t2 = new NewTopic("T3", 3, (short)3);

List<NewTopic> newTopics = new ArrayList<NewTopic>();
newTopics.add("T1");
newTopics.add("T2");
newTopics.add("T3");
Properties properties = new Properties();
properties.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaConnectString);
try (final AdminClient adminClient = AdminClient.create(properties)) {
   final CreateTopicsResult createTopicsResult = adminClient.createTopics(newTopics);
   createTopicsResult.all().get();
} catch (Exception e) {
   logger.info(e.toString());
}

The result I get is the following:

  • on the console a message that T1 already exists and no mention of T2
  • in Kafka T1, T2 and T3 present, meaning that T3 was actually created

Is there a possibility to get a message that says that also T2 was not created because already existing?

1 Answers

Currently(Kafka 2.3), it is impossible for Kafka to display a single message explicitly indicating all the existing topics. However, you could try coding like this to get multiple error messages:

 ......
 CreateTopicsResult result = client.createTopics(Arrays.asList(t1, t2, t3));
 result.values().values().stream().forEach(f -> {
        try {
            f.get();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
 });

Is that what you want?

Related