【kafka】2022-02-25【常用命令】

本人很懶,筆記這些都寫在有道云忘衍;此外逾苫,感覺kafka的資料是真的多,且官網(wǎng)關(guān)于命令是真的很詳細(xì)了枚钓,把自己常用的命令記錄與分享下铅搓。

備注:命令基于kafka-2.1.1,如有雷同搀捷,聯(lián)系侵刪

# topic 相關(guān)

./kafka-topics.sh --zookeeper=zk_server --describe --topic test_topic

./kafka-topics.sh --zookeeper=zk_server --delete --topic test_topic

./kafka-topics.sh --zookeeper=zk_server --create --partitions 1 --replication-factor 3 --topic test_topic

# 可以將副本指定到對應(yīng)的節(jié)點,結(jié)合寫個小工具生成分布字符串,方便做隔離,

./kafka-topics.sh --zookeeper=zk_server --create --replica-assignment 1:3,2:1,3:2 --topic test_topic

# 查看topic列表星掰,看是否創(chuàng)建成功

./kafka-topics.sh --zookeeper=zk_server --list

./kafka-topics.sh --zookeeper=zk_server --alter --topic test_topic --partitions 3

./kafka-topics.sh --zookeeper=zk_server --alter --partitions 3 --topic test_topic --replica-assignment 1:3,2:1,3:2

./kafka-configs.sh --zookeeper=zk_server --alter --entity-name test_topic --entity-type topics --delete-config retention.ms

# 遷移相關(guān),可先修改保存時間避免大量數(shù)據(jù)同步

./kafka-configs.sh --zookeeper=zk_server --alter --entity-type topics --add-config retention.ms=86400000 --entity-name test_topic

# 第一步:指定topic

echo {\"topics\": [{\"topic\": \"test_topic\"}],\"version\": 1} > ../conf/topic.json

# 第二步:生成分配策略

./kafka-reassign-partitions.sh --zookeeper=zk_server --topics-to-move-json-file ../conf/topic.json --broker-list "1,2,3" --generate

# 第三步:不限速執(zhí)行遷移(沒有流量推薦使用,有流量慎重使用)

./kafka-reassign-partitions.sh --zookeeper=zk_server --reassignment-json-file ../conf/reassignment.json --execute

# 第四步:限速執(zhí)行遷移(推薦使用)嫩舟,遷移完成需執(zhí)行下面的命令

./kafka-reassign-partitions.sh --zookeeper=zk_server --reassignment-json-file ../conf/reassignment.json --execute --throttle 52400000

# 切換目錄遷移:any同時限速(如果是鑒權(quán)集群可能要加一些賬密配置)

./kafka-reassign-partitions.sh --zookeeper=zk_server --bootstrap-server broker:port --reassignment-json-file ../conf/reassignment.json --execute --replica-alter-log-dirs-throttle 52400000

# 第五步:查看遷移進度氢烘,如果全部遷移完成執(zhí)行該命令移除限速

./kafka-reassign-partitions.sh --zookeeper=zk_server --reassignment-json-file ../conf/reassignment.json --verify

./kafka-reassign-partitions.sh --zookeeper=zk_server --bootstrap-server broker:port --reassignment-json-file ../conf/reassignment.json --verify

# 指定均衡

./kafka-preferred-replica-election.sh --zookeeper=zk_server --path-to-json-file ../conf/reassignment.json

# 全局均衡(生產(chǎn)慎用)

./kafka-preferred-replica-election.sh --zookeeper=zk_server

# 授權(quán)相關(guān),查看授權(quán)列表

./kafka-acls.sh --authorizer-properties zookeeper.connect=zk_server --list

# 查看用戶信息

./kafka-configs.sh --zookeeper=zk_server --describe --entity-name test_user --entity-type users

# 添加賬戶

./kafka-configs.sh --zookeeper=zk_server --alter --add-config 'SCRAM-SHA-256=[iterations=8192,password=test_user]' --entity-type users --entity-name test_user

# 添加寫

./kafka-acls.sh --authorizer-properties zookeeper.connect=zk_server --add --allow-principal User:'test_user' --allow-host '*' --operation Write --topic 'test_topic'

# 添加讀

./kafka-acls.sh --authorizer-properties zookeeper.connect=zk_server --add --allow-principal User:'test_user' --allow-host '*' --operation Read --topic 'test_topic' --group 'group_test_topic'

# 事務(wù)-賬戶冪等寫入集群

./kafka-acls.sh --authorizer-properties zookeeper.connect=zk_server --add --allow-principal User:'test_user' --allow-host '*' --operation IdempotentWrite --cluster kafka-cluster

# 事務(wù)-賬戶授事務(wù)ID的所有權(quán)限

./kafka-acls.sh --authorizer-properties zookeeper.connect=zk_server --add --allow-principal User:'test_user' --allow-host '*' --operation ALL --transactional-id '*'

# 移除授權(quán)(生產(chǎn)慎用)

# ./kafka-acls.sh --authorizer-properties zookeeper.connect=zk_server --remove --allow-principal User:'test_user' --operation Read --topic 'test_topic' --group 'group_test_topic' --allow-host '*'

# ./kafka-acls.sh --authorizer-properties zookeeper.connect=zk_server --remove --allow-principal User:'test_user' --operation Write --topic 'test_topic' --allow-host '*'

# LAG

./kafka-consumer-groups.sh --bootstrap-server broker:port --describe --group group_test_topic

# 消費內(nèi)部topic數(shù)據(jù)

./kafka-console-consumer.sh --topic __consumer_offsets --bootstrap-server broker:port --formatter "kafka.coordinator.group.GroupMetadataManager\$OffsetsMessageFormatter"? --max-messages 10000 > ./group.txt

./kafka-console-consumer.sh --topic __consumer_offsets --partition 1 --bootstrap-server broker:port --formatter "kafka.coordinator.group.GroupMetadataManager\$OffsetsMessageFormatter" --from-beginning

# 設(shè)置到某個時間-新加坡不用調(diào)整時差,國內(nèi)有8h時差家厌,比如:國內(nèi)需要設(shè)置為14:00開始播玖,則:2021-02-25T06:00:00.000

./kafka-consumer-groups.sh --bootstrap-server broker:port --topic test_topic --reset-offsets --to-datetime 2021-06-25T23:00:00.000 --group group_test_topic --execute

# 控制臺生產(chǎn)

./kafka-console-producer.sh --topic resource_author_pond --broker-list broker:port

# 控制臺消費

./kafka-console-consumer.sh --bootstrap-server broker:port --topic test_topic --from-beginning

./kafka-console-consumer.sh --bootstrap-server broker:port --max-messages 100 --topic test_topic > ./records.txt

# 限速相關(guān)

# 用戶-clientId

./kafka-configs.sh --zookeeper=zk_server --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=700,request_percentage=200' --entity-type users --entity-name test_user --entity-type clients --entity-name my_client_id

# 刪除

./kafka-configs.sh --zookeeper=zk_server --alter --delete-config 'producer_byte_rate,consumer_byte_rate,request_percentage' --entity-type users --entity-name test_user --entity-type clients --entity-name my_client_id

# clientId

./kafka-configs.sh --zookeeper=zk_server --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200' --entity-type clients --entity-name clientA

# 刪除

./kafka-configs.sh --zookeeper=zk_server --alter --delete-config 'producer_byte_rate,consumer_byte_rate,request_percentage' --entity-type clients --entity-name my_client_id

# 添加限速[關(guān)于broker建的副本同步]

./kafka-configs.sh --zookeeper=zk_server --alter --add-config 'leader.replication.throttled.rate=629145600,follower.replication.throttled.rate=629145600' --entity-type brokers --entity-name 1

# 必須同步設(shè)置topic

./kafka-configs.sh --zookeeper=zk_server --alter --add-config 'leader.replication.throttled.replicas=*,follower.replication.throttled.replicas=*' --entity-type topics --entity-name test_topic

# 移除限速

./kafka-configs.sh --zookeeper=zk_server --alter --delete-config 'leader.replication.throttled.rate,follower.replication.throttled.rate' --entity-type brokers --entity-name 1

./kafka-configs.sh --zookeeper=zk_server --alter --delete-config 'leader.replication.throttled.replicas,follower.replication.throttled.replicas' --entity-type topics --entity-name test_topic

./kafka-configs.sh --zookeeper=zk_server --alter --delete-config 'consumer_byte_rate' --entity-type users --entity-name test_user --entity-type clients --entity-name 'my_client_id'

# 查看文件內(nèi)容

./kafka-run-class.sh kafka.tools.DumpLogSegments --files ./xxxxx.log --print-data-log | less

## 修改消費組偏移量,最近

./kafka-run-class.sh kafka.admin.ConsumerGroupCommand --bootstrap-server broker:port --describe --group group_test_topic

./kafka-consumer-groups.sh --bootstrap-server broker:port --topic test_topic --reset-offsets --to-earliest --execute --group group_test_topic

## 修改消費組偏移量,任意

./kafka-consumer-groups.sh --bootstrap-server broker:port --topic test_topic --reset-offsets --to-offset 174000 --execute --group group_test_topic

## 修改消費組偏移量,最開始

./kafka-consumer-groups.sh --bootstrap-server broker:port --topic test_topic --reset-offsets --to-latest --execute --group group_test_topic

# 壓測

./kafka-producer-perf-test.sh --topic tiger_test --num-records 5000000000000 --record-size 10240 --throughput 500000 --producer-props bootstrap.servers=broker:port

./kafka-consumer-perf-test.sh --broker-list broker:port --topic test_topic --fetch-size 1048576 --messages 10000000 --threads 1

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市饭于,隨后出現(xiàn)的幾起案子蜀踏,更是在濱河造成了極大的恐慌维蒙,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件脓斩,死亡現(xiàn)場離奇詭異木西,居然都是意外死亡畴栖,警方通過查閱死者的電腦和手機随静,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來吗讶,“玉大人燎猛,你說我怎么就攤上這事≌战裕” “怎么了重绷?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長膜毁。 經(jīng)常有香客問我昭卓,道長,這世上最難降的妖魔是什么瘟滨? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任候醒,我火速辦了婚禮,結(jié)果婚禮上杂瘸,老公的妹妹穿的比我還像新娘倒淫。我一直安慰自己,他們只是感情好败玉,可當(dāng)我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布敌土。 她就那樣靜靜地躺著,像睡著了一般运翼。 火紅的嫁衣襯著肌膚如雪返干。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天血淌,我揣著相機與錄音矩欠,去河邊找鬼。 笑死六剥,一個胖子當(dāng)著我的面吹牛晚顷,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播疗疟,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼该默,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了策彤?” 一聲冷哼從身側(cè)響起栓袖,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤匣摘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后裹刮,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體音榜,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年捧弃,在試婚紗的時候發(fā)現(xiàn)自己被綠了赠叼。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡违霞,死狀恐怖嘴办,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情买鸽,我是刑警寧澤涧郊,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站眼五,受9級特大地震影響妆艘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜看幼,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一批旺、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧桌吃,春花似錦朱沃、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至瑟俭,卻和暖如春翎卓,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背摆寄。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工失暴, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人微饥。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓逗扒,卻偏偏與公主長得像,于是被迫代替她去往敵國和親欠橘。 傳聞我的和親對象是個殘疾皇子矩肩,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容