Kafka支持的基本命令位於${KAFKA_HOME}/bin文件夾中,主要是kafka-topics.sh命令;Kafka命令參考頁面: kafka-0.8.x-幫助文檔
-1. 查看幫助信息
bin/kafka-topics.sh --help
-2. 創建Topic
bin/kafka-topics.sh --create --topic test0 --zookeeper 192.168.187.146:2181 --config max.message.bytes=12800000 --config flush.messages=1 --partitions 5 --replication-factor 1
--create: 指定創建topic動作
--topic:指定新建topic的名稱
--zookeeper: 指定kafka連接zk的連接url,該值和server.properties文件中的配置項{zookeeper.connect}一樣
--config:指定當前topic上有效的參數值,參數列表參考文檔為: Topic-level configuration
--partitions:指定當前創建的kafka分區數量,默認為1個
--replication-factor:指定每個分區的復制因子個數,默認1個
-3. 查看當前Kafka集群中Topic的情況
bin/kafka-topics.sh --list --zookeeper 192.168.187.146:2181
-4. 查看對應topic的描述信息
bin/kafka-topics.sh --describe --zookeeper 192.168.187.146:2181 --topic test0
--describe: 指定是展示詳細信息命令
--zookeeper: 指定kafka連接zk的連接url,該值和server.properties文件中的配置項{zookeeper.connect}一樣
--topic:指定需要展示數據的topic名稱
-5. Topic信息修改
bin/kafka-topics.sh --zookeeper 192.168.187.146:2181 --alter --topic test0 --config max.message.bytes=128000
bin/kafka-topics.sh --zookeeper 192.168.187.146:2181 --alter --topic test0 --delete-config max.message.bytes
bin/kafka-topics.sh --zookeeper 192.168.187.146:2181 --alter --topic test0 --partitions 10
bin/kafka-topics.sh --zookeeper 192.168.187.146:2181 --alter --topic test0 --partitions 3 ## Kafka分區數量只允許增加,不允許減少
-6. Topic刪除
默認情況下Kafka的Topic是沒法直接刪除的,需要進行相關參數配置
bin/kafka-topics.sh --delete --topic test0 --zookeeper 192.168.187.146:2181
Note: This will have no impact if delete.topic.enable is not set to true.## 默認情況下,刪除是標記刪除,沒有實際刪除這個Topic;如果運行刪除Topic,兩種方式:
方式一:通過delete命令刪除后,手動將本地磁盤以及zk上的相關topic的信息刪除即可
方式二:配置server.properties文件,給定參數delete.topic.enable=true,重啟kafka服務,此時執行delete命令表示允許進行Topic的刪除