我正在尝试编写java api来创建kafka主题。我有Kafka版本0.11.0.0。我搜索堆栈溢出并尝试相同的方式。但无论主题是否存在,它总是给我例外。
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/kafka/common/errors/TopicExistsException
at kafka.admin.AdminUtils.createTopic(AdminUtils.scala)
at kafkaStream.Processor.CreateTopic.main(CreateTopic.java:65)
Caused by: java.lang.ClassNotFoundException:
org.apache.kafka.common.errors.TopicExistsException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
我尝试了以下代码:
String topicName = "t5";
String zookeeperHosts = "XXXX:2181,XXXX:2181";
int sessionTimeOutInMs = 15 * 1000;
int connectionTimeOutInMs = 50 * 1000;
Properties topicConfig = new Properties();
zkClient = new ZkClient("XXXX:2181,XXXX:2181",
sessionTimeOutInMs,
connectionTimeOutInMs,
ZKStringSerializer$.MODULE$);
zkUtils = new ZkUtils(zkClient, new
ZkConnection(zookeeperHosts), false);
ZkUtils.apply(
"XXXX:2181,XXXX:2181",
sessionTimeOutInMs,
connectionTimeOutInMs,
false);
// AdminUtils.createTopic(zkUtils, topicName, numPartitions, 1,
topicConfig, RackAwareMode.Enforced$.MODULE$);
AdminUtils.createTopic(zkUtils, topicName, 2, 1, new
Properties(), RackAwareMode.Enforced$.MODULE$);
maven依赖 - >
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.11</artifactId>
<version>0.11.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.11.0.0</version>
</dependency>
答案 0 :(得分:1)
由于您使用的是新的0.11.0版本,我建议您使用新的管理客户端API(以下链接获取一些文档:https://kafka.apache.org/0110/javadoc/index.html?org/apache/kafka/clients/admin/AdminClient.html)。
将来不推荐使用Zookeeper执行此类操作,推动使用新的Admin Client API。