Apache Kafka Series (i) Start
Apache Kafka Series (ii) command line tools (CLI)
Apache Kafka Command Line INTERFACE,CLI, hereinafter referred to as the CLI.
1. Start Kafka
Starting Kafka takes two steps:
1.1. Start Zookeeper
[Email protected] kafka_2. -0.11. 0.0] # Bin/zookeeper-server-start. SH config/zookeeper.properties
1.2. Start Kafka Server
[Email protected] kafka_2. -0.11. 0.0] # Bin/kafka-server-start. SH
2. List Topic
[Email protected] kafka_2. -0.11. 0.0] # bin/kafka-topics. sh --zookeeper localhost:2181 --Listhelloworld
3. Create Topic
[Email protected] kafka_2. -0.11. 0.0] # bin/kafka-topics. sh --create--zookeeper localhost:218111 --"Demo1 ".
The above command creates a topic named Demo1 and specifies that Replication-factor and partitions are 1 respectively. Where Replication-factor controls how many servers a message is written to, so this value must be less than or
equals the number of broker.
4. Description Topic
[Email protected] kafka_2. -0.11. 0.0] # bin/kafka-topics. sh --describe--zookeeper localhost:2181 --topic demo1topic:demo1 partitioncount: 1 Replicationfactor:1 configs: topic:demo1 0 0 0 0
5. Publish the message to the specified topic
[Email protected] kafka_2. -0.11. 0.0] # Bin/kafka-console-producer. sh --broker-list localhost:9092 --topic Demo1>this >> >firest>input
You can enter any message row by line in the console. The terminating character of the command is: Control + C key combination.
6. Consume the message on the specified topic
[Email protected] kafka_2. -0.11. 0.0] # Bin/kafka-console-consumer. sh --zookeeper localhost:2181 --from-beginning--topic Demo1thisis thefirestinput
7. Modify Topic
7.1 Increase the partition of the specified topic, and the partition of DEMO1 created in step 3rd is 1. The following command will add 10 partition
[Email protected] kafka_2. -0.11. 0.0] # bin/kafka-topics. sh --alter--zookeeper localhost:2181 -- for a topic the has a key, th e partition logic or ordering of the messages would be affectedadding partitions succeeded!
7.2. Delete the specified topic
[Email protected] kafka_2. -0.11. 0.0] # bin/kafka-topics. sh --delete--zookeeper localhost:2181 --foriftrue.
Note that the topic is not really deleted, and if you delete it, you need to set the delete.topic.enable in Server.properties to true.
7.3 Adds a configuration item to the specified topic, such as adding a max message to a size value of 128000
[[email protected] Kafka_2. -0.11 . 0.0 ] # bin/kafka-topics. sh --alter--zookeeper localhost:2181 -- Topic Demo1--config max.message.bytes=128000 Warning:altering topic configuration From this script have been deprecated and may be removed in future releases. Going forward, use Kafka -configs. sh for this functionalityupdated config for topic " Demo1 ".
Warning indicates that the command has expired and may be removed in the future, instead of using kafka-config.sh. The new command is as follows:
[Email protected] kafka_2. -0.11. 0.0] # Bin/kafka-configs. sh --alter--zookeeper localhost:2181 --entity-type topics--entity-name Demo1--add-config max.message.bytes=12800for'Demo1'.
You need to use Entity-type to topics and specify the corresponding name in Entity-name
8. Conclusion
This article shows some of the commonly used commands provided by the CLI, which are useful in the operations Kafka process.
Apache Kafka Series (ii) command line tools (CLI)