我正在嘗試編寫java api來創建kafka主題。我有Kafka版本0.11.0.0。我搜索堆棧溢出並嘗試了相同的方式。但無論話題是否存在,它總是讓我異常。使用java創建主題 - kafka版本> 0.10.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>
你是否添加了依賴api(kafka-特定版本的客戶端)jar到類路徑? –
是的,我已經添加 – Megha