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?