[英]How to get detailed error messages from Java Kafka AdminClient when creating topics
I need to create some Kafka topics from a Java program.我需要从 Java 程序创建一些 Kafka 主题。
I am using the AdminClient
class method createTopics
to which I pass an array of NewTopic
.我正在使用AdminClient
class 方法createTopics
,我将NewTopic
数组传递给该方法。
Let's say I want to create 3 topics: T1 , T2 and T3 .假设我要创建 3 个主题: T1 、 T2和T3 。 Let's say that T1 and T2 already exist, while T3 doesn't.假设T1和T2已经存在,而T3不存在。
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:我得到的结果如下:
Is there a possibility to get a message that says that also T2 was not created because already existing?是否有可能收到一条消息说T2也不是因为已经存在而创建的?
Currently(Kafka 2.3), it is impossible for Kafka to display a single message explicitly indicating all the existing topics.目前(Kafka 2.3),Kafka 不可能显示一条明确指示所有现有主题的消息。 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?那是你要的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.