若代理設置了 auto.create.topics.enable=true,這樣還未建立topic就往kafka發送消息時,bash
會自動建立一個 ${num.partitions}個分區和{default.replication.factor}個副本的主題app
建立主題ide
1.建立一個名稱爲「kafka-action」的主題,該主題有兩個副本,3個分區:線程
[root@docp-n bin]# ./kafka-topics.sh --create --zookeeper docp-n:2181 --replication-factor 2 --partitions 3 --topic kafka-action Created topic "kafka-action".
*docp-n:2181 zookeeper所在的ip3d
zookeeper 必傳參數,多個zookeeper用 ‘,’分開。代理
partitions 用於設置主題分區數,每一個線程處理一個分區數據orm
replication-factor 用於設置主題副本數,每一個副本分佈在不通節點,不能超過總結點數。如你只有一個節點,可是建立時指定副本數爲2,就會報錯。server
建立主題時,還能夠經過config參數設置主題幾倍的配置以覆蓋默認配置blog
例如:ip
kafka-topics.sh --create --zookeeper server-1:2181,server-2:2181 --replacation-factor 2 --partitions 3 --topic config-test --config max.messege.bytes=404800
經過zookeeper客戶端能夠看到覆蓋的相關配置。get /config/topics/config-test
[root@haha bin]# /data/app/zookeeper/bin/zkCli.sh -server localhost:2181 [zk: localhost:2181(CONNECTED) 2] get /brokers/topics/test2 {"version":1,"partitions":{"1":[5],"0":[5]}} cZxid = 0x25a ctime = Thu Nov 01 15:25:29 CST 2018 mZxid = 0x25a mtime = Thu Nov 01 15:25:29 CST 2018 pZxid = 0x25d cversion = 1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 44 numChildren = 1
能夠登陸zookeeper客戶端查看所建立topic元數據信息,「kafka-action」
[root@docp-n bin]# /data/app/zookeeper/bin/zkCli.sh -server 127.0.0.1:2181 Connecting to docp-n:2181 Welcome to ZooKeeper! JLine support is enabled [zk: docp-n:2181(CONNECTING) 0] WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: docp-n:2181(CONNECTED) 0] ls /brokers/topics/kafka-action/partitions [0, 1, 2]
2.刪除kafka主題
(1)手動刪除log.dir目錄下的分區目錄,並刪除zookeeper客戶端對應的節點,/broker/topics/ && /config/topics
(2) 執行 kafka-topics.sh 腳本刪除,若是想經過腳本完全刪除,須要修改啓動時加載的配置文件 server.propers中
delete-topic-enable=true 默認爲false,不然並未真正刪除
[root@docp-n bin]# ./kafka-topics.sh --delete --zookeeper docp-n:2181 --topic test Topic test is marked for deletion. Note: This will have no impact if delete.topic.enable is not set to true.
注意控制檯打印的消息
3.刪除主題
[root@docp-n bin]# ../bin/kafka-topics.sh --delete --zookeeper 127.0.0.1:2181 --topic test Topic test is marked for deletion. Note: This will have no impact if delete.topic.enable is not set to true.
4.查看主題
(1)查看全部主題
[root@docp-n bin]# ./kafka-topics.sh --list --zookeeper 127.0.0.1:2181 1ab42b8d882347ef81a19914440bc308 286a9b791be54ca7bda4b65530fde8b4 2dfb690e5a514398879894a56f081520 414a2a3e915b4fd6811a77e6cadf75be 87a9ba31db734577bef86143e95a8b44 __consumer_offsets b1fe8eb154f5493e90e939dff8031e71 f53def0af82344b796529ba7ab79fe66 hello_topic kafka-action log-format test
(2) 查看某個特定主題信息,不指定topic則查詢全部
經過 --describe
[root@docp-n bin]# ./kafka-topics.sh --describe --zookeeper 127.0.0.1:2181 --topic kafka-action Topic:kafka-action PartitionCount:3 ReplicationFactor:2 Configs: Topic: kafka-action Partition: 0 Leader: 1 Replicas: 1,5 Isr: 1,5 Topic: kafka-action Partition: 1 Leader: 5 Replicas: 5,0 Isr: 5,0 Topic: kafka-action Partition: 2 Leader: 0 Replicas: 0,1 Isr: 0,1
(3)查看正在同步的主題
經過 --describe 和 under-replicated-partitions命令組合查看 under-replacation狀態
[root@docp-n bin]# ./kafka-topics.sh --describe --zookeeper 127.0.0.1:2181 --under-replicated-partitions
(4)查看主題覆蓋的配置
經過 describe 和 topics-with-overrides 組合
[root@docp-n bin]# ./kafka-topics.sh --describe --zookeeper 127.0.0.1:2181 --topics-with-override Topic:__consumer_offsets PartitionCount:50 ReplicationFactor:1 Configs:segment.bytes=104857600,cleanup.policy=compact,compression.type=producer