標籤:tor func update 描述 delete res enable 按鍵組合 lead
Apache Kafka系列(一) 起步
Apache Kafka系列(二) 命令列工具(CLI)
Apache Kafka命令列工具(Command Line Interface,CLI),下文簡稱CLI。
1. 啟動Kafka
啟動Kafka需要兩步:
1.1. 啟動ZooKeeper
[[email protected] kafka_2.12-0.11.0.0]# bin/zookeeper-server-start.sh config/zookeeper.properties
1.2. 啟動Kafka Server
[[email protected] kafka_2.12-0.11.0.0]# bin/kafka-server-start.sh config/server.properties
2. 列出Topic
[[email protected] kafka_2.12-0.11.0.0]# bin/kafka-topics.sh --zookeeper localhost:2181 --listHelloWorld
3. 建立Topic
[[email protected] kafka_2.12-0.11.0.0]# bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic Demo1Created topic "Demo1".
上述命令會建立一個名為Demo1的Topic,並指定了replication-factor和partitions分別為1。其中replication-factor控制一個Message會被寫到多少台伺服器上,因此這個值必須小於或者
等於Broker的數量。
4. 描述Topic
[[email protected] kafka_2.12-0.11.0.0]# bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic Demo1Topic:Demo1 PartitionCount:1 ReplicationFactor:1 Configs: Topic: Demo1 Partition: 0 Leader: 0 Replicas: 0 Isr: 0
5. 發布訊息到指定的Topic
[[email protected] kafka_2.12-0.11.0.0]# bin/kafka-console-producer.sh --broker-list localhost:9092 --topic Demo1>this>is >the >firest>input
可以在控制台逐行輸入任意訊息。命令的終止符是:control + C按鍵組合。
6. 消費指定Topic上的訊息
[[email protected] kafka_2.12-0.11.0.0]# bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic Demo1thisis the firestinput
7. 修改Topic
7.1 增加指定Topic的partition,在第3步中建立的Demo1的partition是1。如下命令將增加10個partition
[[email protected] kafka_2.12-0.11.0.0]# bin/kafka-topics.sh --alter --zookeeper localhost:2181 --partitions 11 --topic Demo1WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affectedAdding partitions succeeded!
7.2. 刪除指定Topic
[[email protected] kafka_2.12-0.11.0.0]# bin/kafka-topics.sh --delete --zookeeper localhost:2181 --topic Demo1Topic Demo1 is marked for deletion.Note: This will have no impact if delete.topic.enable is not set to true.
Note中指出該Topic並沒有真正的刪除,如果真刪除,需要把server.properties中的delete.topic.enable置為true
7.3 給指定的Topic增加配置項,如給一個增加max message size值為128000
[[email protected] kafka_2.12-0.11.0.0]# bin/kafka-topics.sh --alter --zookeeper localhost:2181 --topic Demo1 --config max.message.bytes=128000WARNING: Altering topic configuration from this script has been deprecated and may be removed in future releases. Going forward, please use kafka-configs.sh for this functionalityUpdated config for topic "Demo1".
warning中指出該命令已經到期,將來可能被刪除,替代的命令是使用kafka-config.sh。新命令如下:
[[email protected] kafka_2.12-0.11.0.0]# bin/kafka-configs.sh --alter --zookeeper localhost:2181 --entity-type topics --entity-name Demo1 --add-config max.message.bytes=12800Completed Updating config for entity: topic ‘Demo1‘.
需要使用entity-type置為topics,並在entity-name中指定對應的名稱
8. 結論
本文展示了CLI所提供的一些常用的命令,這些基本的命令在營運Kafka過程中很實用。
Apache Kafka系列(二) 命令列工具(CLI)