開發與維運

【kafka運維】TopicCommand-Kafka運維腳本(1)

在這裡插入圖片描述

日常運維
問題排查
怎麼能夠少了滴滴開源的
滴滴開源LogiKM一站式Kafka監控與管控平臺

TopicCommand

1.Topic創建

bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 3 --topic test


相關可選參數

參數 描述 例子
--bootstrap-server 指定kafka服務 指定連接到的kafka服務; 如果有這個參數,則 --zookeeper可以不需要 --bootstrap-server localhost:9092
--zookeeper 棄用, 通過zk的連接方式連接到kafka集群; --zookeeper localhost:2181 或者localhost:2181/kafka
--replication-factor 副本數量,注意不能大於broker數量;如果不提供,則會用集群中默認配置 --replication-factor 3
--partitions 分區數量,當創建或者修改topic的時候,用這個來指定分區數;如果創建的時候沒有提供參數,則用集群中默認值; 注意如果是修改的時候,分區比之前小會有問題 --partitions 3
--replica-assignment 副本分區分配方式;創建topic的時候可以自己指定副本分配情況; --replica-assignment BrokerId-0:BrokerId-1:BrokerId-2,BrokerId-1:BrokerId-2:BrokerId-0,BrokerId-2:BrokerId-1:BrokerId-0 ; 這個意思是有三個分區和三個副本,對應分配的Broker; 逗號隔開標識分區;冒號隔開表示副本
--config <String: name=value> 用來設置topic級別的配置以覆蓋默認配置;只在--create 和--bootstrap-server 同時使用時候生效; 可以配置的參數列表請看文末附件 例如覆蓋兩個配置 --config retention.bytes=123455 --config retention.ms=600001
--command-config <String: command 文件路徑> 用來配置客戶端Admin Client啟動配置,只在--bootstrap-server 同時使用時候生效; 例如:設置請求的超時時間 --command-config config/producer.proterties ; 然後在文件中配置 request.timeout.ms=300000

2.刪除Topic

bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic test


支持正則表達式匹配Topic來進行刪除,只需要將topic 用雙引號包裹起來
例如: 刪除以create_topic_byhand_zk為開頭的topic;

bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic "create_topic_byhand_zk.*"
.表示任意匹配除換行符 \n 之外的任何單字符。要匹配 . ,請使用 . 。
·*·:匹配前面的子表達式零次或多次。要匹配 * 字符,請使用 *。
.* : 任意字符

刪除任意Topic (慎用)

bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic ".*?"

更多的用法請參考正則表達式

3.Topic分區擴容

zk方式(不推薦)

>bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic topic1 --partitions 2

kafka版本 >= 2.2 支持下面方式(推薦)

單個Topic擴容

bin/kafka-topics.sh --bootstrap-server broker_host:port --alter --topic test_create_topic1 --partitions 4

批量擴容 (將所有正則表達式匹配到的Topic分區擴容到4個)

sh bin/kafka-topics.sh --topic ".*?" --bootstrap-server 172.23.248.85:9092 --alter --partitions 4

".*?" 正則表達式的意思是匹配所有; 您可按需匹配

PS: 當某個Topic的分區少於指定的分區數時候,他會拋出異常;但是不會影響其他Topic正常進行;


相關可選參數

參數 描述 例子
--replica-assignment 副本分區分配方式;創建topic的時候可以自己指定副本分配情況; --replica-assignment BrokerId-0:BrokerId-1:BrokerId-2,BrokerId-1:BrokerId-2:BrokerId-0,BrokerId-2:BrokerId-1:BrokerId-0 ; 這個意思是有三個分區和三個副本,對應分配的Broker; 逗號隔開標識分區;冒號隔開表示副本

PS: 雖然這裡配置的是全部的分區副本分配配置,但是正在生效的是新增的分區;
比如: 以前3分區1副本是這樣的

Broker-1 Broker-2 Broker-3 Broker-4
0 1 2

現在新增一個分區,--replica-assignment 2,1,3,4 ; 看這個意思好像是把0,1號分區互相換個Broker

Broker-1 Broker-2 Broker-3 Broker-4
1 0 2 3

但是實際上不會這樣做,Controller在處理的時候會把前面3個截掉; 只取新增的分區分配方式,原來的還是不會變

Broker-1 Broker-2 Broker-3 Broker-4
0 1 2 3

4.查詢Topic描述

1.查詢單個Topic

sh bin/kafka-topics.sh --topic test --bootstrap-server xxxx:9092 --describe --exclude-internal

2.批量查詢Topic(正則表達式匹配,下面是查詢所有Topic)
sh bin/kafka-topics.sh --topic ".*?" --bootstrap-server xxxx:9092 --describe --exclude-internal

支持正則表達式匹配Topic,只需要將topic 用雙引號包裹起來


相關可選參數

參數 描述 例子
--bootstrap-server 指定kafka服務 指定連接到的kafka服務; 如果有這個參數,則 --zookeeper可以不需要 --bootstrap-server localhost:9092
--at-min-isr-partitions 查詢的時候省略一些計數和配置信息 --at-min-isr-partitions
--exclude-internal 排除kafka內部topic,比如__consumer_offsets-* --exclude-internal
--topics-with-overrides 僅顯示已覆蓋配置的主題,也就是單獨針對Topic設置的配置覆蓋默認配置;不展示分區信息 --topics-with-overrides

5.查詢Topic列表

1.查詢所有Topic列表

sh bin/kafka-topics.sh --bootstrap-server xxxxxx:9092 --list --exclude-internal

2.查詢匹配Topic列表(正則表達式)

查詢test_create_開頭的所有Topic列表
sh bin/kafka-topics.sh --bootstrap-server xxxxxx:9092 --list --exclude-internal --topic "test_create_.*"


相關可選參數

參數 描述 例子
--exclude-internal 排除kafka內部topic,比如__consumer_offsets-* --exclude-internal
--topic 可以正則表達式進行匹配,展示topic名稱 --topic

關於作者:石臻臻的雜貨鋪, 專注於 Java領域、大數據領域 等知識分享, 內容多為 原理 、源碼、實戰 等等, 堅持輸出乾貨,所寫內容必定經過驗證,並深入源碼分析,保證內容準確性, 長期在CSDN、和公眾號【石臻臻的雜貨鋪】發佈原創文章,歡迎關注! 如果有相關技術領域問題,歡迎進群交流,各個領域都有專人解答,你所問的,都會得到迴應!


歡迎Star共建滴滴開源的kafka的管理平臺
滿足所有開發運維日常需求

滴滴開源Logi-KafkaManager 一站式Kafka監控與管控平臺

Leave a Reply

Your email address will not be published. Required fields are marked *