Apache Kafka學習(三)之Kafka經常使用命令

一、開啓zookeeper(在安裝目錄下使用命令)

Linux:bin/zkServer.sh starthtml

windows:bin\zkServer.cmdwindows

二、啓動kafka(安裝目錄下使用命令)

Linux:bin/kafka-server-start.sh start config/server.propertiesspa

windows:bin\windows\kafka-server-start.bat config\server.propertiesorm

三、查看topic名稱列表

Linux:bin/kafka-topics.sh -list --zookeeper 172.16.0.99:2181,172.16.0.218:2181server

windows:bin\windows\kafka-topics.bat -list --zookeeper 172.16.0.99:2181,172.16.0.218:2181htm

四、查看topic詳情

Linux:bin/kafka-topics.sh -zookeeper localhost:2181 --topic test --describeip

windows:bin\windows\kafka-topics.bat --zookeeper localhost:2181 --topic test --describeget

五、刪除topic

Linux:bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic "test"kafka

windows:bin\windows\kafka-topics.bat --zookeeper localhost:2181 --delete --topic "test"cmd

注意:集羣中一臺機器刪除了topic,其餘機器同步刪除相同topic

六、建立topic

Linux:bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

windows:bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

注意:

replication-factor:副本個數,通常爲小於等於Broker個數。

partitions:分區個數。若是副本個數爲1,分區爲4,則4個分區會均勻的分佈在各個Broker上。若是Broker爲2,副本爲2,分區爲4,則每一個Broker上面都有4個分區。

七、建立Consumer

Linux:bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning

windows:bin\windows\kafka-console-consumer.bat --zookeeper localhost:2181 --topic test --form-beginning

注意:form beginning表示從頭拉取。

八、建立Producer

Linux:bin/kafka-console-producer.sh --broker-list 172.16.0.99:9020,172.16.0.218:9020 --topic test

windows:bin\windows\kafka-console-producer.bat --broker-list 172.16.0.99:9092,172.16.0.218:9080 --topic test

注意:此處是kafka的端口,並且在集羣裏若是此處填localhost,會報一個鏈接錯誤,猜測應該是消息沒有到達集羣,所以此處將集羣的ip都填上。

九、查詢topic全部分區的offset值

Linux:bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list 10.162.160.115:9092 --topic s1mmetest --time -1

十、查詢kafka集羣當前topic全部分區中的消息數目

Linux:bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list 132.232.78.175:9092 --topic s1mmetest --time -2

 

參考:https://www.2cto.com/kf/201804/739331.html

相關文章
相關標籤/搜索