Apache Kafka create topic command

In Apache Kafka, a topic is a named stream of records. Producers write records to a topic, and consumers read records from that topic. When you create a Kafka topic, the important choices are the topic name, the number of partitions, and the replication factor.

For current Kafka installations, use kafka-topics.sh with --bootstrap-server to connect to a Kafka broker. Older ZooKeeper-based Kafka installations may still use the --zookeeper option. This tutorial keeps the original ZooKeeper example and also shows the current broker-based command used in the Apache Kafka quickstart.

Current syntax to create a Kafka topic with bootstrap server

The recommended command format is:

</>
Copy
bin/kafka-topics.sh --create --bootstrap-server <BOOTSTRAP_SERVER:PORT> --topic <TOPIC_NAME> --partitions <NO_OF_PARTITIONS> --replication-factor <NO_OF_REPLICATIONS>

For a local Kafka broker running on the default port, the bootstrap server is usually localhost:9092.

Legacy ZooKeeper syntax to create Kafka Topic

In older Kafka setups that depend on ZooKeeper, topic metadata is maintained through ZooKeeper. For each Topic, you may specify the replication factor and the number of partitions. A topic is identified by its name. All these information has to be provided as arguments to the shell script, /kafka-topics.sh, while creating a new Kafka Topic.

Following is the syntax to create a topic :

./kafka-topics.sh --create --zookeeper <ZOOKEEPER_URL:PORT> --replication-factor <NO_OF_REPLICATIONS> --partitions <NO_OF_PARTITIONS> --topic <TOPIC_NAME>

where the arguments are :

ArgumentDescription
<ZOOKEEPER_URL:PORT>IP and port at which zookeeper is running. Use this only for older ZooKeeper-based Kafka clusters.
<BOOTSTRAP_SERVER:PORT>Kafka broker address used by current Kafka CLI commands, for example localhost:9092.
<NO_OF_REPLICATIONS>Number of copies maintained for each partition. In a single-broker local setup, use 1.
<NO_OF_PARTITIONS>Number of partitions into which the Topic has to be partitioned.
<TOPIC_NAME> Name of the Kafka Topic to be created.

Choose partitions and replication factor before creating a Kafka topic

Before running the create topic command, decide these values carefully:

  • Topic name: Use a clear name such as orders, payments, or sampleTopic. Kafka topic names commonly use letters, numbers, dots, hyphens, and underscores.
  • Partitions: Partitions allow Kafka to distribute data and consumer work. For a beginner local example, 1 partition is enough. For real workloads, choose partitions based on expected throughput and consumer parallelism.
  • Replication factor: Replication stores copies of partitions on different brokers. In a single-node local setup, use 1. In a multi-broker cluster, a replication factor such as 3 is common, but it must not be greater than the number of available brokers.

Example to Create a Kafka Topic named sampleTopic

To create a topic in Apache Kafka, the Kafka environment has to be up and running. If you are using a modern local Kafka distribution, start Kafka as described in the official Apache Kafka quickstart. If you are using an older ZooKeeper-based distribution, start ZooKeeper and Kafka as shown below.

Start Zookeeper and Kafka Cluster

Navigate to the root of Kafka directory and run each of the following commands in separate terminals to start Zookeeper and Kafka Cluster.

$ bin/zookeeper-server-start.sh config/zookeeper.properties
$ bin/kafka-server-start.sh config/server.properties

Create sampleTopic using the current Kafka bootstrap-server option

For current Kafka CLI usage, run kafka-topics.sh with --bootstrap-server. The following command creates a topic named sampleTopic with one partition and one replica on a local broker.

</>
Copy
bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --topic sampleTopic --partitions 1 --replication-factor 1

A successful command returns a message similar to the following:

Created topic sampleTopic.

Run kafka-topics.sh with required arguments in an older ZooKeeper setup

Kafka provides a script, kafka-topics.sh, in the <KAFKA_HOME>/bin/ directory, to create a topic in the Kafka cluster.

