天天看點

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

【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

--replica-assignment

PS: 雖然這裡配置的是全部的分區副本配置設定配置,但是正在生效的是新增的分區;

比如: 以前3分區1副本是這樣的

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

現在新增一個分區,

--replica-assignment

2,1,3,4 ; 看這個意思好像是把0,1号分區互相換個Broker

3

但是實際上不會這樣做,Controller在處理的時候會把前面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

--zookeeper

--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

__consumer_offsets-*

--exclude-internal

--topic

可以正規表達式進行比對,展示topic名稱

--topic

關于作者:石臻臻的雜貨鋪, 專注于 Java領域、大資料領域 等知識分享, 内容多為 原理 、源碼、實戰 等等, 堅持輸出幹貨,所寫内容必定經過驗證,并深入源碼分析,保證内容準确性, 長期在CSDN、和公衆号【石臻臻的雜貨鋪】釋出原創文章,歡迎關注! 如果有相關技術領域問題,歡迎進群交流,各個領域都有專人解答,你所問的,都會得到回應!

歡迎Star和共建由滴滴開源的kafka的管理平台

滿足所有開發運維日常需求

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