kafka相關常用命令
查看所有topic詳細信息
./kafka-topics.sh --describe --zookeeper 127.0.0.1:2181
只查看有多少topic
./kafka-topics.sh --list --zookeeper 127.0.0.1:2181
查看具體的topic
./kafka-topics.sh --describe --zookeeper 127.0.0.1:2181 --topic test-kafka
刪除topic
kafka啟動之前,在server.properties配置delete.topic.enable=true
./kafka-topics.sh --delete --zookeeper 127.0.0.1:2181 --topic xxxxx
查看消費幾條
sh kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list '127.0.0.1:2181 :6667' --topic 'test-kafka' --time -1
增加partition
sh kafka-topics.sh --alter --zookeeper 127.0.0.1:2181:2181 --partitions 2 --topic one-kafka
運行消費者
./kafka-console-consumer.sh --zookeeper 127.0.0.1:2181:2181 --topic test-01
從一開始就消費
./kafka-console-consumer.sh --zookeeper 127.0.0.1:2181:2181 --topic test-01 --from beginning
運行生產者
./kafka-console-producer.sh --broker-list 127.0.0.1:2181:6667 --topic test-01
創建topic
./kafka-topics.sh --create --zookeeper 127.0.0.1:2181:2181 --topic test-01 --partitions 5 --replication-factor 5
解釋
bin/kafka-topics.sh --describe --zookeeper 127.0.0.1:2181 --topic test0
運行結果:
Topic:test0 PartitionCount:4 ReplicationFactor:3 Configs:
Topic: test0 Partition: 0 Leader: 0 Replicas: 0,2,1 Isr: 1,0,2
Topic: test0 Partition: 1 Leader: 1 Replicas: 1,2,0 Isr: 1,0,2
Topic: test0 Partition: 2 Leader: 2 Replicas: 2,0,1 Isr: 1,0,2
Topic: test0 Partition: 3 Leader: 1 Replicas: 1,2,0 Isr: 1,0,2
結果分析:
第一行顯示partitions的概況,列出了Topic名字,partition總數,存儲這些partition的broker數
以下每一行都是其中一個partition的詳細信息:
leader
是該partitons所在的所有broker中擔任leader的broker id,每個broker都有可能成為leader
replicas
顯示該partiton所有副本所在的broker列表,包括leader,不管該broker是否是存活,不管是否和leader保持了同步。
isr
in-sync replicas的簡寫,表示存活且副本都已同步的的broker集合,是replicas的子集
舉例:
比如上面結果的第一行:Topic: test0 Partition:0 Leader: 0 Replicas: 0,2,1 Isr: 1,0,2
Partition: 0
該partition編號是0
Replicas: 0,2,1
代表partition0 在broker0,broker1,broker2上保存了副本
Isr: 1,0,2
代表broker0,broker1,broker2都存活而且目前都和leader保持同步
Leader: 0
代表保存在broker0,broker1,broker2上的這三個副本中,leader是broker0
leader負責讀寫,broker1、broker2負責從broker0同步信息,平時沒他倆什麼事
當producer發送一個消息時,producer自己會判斷髮送到哪個partiton上,如果發到了partition0上,消息會發到leader,也就是broker0上,broker0處理這個消息,broker1、broker2從broker0同步這個消息
如果這個broker0掛了,那麼kafka會在Isr列表裡剩下的broker1、broker2中選一個新的leader
閱讀更多 小民學歷史 的文章