[英]Create topic using java - kafka version >0.10.0.0
I am trying to write java api to create kafka topic . 我正在尝试编写Java api来创建kafka主题。 I have Kafka version 0.11.0.0 .
我有Kafka版本0.11.0.0。 I searched in stack overflow and tryied the same way.
我在堆栈溢出中进行搜索,并以相同的方式进行尝试。 But it is always giving me exception no matter topic exists or not.
但是,无论主题是否存在,它总是给我例外。
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)
I tried following code: 我尝试了以下代码:
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 dependencies -> 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>
Because you are using the new 0.11.0 version I suggest to use the new Admin Client API (following link for some documentation : https://kafka.apache.org/0110/javadoc/index.html?org/apache/kafka/clients/admin/AdminClient.html ). 因为您使用的是新的0.11.0版本,所以我建议使用新的Admin Client API(以下文档的链接: https ://kafka.apache.org/0110/javadoc/index.html?org/apache/kafka / client / admin / AdminClient.html )。
Using Zookeeper for doing such operations will be deprecated in the future pushing the usage of the new Admin Client API. 将来将不建议使用Zookeeper进行此类操作,以推动使用新的Admin Client API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.