Kafka入門:常用開發、運維命令(kafka0.10.x)

如果您覺得“大數據開發運維架構”對你有幫助,歡迎轉發朋友圈


概述:

下面是開發和項目運維實戰過程中總結的一些kafka常用的命令,已在kafka0.10.x環境進行了測試驗證。

常用命令:

1.查看幫助

<code>/usr/hdp/2.6.3.0-235/kafka/bin/kafka-topics.sh  --help/<code>

2.查看當前集群所有topic

<code>./kafka-topics.sh  --zookeeper salver31.hadoop.unicom:2181 --list /<code>

3.查看topic詳細信息 (kafkawordcount)

<code>./kafka-topics.sh  --zookeeper salver31.hadoop.unicom:2181 -describe -topic kafkawordcount/<code>

4.查看kafka集群詳細信息

<code>./kafka-topics.sh  -zookeeper  salver31.hadoop.unicom:2181 --describe/<code>

5.新建topic

<code>./kafka-topics.sh --zookeeper salver31.hadoop.unicom:2181 --create --topic topic001       --partitions 1 --replication-factor 1 --config max.message.bytes=64000 --config flush.messages=1/<code>
Kafka入門:常用開發、運維命令(kafka0.10.x)

注:

與主題相關的配置默認使用全局缺省值,也可通過--config key=value的形式對全局缺省值進行覆蓋。上面的topic1自定義了最大消息大小和刷新速度。


6.修改topic配置:


可以使用alter topic命令更改或設置覆蓋。

更新分區數:

<code>./kafka-topics.sh --zookeeper salver31.hadoop.unicom:2181 --alter --topic topic001 --partitions 2/<code>

更新my-topic的最大消息大小:

<code>./kafka-topics.sh --zookeeper salver31.hadoop.unicom:2181 --alter --topic topic001 --config max.message.bytes=128000/<code>

刪除自定義配置(刪除自定義最大消息字節數):

<code>./kafka-topics.sh --zookeeper salver31.hadoop.unicom:2181 --alter --topic topic001 --delete-config max.message.bytes/<code>


7.查看topic各個分區消息的數據量:

<code> ./kafka-run-class.sh  kafka.tools.GetOffsetShell --broker-list salver31.hadoop.unicom:6667,salver32.hadoop.unicom:6667 --topic kafkawordcount --time -1/<code>


8.查看log日誌消息內容:

<code>./kafka-run-class.sh kafka.tools.DumpLogSegments --files  /kafka-logs/kafkawordcount-0/00000000000000000000.log --print-data-lo/<code>

9.Producer發送消息:

<code>./kafka-console-producer.sh --broker-list salver31.hadoop.unicom:6667,salver32.hadoop.unicom:6667  --topic topic001/<code>

10.Consumer接收消息:

<code>./kafka-console-consumer.sh --bootstrap-server salver31.hadoop.unicom:6667,salver32.hadoop.unicom:6667 --from-beginning --topic kafkawordcount/<code>

接收消息,三個常用參數:

consumer-property 參數以鍵值對的形式指定消費者級別的配置。

from-beginning 設置消息起始位置開始消費。默認是從新位置 latest開始消費

delete-consumer-offsets 刪除在zookeeper中記錄已消費的偏移量

帶參數Consumer接收消息:

<code>./kafka-console-consumer.sh --bootstrap-server salver31.hadoop.unicom:6667,salver32.hadoop.unicom:6667 --topic kafkawordcount --consumer-property group.id=consumerGroup1 --consumer-property consumer.id=consumerId1 --from-beginning --delete-consumer-offsets/<code>

11.查看topic某個消費組對應的offset:

<code> ./kafka-run-class.sh kafka.tools.ConsumerOffsetChecker --topic kafkawordcount --zookeeper localhost:2181 --group  consumerGroup1/<code>

12.查看topic最大offset:

<code>/usr/hdp/2.6.3.0-235/kafka/bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list salver31.hadoop.unicom:6667,salver32.hadoop.unicom:6667 --topic test511 --time -2/<code>

查看最小offset

<code>/usr/hdp/2.6.3.0-235/kafka/bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list salver31.hadoop.unicom:6667,salver32.hadoop.unicom:6667 --topic test511 --time -1/<code>

.刪除topic

<code>./kafka-topics.sh --zookeeper salver31.hadoop.unicom:2181 --delete --topic topic001/<code> 

注意:

這裡刪除topic並沒有真正刪除,只是給topic標記為"deleted",如果需要徹底刪除topic,請參考kafka進階中的文章:“kafka進階:如何徹底刪除topic及數據”。


分享到:


相關文章: