TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Creating Kafka Topics

Creating Kafka Topics with Apache Kafka Introduction, What is Kafka, Kafka Topics, Kafka Topic Replication, Kafka Fundamentals, Kafka Architecture, Installation, Kafka Tools, Kafka Application etc.

<< Back to CREATING

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'

kafka-topics -zookeeper localhost:2181 -topic <topic_name> -create

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.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf