Please note, this is a STATIC archive of website www.javatpoint.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
Javatpoint Logo
Javatpoint Logo

Creating Kafka Topics

In this section, the user will learn to create topics using Command Line Interface(CLI) on Windows.

There are following steps used to create a topic:

Step1: Initially, make sure that both zookeeper, as well as the Kafka server, should be started.

Step2: Type 'kafka-topics -zookeeper localhost:2181 -topic -create' on the console and press enter. Here, the name of the topic is 'myfirst'

Note: Instead '.bat', use '.sh' while creating topics on Linux(accordingly).

Creating Kafka Topics

The output will be displayed as:

Creating Kafka Topics

The above snapshot displays an error "missing required argument partitions". As it is necessary to declare the number of partitions and its ISR(replication factor), while creating a topic. If not declared, such type of errors will be thrown.

Step3: Now, rewrite the above command after fulfilling the necessities, as:

'kafka-topics.bat -zookeeper localhost:2181 -topic <topic_name> --create ?partitions <value> --replication-factor <value>'. Press enter.

Creating Kafka Topics

Again, an error will be thrown, indicating a replication factor greater than the number of brokers. Till now, we have started only a single broker, not multiple brokers. Thus, such type of error will appear.

Note: The replication factor can never be greater than the number of available brokers.

Step4: Consider the replication factor as 1 for now, and rewrite the above command. Press enter. The output will be displayed as:

Creating Kafka Topics

Thus, if all the steps are processed successfully, then the topic will be created successfully, as shown in the above output.

Topic already exists

In case the user creates another topic with the same name as the existing topic, an error " topic <topic_name> already exists" will be thrown.

Creating Kafka Topics

Few more about topics

1) Listing the number of Topics

To list the number of topics created within a broker, use '-list' command as:

'kafka-topics.bat -zookeeper localhost:2181 -list'.

Creating Kafka Topics

There are two topics 'myfirst' and 'mysecond' present in the above snapshot.

2) Describing a topic

To describe a topic within the broker, use '-describe' command as:

'kafka-topics.bat -zookeeper localhost:2181 -describe --topic <topic_name>'. This command gives the whole description of a topic with the number of partitions, leader, replicas and, ISR.

Creating Kafka Topics

3) Deleting a topic

To delete a topic, use '-delete' command. The delete command is used as: 'kafka-topics.sh -zookeeper localhost:2181 -topic<topic_name> --delete'

Note: Window users will face issues while deleting any topic. So, prefer this command on Linux, or on macOS.

Creating Kafka Topics

In the above snapshot, it is seen that there were two existing topics 'myfirst' and 'mysecond'. After deleting 'mysecond' topic, only 'myfirst' topic is listed by the '-list' command.

So, in this way, various topics can be created and several commands can be applied on different topics.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA