I am programming a client to work with kafka 0.9. I want to know how to create a topic. This answer: How to create a Topic in Kafka through Java is similar to what I am aski
After looking through the scala api and various links online.
This is the solution I found:
Maven Dependencies:
org.apache.kafka
kafka_2.11
0.9.0.0
com.101tec
zkclient
0.7
Code:
import java.util.Properties;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;
import kafka.admin.AdminUtils;
import kafka.utils.ZKStringSerializer$;
import kafka.utils.ZkUtils;
public class KafkaJavaExample {
public static void main(String[] args) {
String zookeeperConnect = "zkserver1:2181,zkserver2:2181";
int sessionTimeoutMs = 10 * 1000;
int connectionTimeoutMs = 8 * 1000;
ZkClient zkClient = new ZkClient(
zookeeperConnect,
sessionTimeoutMs,
connectionTimeoutMs,
ZKStringSerializer$.MODULE$);
// Security for Kafka was added in Kafka 0.9.0.0
boolean isSecureKafkaCluster = false;
// ZkUtils for Kafka was used in Kafka 0.9.0.0 for the AdminUtils API
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect), isSecureKafkaCluster);
String topic = "my-topic";
int partitions = 2;
int replication = 3;
// Add topic configuration here
Properties topicConfig = new Properties();
AdminUtils.createTopic(zkUtils, topic, partitions, replication, topicConfig);
zkClient.close();
}
}
If you are wondering why the code below doesn't look like Java:
ZKStringSerializer$.MODULE$
It is because ZkStringSerializer is a Scala Object. You can read more information about that here:
How create Kafka ZKStringSerializer in Java?
Note: You must initialize the ZkClient with ZKStringSerializer.
If you don't, then createTopic() will only seem to work
(In other words: it will return without error).
The topic will exist in only Zookeeper and only works when listing topics.
i.e. list command below works fine
bin/kafka-topics.sh --list --zookeeper localhost:2181
but Kafka itself does not create the topic. To illustrate, the describe command below will throw an error.
bin/kafka-topics.sh --describe --zookeeper localhost:2181
Therefore, make sure you initialize it with ZKStringSerializer$.MODULE$.
References: How Can we create a topic in Kafka from the IDE using APIfrom-the-ide-using-api
Soon Chee Loong, University of Toronto