In searching for how to create a Kafka topic through the API, I found this example in Scala:
import kafka.admin.AdminUtils
import kafka.utils.ZKStringSerializer
import org.I0Itec.zkclient.ZkClient
// Create a ZooKeeper client
val sessionTimeoutMs = 10000
val connectionTimeoutMs = 10000
val zkClient = new ZkClient("zookeeper1:2181", sessionTimeoutMs,
connectionTimeoutMs, ZKStringSerializer)
// Create a topic with 8 partitions and a replication factor of 3
val topicName = "myTopic"
val numPartitions = 8
val replicationFactor = 3
val topicConfig = new Properties
AdminUtils.createTopic(zkClient, topicName,
numPartitions, replicationFactor, topicConfig)
Source: https://stackoverflow.com/a/23360100/871012
The last arg ZKStringSerializer
is apparently a Scala object. It is not clear to me how to make this example work in Java.
This post How to create a scala object in clojure asks the same question in Clojure and the answer was:
ZKStringSerializer$/MODULE$
which in Java would (I think) translate to:
ZKStringSerializer$.MODULE$
But when I try that (or any number of other variations) none of them compile.
The compilation error is:
KafkaTopicCreator.java:[16,18] cannot find symbol
symbol: variable ZKStringSerializer$
location: class org.sample.KafkaTopicCreator
I am using kafka_2.9.2-0.8.1.1 and Java 8.
For java try the following,
First import below statement
Create object for ZkClient in the following way,