Megha
Megha

Reputation: 198

Create topic using java - kafka version >0.10.0.0

I am trying to write java api to create kafka topic . I have Kafka version 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 ->

       <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>

Upvotes: 0

Views: 1367

Answers (1)

ppatierno
ppatierno

Reputation: 10075

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).

Using Zookeeper for doing such operations will be deprecated in the future pushing the usage of the new Admin Client API.

Upvotes: 1

Related Questions