The basic command to create a Kafka topic

Category : Apache Kafka | Sub Category : Apache Kafka | By Prasad Bonam Last updated: 2023-08-05 09:08:54 Viewed : 297


The basic command to create a Kafka topic :

To create a Kafka topic, you can use the kafka-topics.sh script (or kafka-topics.bat on Windows) provided by Apache Kafka. This script allows you to interact with Kafka topics and perform various operations, including creating, listing, and deleting topics.

The basic command to create a Kafka topic is as follows:

css
kafka-topics.sh --bootstrap-server <BROKER_LIST> --create --topic <TOPIC_NAME> --partitions <NUM_PARTITIONS> --replication-factor <REPLICATION_FACTOR>
  • --bootstrap-server: Specifies the Kafka broker(s) to connect to in the format host:port. You need to provide at least one broker to execute the command.

  • --create: Indicates that you want to create a new topic.

  • --topic: Specifies the name of the topic you want to create.

  • --partitions: Specifies the number of partitions for the topic. Each partition is a linearly ordered sequence of messages.

  • --replication-factor: Specifies the replication factor for the topic. The replication factor determines how many copies of each partition will be created across different brokers. It should be equal to or less than the number of available brokers in the Kafka cluster.

Example: Lets say you want to create a Kafka topic named "my_topic" with 3 partitions and a replication factor of 2. You have a Kafka cluster running with two brokers on localhost:9092 and localhost:9093.

Using the command-line tool on Unix/Linux/Mac:

bash
bin/kafka-topics.sh --bootstrap-server localhost:9092,localhost:9093 --create --topic my_topic --partitions 3 --replication-factor 2

Using the command-line tool on Windows:

batch
binwindowskafka-topics.bat --bootstrap-server localhost:9092,localhost:9093 --create --topic my_topic --partitions 3 --replication-factor 2

Explanation: In the above example, we are using the kafka-topics.sh (or kafka-topics.bat) script to create a Kafka topic named "my_topic." We specify the Kafka brokers to connect to using the --bootstrap-server option, providing the broker addresses localhost:9092 and localhost:9093. We want to create a topic with 3 partitions and a replication factor of 2, which means each partition will have two replicas across different brokers.

After executing the command, Kafka will create the "my_topic" topic with the specified number of partitions and replicas. The topic is now ready to be used for publishing and consuming messages in the Kafka cluster.


Search
Sub-Categories
Related Articles

Leave a Comment: