kafka工具kafka-topic.sh

朱清震發表於2017-10-08

kafka 工具 kafka-topic.sh

操作

kafka-topic.sh有6種操作:

建立topic – create

[lch@linux129 bin]$ ./kafka-topics.sh --create --zookeeper ip:2181,ip:2181 --replication-factor 2 --partitions 20 --topic TEST
引數說明:
--replication-factor    : 表明 副本的個數, 這裡寫成2,表明會有兩個副本
--partitions              :  表明會有20個partition, 表現在log日誌中,就是有20個資料夾,名稱以“topic-編號” ,編號從0~19

執行完成後去kafka資料目錄下檢視,會看到建立了兩個資料夾
    test-0
    test-1
    test-2

注: 在leader節點上面才是20個資料夾,如果是從節點上面,可能只有一個也可能沒有

列出全部的topic –list命令

[root@tingyun bin]# ./kafka-topics.sh --zookeeper=localhost:2181  --list
__consumer_offsets
crashMessage
mobileActions
mobileActionsWrappedMessages
mobileErrors
mobileErrorsWrappedMessages
mobileInteractionTraces
mobileInteractionTracesWrappedMessages
mobileReportUsers
mobileReportUsersWrappedMessages

列出全部topic的資訊 –describe

包括每個分割槽的leader,副本,isr資訊

[root@tingyun bin]# ./kafka-topics.sh --zookeeper=localhost:2181  --describe
Topic:__consumer_offsets    PartitionCount:50   ReplicationFactor:3 Configs:segment.bytes=104857600,cleanup.policy=compact,compression.type=uncompressed
    Topic: __consumer_offsets   Partition: 0    Leader: 4   Replicas: 4,0,1 Isr: 0,1,4
    Topic: __consumer_offsets   Partition: 1    Leader: 0   Replicas: 0,1,2 Isr: 0,1,2
    Topic: __consumer_offsets   Partition: 2    Leader: 1   Replicas: 1,2,3 Isr: 1,3,2
    Topic: __consumer_offsets   Partition: 3    Leader: 2   Replicas: 2,3,4 Isr: 3,4,2
    Topic: __consumer_offsets   Partition: 4    Leader: 3   Replicas: 3,4,0 Isr: 0,3,4
    Topic: __consumer_offsets   Partition: 5    Leader: 4   Replicas: 4,1,2 Isr: 1,4,2
    Topic: __consumer_offsets   Partition: 6    Leader: 0   Replicas: 0,2,3 Isr: 0,3,2
    Topic: __consumer_offsets   Partition: 7    Leader: 1   Replicas: 1,3,4 Isr: 1,3,4
    Topic: __consumer_offsets   Partition: 8    Leader: 2   Replicas: 2,4,0 Isr: 0,4,2
    Topic: __consumer_offsets   Partition: 9    Leader: 3   Replicas: 3,0,1 Isr: 0,1,3
    Topic: __consumer_offsets   Partition: 10   Leader: 4   Replicas: 4,2,3 Isr: 3,4,2
    Topic: __consumer_offsets   Partition: 11   Leader: 0   Replicas: 0,3,4 Isr: 0,3,4
    Topic: __consumer_offsets   Partition: 12   Leader: 0   Replicas: 1,4,0 Isr: 0,1,4
    Topic: __consumer_offsets   Partition: 13   Leader: 2   Replicas: 2,0,1 Isr: 0,1,2
    Topic: __consumer_offsets   Partition: 14   Leader: 3   Replicas: 3,1,2 Isr: 1,3,2
    Topic: __consumer_offsets   Partition: 15   Leader: 4   Replicas: 4,3,0 Isr: 0,3,4
    Topic: __consumer_offsets   Partition: 16   Leader: 0   Replicas: 0,4,1 Isr: 0,1,4
    Topic: __consumer_offsets   Partition: 17   Leader: 0   Replicas: 1,0,2 Isr: 0,1,2
    Topic: __consumer_offsets   Partition: 18   Leader: 2   Replicas: 2,1,3 Isr: 1,3,2
    Topic: __consumer_offsets   Partition: 19   Leader: 3   Replicas: 3,2,4 Isr: 3,4,2
    Topic: __consumer_offsets   Partition: 20   Leader: 4   Replicas: 4,0,1 Isr: 0,1,4
    Topic: __consumer_offsets   Partition: 21   Leader: 0   Replicas: 0,1,2 Isr: 0,1,2
    Topic: __consumer_offsets   Partition: 22   Leader: 1   Replicas: 1,2,3 Isr: 1,3,2
    Topic: __consumer_offsets   Partition: 23   Leader: 2   Replicas: 2,3,4 Isr: 3,4,2
    Topic: __consumer_offsets   Partition: 24   Leader: 3   Replicas: 3,4,0 Isr: 0,3,4
    Topic: __consumer_offsets   Partition: 25   Leader: 4   Replicas: 4,1,2 Isr: 1,4,2
    Topic: __consumer_offsets   Partition: 26   Leader: 0   Replicas: 0,2,3 Isr: 0,3,2
    Topic: __consumer_offsets   Partition: 27   Leader: 1   Replicas: 1,3,4 Isr: 1,3,4
    Topic: __consumer_offsets   Partition: 28   Leader: 2   Replicas: 2,4,0 Isr: 0,4,2
    Topic: __consumer_offsets   Partition: 29   Leader: 3   Replicas: 3,0,1 Isr: 0,1,3
    Topic: __consumer_offsets   Partition: 30   Leader: 4   Replicas: 4,2,3 Isr: 3,4,2
    Topic: __consumer_offsets   Partition: 31   Leader: 0   Replicas: 0,3,4 Isr: 0,3,4
    Topic: __consumer_offsets   Partition: 32   Leader: 0   Replicas: 1,4,0 Isr: 0,1,4
    Topic: __consumer_offsets   Partition: 33   Leader: 2   Replicas: 2,0,1 Isr: 0,1,2
    Topic: __consumer_offsets   Partition: 34   Leader: 3   Replicas: 3,1,2 Isr: 1,3,2
    Topic: __consumer_offsets   Partition: 35   Leader: 4   Replicas: 4,3,0 Isr: 0,3,4
    Topic: __consumer_offsets   Partition: 36   Leader: 0   Replicas: 0,4,1 Isr: 0,1,4
    Topic: __consumer_offsets   Partition: 37   Leader: 0   Replicas: 1,0,2 Isr: 0,1,2
    Topic: __consumer_offsets   Partition: 38   Leader: 2   Replicas: 2,1,3 Isr: 1,3,2
    Topic: __consumer_offsets   Partition: 39   Leader: 3   Replicas: 3,2,4 Isr: 3,4,2
    Topic: __consumer_offsets   Partition: 40   Leader: 4   Replicas: 4,0,1 Isr: 0,1,4
    Topic: __consumer_offsets   Partition: 41   Leader: 0   Replicas: 0,1,2 Isr: 0,1,2
    Topic: __consumer_offsets   Partition: 42   Leader: 1   Replicas: 1,2,3 Isr: 1,3,2
    Topic: __consumer_offsets   Partition: 43   Leader: 2   Replicas: 2,3,4 Isr: 3,4,2
    Topic: __consumer_offsets   Partition: 44   Leader: 3   Replicas: 3,4,0 Isr: 0,3,4
    Topic: __consumer_offsets   Partition: 45   Leader: 4   Replicas: 4,1,2 Isr: 1,4,2
    Topic: __consumer_offsets   Partition: 46   Leader: 0   Replicas: 0,2,3 Isr: 0,3,2
    Topic: __consumer_offsets   Partition: 47   Leader: 1   Replicas: 1,3,4 Isr: 1,3,4
    Topic: __consumer_offsets   Partition: 48   Leader: 2   Replicas: 2,4,0 Isr: 0,4,2
    Topic: __consumer_offsets   Partition: 49   Leader: 3   Replicas: 3,0,1 Isr: 0,1,3
Topic:TEST  PartitionCount:20   ReplicationFactor:1 Configs:
    Topic: TEST Partition: 0    Leader: 4   Replicas: 4 Isr: 4
    Topic: TEST Partition: 1    Leader: 0   Replicas: 0 Isr: 0
    Topic: TEST Partition: 2    Leader: 1   Replicas: 1 Isr: 1
    Topic: TEST Partition: 3    Leader: 2   Replicas: 2 Isr: 2
    Topic: TEST Partition: 4    Leader: 3   Replicas: 3 Isr: 3
    Topic: TEST Partition: 5    Leader: 4   Replicas: 4 Isr: 4
    Topic: TEST Partition: 6    Leader: 0   Replicas: 0 Isr: 0
    Topic: TEST Partition: 7    Leader: 1   Replicas: 1 Isr: 1
    Topic: TEST Partition: 8    Leader: 2   Replicas: 2 Isr: 2
    Topic: TEST Partition: 9    Leader: 3   Replicas: 3 Isr: 3
    Topic: TEST Partition: 10   Leader: 4   Replicas: 4 Isr: 4
    Topic: TEST Partition: 11   Leader: 0   Replicas: 0 Isr: 0
    Topic: TEST Partition: 12   Leader: 1   Replicas: 1 Isr: 1
    Topic: TEST Partition: 13   Leader: 2   Replicas: 2 Isr: 2
    Topic: TEST Partition: 14   Leader: 3   Replicas: 3 Isr: 3
    Topic: TEST Partition: 15   Leader: 4   Replicas: 4 Isr: 4
    Topic: TEST Partition: 16   Leader: 0   Replicas: 0 Isr: 0
    Topic: TEST Partition: 17   Leader: 1   Replicas: 1 Isr: 1
    Topic: TEST Partition: 18   Leader: 2   Replicas: 2 Isr: 2
    Topic: TEST Partition: 19   Leader: 3   Replicas: 3 Isr: 3

刪除topic –delete

 [root@hadoop221 bin]# ./kafka-topics.sh  --zookeeper ip:port  --topic TEST  --delete

topic無法刪掉
Topic TEST is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.

需要增加配置引數:delete.topic.enable=true

修改topic時配置引數 –alter –config

覆蓋已經有topic引數

 [root@hadoop221 bin]# ./kafka-topics.sh --zookeeper zk_host:port  --topic TEST --alter --config flush.messages=1

刪除topic級別配置引數

 [root@hadoop221 bin]# ./kafka-topics.sh --zookeeper zk_host:port --alter --topic TEST --delete-config flush.messages=1

修改topic分割槽數量

 [root@hadoop221 bin]# ./kafka-topics.sh --zookeeper zk_host:port --alter --topic TEST --partitions 6

相關文章