1. 程式人生 > >kafka如何徹底刪除topic

kafka如何徹底刪除topic

網上都說用kafka-run-class.shkafka.admin.DeleteTopicCommand 命令刪除topic,但是並沒有成功,用kafka-topics.sh命令檢視依然可以檢視到topic,應該怎樣才能徹底刪除topic?

一、**kafka 0.8.1.1以及之前版本**
都無法使用類似一條命令就徹底刪除topic,此命令不過只是在zookeeper登出資訊而已,但是實際的日誌內容還是儲存在kafka log中,如果想徹底刪除topic,過程如下:
1、從zookeerer刪除資訊
./bin/kafka-run-class.sh kafka.admin.DeleteTopicCommand --zookeeper 10.0.1.10:2181,10.0.1.11:2181,10.0.1.12:2181 --topic test成功後返回資訊:deletion succeeded!
2、利用JPS命令檢視kafka和zookeeper程序,kill掉QuorumPeerMain和Kafka程序
3、從kafka的log.dirs目錄刪除檔案,可以看到多個子目錄名字如test-0,test-1…test-n(就是你topic的partition個數)
進入到kafka的log.dirs目錄,執行
rm –fr test-0……test-n(4)  修改日誌目錄的recovery-point-offset-checkpoint和replication-offset-checkpoint檔案(要小心刪除,否則待會kafka不能正常啟動起來)
replication-offset-checkpoint格式如下:
0
4(partition總數)
test 0 0
test 3 0
hehe 0 0
hehe 1 0
修改後如下:
0
2(partition總數)
hehe 0 0
hehe 1 0
把含有test行全部去掉,並且把partition總數修改為減去test的partition的剩餘數目,同理recovery-point-offset-checkpoint也是這樣修改。
完成後就可以正常啟動zookeeper和kafka。

注意:如果topic很多的話,手動操作很複雜,建議可以停止kafka後將本地的kafka-log目錄內容刪除,之後啟動kafka,
這樣省的一個一個刪除目錄和修改recovery-point-offset-checkpoint和replication-offset-checkpoint檔案(建議刪除前備份一份檔案)
config/server.properties 檔案中配置
log.dirs=/data1/kafka-log,/data2/kafka-log
操作:rm -rf /data1/kafka-log/* /data2/kafka-log/*

二、**從kafka 0.8.2.1**開始可以直接刪除topic

步驟如下:

kafka 0.8.1.1之前版本,kafka-topics.sh命令連--delete                                Delete a topic引數都沒有,如下:
# ./kafka-topics.sh --help
Command must include exactly one action: --list, --describe, --create or --alter
Option Description
------ -----------
--alter Alter the configuration for the topic.
--config <name=value> A topic configuration override for the
topic being created or altered.
--create Create a new topic.
--deleteConfig <name> A topic configuration override to be
removed for an existing topic
--describe List details for the given topics.
--help Print usage information.
--list List all available topics.
--partitions <Integer: # of partitions> The number of partitions for the topic
being created or altered (WARNING:
If partitions are increased for a
topic that has a key, the partition
logic or ordering of the messages
will be affected
--replica-assignment A list of manual partition-to-broker
<broker_id_for_part1_replica1 : assignments for the topic being
broker_id_for_part1_replica2 , created or altered.
broker_id_for_part2_replica1 :
broker_id_for_part2_replica2 , ...>
--replication-factor <Integer: The replication factor for each
replication factor> partition in the topic being created.
--topic <topic> The topic to be create, alter or
describe. Can also accept a regular
expression except for --create option
--topics-with-overrides if set when describing topics, only
show topics that have overridden
configs
--unavailable-partitions if set when describing topics, only
show partitions whose leader is not
available
--under-replicated-partitions if set when describing topics, only
show under replicated partitions
--zookeeper <urls> REQUIRED: The connection string for
the zookeeper connection in the form
host:port. Multiple URLS can be
given to allow fail-over.

#**而kafka 0.8.2.1**新增加了這個引數
./kafka-topics.sh --help 中出現
--delete引數

刪除過程:
1、在kafka配置檔案中新增刪除引數
delete.topic.enable=true2、利用命令刪除需要刪除的topic
bin/kafka-topics.sh --zookeeper zk_host:port/chroot --delete --topic my_topic_name

參考:http://www.cnblogs.com/the-tops/p/5909781.html