user1708054
user1708054

Reputation: 191

Create kafka topic in Java

I am trying to create kafka topics on the fly for that I have created a method which takes input as topic name and should create a topic.

I am using kafka 2.12 and Java 1.8 below is what I tried....

private static void CreateKafkaTopic(String topicName) {
    ZkClient zkClient = null;
    ZkUtils zkUtils = null;
    try {
        String zookeeperConnect = "localhost:2181";
        int sessionTimeOutInMs = 15 * 1000; // 15 secs
        int connectionTimeOutInMs = 10 * 1000; // 10 secs
        zkClient = new ZkClient(zookeeperConnect, sessionTimeOutInMs, connectionTimeOutInMs, ZKStringSerializer$.MODULE$);
        boolean isSecureKafkaCluster = false;
        zkUtils = new ZkUtils(zkClient, new  ZkConnection(zookeeperConnect), isSecureKafkaCluster);
        Properties topicConfig = new Properties();
        AdminUtils.createTopic(zkUtils, topicName, 1, 1, topicConfig,RackAwareMode.Disabled$.MODULE$);
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (zkClient != null) {
            zkClient.close();
        }
    }
}

but when I try this I am getting error as below

Exception in thread "main" java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Product;)V at kafka.admin.RackAwareMode$Disabled$.(RackAwareMode.scala:27) at kafka.admin.RackAwareMode$Disabled$.(RackAwareMode.scala) at com.OTMProducer.CreateKafkaTopic(OTMProducer.java:243)

Kindly suggest me what is the reason for this. I referred some already available answers like How To create a kafka topic from java for KAFKA-2.1.1-1.2.1.1?

Upvotes: 0

Views: 2166

Answers (1)

Mickael Maison
Mickael Maison

Reputation: 26885

This error usually means you are missing the Scala library. Adding Scala 2.12 to your path should solve the issue.


2.12 is not Kafka's version but the Scala version. Kafka's version is the next number in the file name. For example, in kafka_2.11-1.1.0, the Kafka version is 1.1.0 and 2.11 is the Scala version.

Assuming you're running a recent Kafka version >= 0.11, the recommended way to create topics programmatically is via the Kafka Admin API. That avoids directly writing to Zookeeper and also does not depend on Scala (so you won't have to add it).

See AdminClient.createTopics() in the AdminClient's javadoc

Upvotes: 1

Related Questions