Creating Topic ZooKeeper/Kafka Server: Apache ZooKeeper is a software project of the Apache Software Foundation. It is essentially a service for distributed systems offering a hierarchical key-value store, which is used to provide a distributed configuration service, synchronization service, and naming registry for large distributed systems.

The topic will be created in ZooKeeper.

Data is inserted into partitions created in topic with Kafka server, we can create any number of Topics and we can any number of partitions in a topic

How to Create a Topic in ZooKeeper?

Create the maven project with below dependency


Create the class with name

import java.util.Properties;
import kafka.admin.AdminUtils;
import kafka.admin.RackAwareMode;
import kafka.utils.ZKStringSerializer$;
import kafka.utils.ZkUtils;

import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;

public class TopicCreation
    public static void main(String[] args) throws Exception {
        ZkClient zkClient = null;
        ZkUtils zkUtils = null;
        try {
            String zookeeperHosts = ""; // If multiple zookeeper then -> String zookeeperHosts = "host1:2181,host2:2181";
            int sessionTimeOutInMs = 15 * 1000; // 15 secs
            int connectionTimeOutInMs = 10 * 1000; // 10 secs

            zkClient = new ZkClient(zookeeperHosts, sessionTimeOutInMs, connectionTimeOutInMs, ZKStringSerializer$.MODULE$);
            zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperHosts), false);
            // creating topic name 
            // we are creating testTopic topic name 
            String topicName = "testTopic";
            // No of partitions count
            int noOfPartitions = 1;
            int noOfReplication = 1;
            Properties topicConfiguration = new Properties();
			AdminUtils.createTopic(zkUtils, topicName, noOfPartitions, noOfReplication, topicConfiguration, RackAwareMode.Enforced$.MODULE$ );

        } catch (Exception ex) {
        } finally {
            if (zkClient != null) {

Generally, ZooKeeper runs on 2181 Port if your ZooKeeper runs on different port change accordingly.

We can give multiple ZooKeeper Hosts with respective port no.

Run the class as Java application to create Topic in

Also Read: Kafka Consumer Examples Using Java

Creating Topic Zookeeper/Kafka Server

Leave a Reply

Your email address will not be published. Required fields are marked *