An example is given below :

./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic sampleTopic

Running the script creates a Topic named  sampleTopic  with   replication and  1  partition maintaining metadata in the Zookeeper live at localhost:2181.

Open a terminal from bin directory and run the shell script  kafka-topics.sh  as shown below :

create a Topic in Apache Kafka - Apache Kafka Tutorial - www.tutorialkart.com

Created topic “sampleTopic”. : sampleTopic has been created.

Verify the Kafka topic after creation

After creating the topic, list topics in the cluster to confirm that sampleTopic is available.

</>
Copy
bin/kafka-topics.sh --list --bootstrap-server localhost:9092

You can also describe the topic to check partition count, replication factor, leader, replicas, and in-sync replicas.

</>
Copy
bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic sampleTopic

For a one-partition, one-replica local topic, the output will show one partition and one replica. In a multi-broker cluster, this command is useful for checking whether replicas are assigned as expected.

Error : Topic already exists.

When you try to create a duplicate topic, you could get an error saying the topic already exists. An example is given below :

If you are not sure whether the topic already exists, list topics first. If the topic exists but needs a different partition count, use the alter command. Kafka allows increasing partitions, but decreasing partitions is not supported because it would change the way existing records are stored and assigned.

</>
Copy
bin/kafka-topics.sh --alter --bootstrap-server localhost:9092 --topic sampleTopic --partitions 3

Common Kafka topic creation issues and fixes

IssueLikely reasonWhat to check
Topic already existsThe topic name is already present in the cluster.Run --list or choose another topic name.
Replication factor larger than available brokersThe requested replication factor is greater than the number of brokers.Use --replication-factor 1 for a single broker, or start more brokers.
Connection refusedThe Kafka broker is not running, or the bootstrap server address is wrong.Check the broker process and confirm localhost:9092 or the configured broker address.
ZooKeeper option not recognizedThe Kafka version or distribution expects broker-based topic management.Use --bootstrap-server instead of --zookeeper.

Kafka create topic command reference links

For the current Kafka startup flow and topic creation command, refer to the Apache Kafka Quickstart. For broader Kafka CLI behavior and configuration details, refer to the Apache Kafka Documentation.

FAQs on Apache Kafka create topic command

Should I use –bootstrap-server or –zookeeper to create a Kafka topic?

Use --bootstrap-server for current Kafka installations. Use --zookeeper only when you are working with an older ZooKeeper-based Kafka cluster that still supports that option.

What replication factor should I use when creating a Kafka topic locally?

For a single-broker local Kafka setup, use --replication-factor 1. A replication factor greater than one needs multiple brokers because Kafka must place replicas on different brokers.

How many partitions should I create for a new Kafka topic?

For a beginner example, one partition is simple and easy to inspect. For production topics, choose partitions based on message volume, consumer parallelism, ordering requirements, and expected future growth.

Can I create a Kafka topic without specifying partitions and replication factor?

Some Kafka configurations allow topic creation with broker defaults when partitions and replication factor are not specified. In tutorials and controlled setups, it is better to pass both values explicitly so the result is clear.

How do I check whether a Kafka topic was created successfully?

Run kafka-topics.sh --list --bootstrap-server localhost:9092 to see the topic name, or run kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic sampleTopic to inspect partition and replica details.

Editorial QA checklist for Apache Kafka topic creation

  • The tutorial shows the current --bootstrap-server command before the older --zookeeper command.
  • The example topic name, partition count, and replication factor are consistent across commands.
  • Single-broker examples use replication factor 1.
  • The verification commands include both --list and --describe.
  • The duplicate topic error section explains what to do before creating or altering a topic.

Conclusion

In this Apache Kafka TutorialKafka Create Topic, we created a Kafka topic using kafka-topics.sh, reviewed the current --bootstrap-server command, kept the older ZooKeeper command for legacy clusters, and verified the topic using list and describe commands.