1. 程式人生 > 其它 >kafka常用指令碼

kafka常用指令碼

原文地址: kafka常用指令碼

歡迎訪問: 我的部落格

引言

本文是轉載的, 但是原文

在Kafka安裝目錄下 ($KAFKA_HOME/bin), 提供了很多內建的指令碼供我們使用, 使用指令碼可以測試 Kafka 的大多數功能, 下面我們就指令碼的使用作出說明.

啟動 broker

bin/kafka-server-start.sh 指令碼提供了啟動 broker 的功能.

前臺啟動

bin/kafka-server-start.sh config/server.properties

後臺啟動:

bin/kafka-server-start.sh -daemon config/server.properties

停止 broker

bin/kafka-server-stop.sh 指令碼提供了停止 broker 的功能.

bin/kafka-server-stop.sh

建立topic

bin/kafka-topic.sh指令碼提供了建立 topic 的功能. 建立 topic 時, 需要指定兩個引數:

  • 分割槽數量
  • 副本數量

注意, 副本數量最多不能超過當前叢集中 broker 節點的數量.

下面建立一個名為 test 的 topic, 具有 3 個分割槽, 副本數量為 2.

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

檢視topic列表

bin/kafka-topic.sh指令碼提供了檢視 topic 列表的功能. 通過 --list 引數即可檢視當前叢集所有的 topic 列表.

bin/kafka-topics.sh --zookeeper localhost:2181 --list
__consumer_offsets
test

檢視某個topic的詳細資訊

bin/kafka-topic.sh 指令碼提供了檢視某個 topic 詳細資訊的功能. 通過 --describe 引數和 --topic 引數指定 topic 名稱即可.

bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic test
Topic:test PartitionCount:3 ReplicationFactor:2 Configs:
Topic: test Partition: 0 Leader: 2 Replicas: 2,0 Isr: 2,0
Topic: test Partition: 1 Leader: 0 Replicas: 0,1 Isr: 0,1
Topic: test Partition: 2 Leader: 1 Replicas: 1,2 Isr: 1,2

刪除topic

bin/kafka-topic.sh指令碼提供了刪除 topic 的功能. 通過--delete 引數和--topic引數指定 topic 名稱即可.

將名稱為 test 的 topic 刪除:

bin/kafka-topics.sh --zookeeper localhost:2181/kafka --delete --topic test
Topic test is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.

注意, broker 配置中必須設定 delete.topic.enable=true, 否則刪除操作不會生效.

命令列生產者

bin/kafka-console-producer.sh 指令碼能夠通過命令列輸入向指定的 topic 中傳送資料.

向名稱為 test 的 topic 中傳送 3 條資料:

bin/kafka-console-producer.sh --broker-list hostname:9092 --topic test
This is a message
This is another message
hello kafka

命令列消費者

bin/kafka-console-consumer.sh指令碼能夠消費 topic 中的資料並列印到控制檯.

消費名稱為 test 的 topic 中的資料:

bin/kafka-console-consumer.sh --bootstrap-server hostname:9092 --topic test --from-beginning
This is a message
This is another message
hello kafka
^CProcessed a total of 3 messages

注意, –from-beginning 引數表示從 topic 的最開始位置進行消費, 如果沒有指定該引數, 表示從末尾開始消費, 只有當啟動消費者後, 有新的資料寫入, 才能夠顯示到控制檯.

檢視消費組

bin/kafka-consumer-groups.sh 指令碼能夠檢視叢集中消費組的資訊. 通過--list 引數能夠列出當前消費組列表.

bin/kafka-consumer-groups.sh --bootstrap-server hostname:9092 --new-consumer --list
console-consumer-54336

通過 --describe 引數可以檢視消費組的消費詳情:

bin/kafka-consumer-groups.sh --bootstrap-server company01:9092 --new-consumer --describe --group console-consumer-54336

TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test 0 1 1 0 consumer-1-860f59b3-ebe9-4cda-a074-d108b1fec7bf /192.168.100.109 consumer-1
test 1 1 1 0 consumer-1-860f59b3-ebe9-4cda-a074-d108b1fec7bf /192.168.100.109 consumer-1
test 2 1 1 0 consumer-1-860f59b3-ebe9-4cda-a074-d108b1fec7bf /192.168.100.109 consumer-1

CURRENT-OFFSET 表示消費者當前消費到該分割槽的偏移量.

LOG-END-OFFSET 表示當前分割槽最後的偏移量.

LAG 表示消費者 "落後" 的消費進度.

原文地址: kafka常用指令碼

歡迎訪問: 我的部落格