C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Creating Kafka TopicsIn 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 kafka-topics -zookeeper localhost:2181 -topic <topic_name> -create Note: Instead '.bat', use '.sh' while creating topics on Linux(accordingly).The output will be displayed as: 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. 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: Thus, if all the steps are processed successfully, then the topic will be created successfully, as shown in the above output. Topic already existsIn case the user creates another topic with the same name as the existing topic, an error " topic <topic_name> already exists" will be thrown. Few more about topics1) 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'. 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. 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.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.
Next TopicSending data to Kafka Topics
|