KAFKA基本命令

如下操做基於Linux系統apache

1. 建立一個Topic bootstrap

用一個分區和一個副本建立一個名爲「test」的Topic服務器

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

 

2. 查看Topic列表server

bin/kafka-topics.sh --list --zookeeper localhost:2181文檔

 

3. 發送消息kafka

運行生產者,而後在控制檯輸入一些消息(以行的形式)發送到服務器,CTRL+C 終止消息的輸入it

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic testio

 > message 1console

 > message 2

 

4. 查看收到的消息

運行消費者,將消息轉儲到標準輸出

實時顯示從最初到如今的消息:bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic test

實時查看消息:bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test

 

5. 查看消費了多少數據

bin/kafka-run-class.sh kafka.tools.ConsumerOffsetChecker --zookeeper 127.0.0.1:2181 --group hansight --topic event

 

 6. 查看group

./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list

 

 7. 導入/導出數據

1)準備一些數據,輸出到當前目錄下的 test.txt 文件

   echo -e "test_data01\ntest_data02" > test.txt

2)啓動兩個獨立模式運行的鏈接器

   執行 connect-standalone.sh,攜帶三個配置文件做爲參數:kafka connect過程的配置、建立鏈接器的配置

      bin/connect-standalone.sh config/connect-standalone.properties config/connect-file-source.properties config/connect-file-sink.properties

   第一個鏈接器:源鏈接器,用於從輸入文件中按行讀取數據,並將每一個鏈接生成爲Topic

   第二個鏈接器:接收器鏈接器,從Topic中讀取消息,並在文件中已行的形式輸出消息

   啓動後,源鏈接器開始讀取 test.txt,並生成Topic:connect-test;接收器鏈接器開始讀取 connect-test 中的消息,並寫入到文件:test.sink.txt

   能夠在控制檯查看Topic中的數據:bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic connect-test --from-beginning

 

8. 刪除Topic

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

若是提示:marked for deletion,則並未真正刪除,須要修改配置文件 kafka/config/server.properties,將 delete.topic.enable=false 改成 true,保存後重啓kafka,再執行刪除命令便可

 

 9. 查看Topic詳情

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

 

參考文檔:kafka.apache.org/quickstart   —— kafka官方文檔

相關文章
相關標籤/搜索