分布式集群Kafka删除已经创建的topic

时间:2022-11-03 22:40:47

应用场景

按照Kafka集群的测试和简单试用博文进行了对Kafka的使用,与topic创建,并且模拟了消息的生产者,消息的消费者进行模拟环境,验证Kafka可以执行,但是想到,建立了topic,如何进行删掉呢?这是一个问题。

操作步骤

方法一:修改配置文件,通过命令删除

修改配置文件server.properties

 # vim /opt/kafka2.11/config/server.properties

delete.topic.enable=true #添加这条配置

注:如果想删除topic,此项配置必须为true,默认为false。配置完重启kafka、zookeeper。

#然后,才能通过命令删除topic

# kafka-topics --delete --zookeeper 【zookeeper server】 --topic 【topic name】

方法二:不修改配置文件,直接强制删除

1、删除kafka存储目录/tmp/kafka-logs下对应的topic。【注:删除所有节点的topic目录】
2、进入zookeeper客户端删掉对应topic
# zkCli.sh -server 127.0.0.1:2181
找到topic目录
ls /brokers/topics
删掉对应topic
rmr /brokers/topic/topic-name
   找到目录
ls /config/topics
删掉对应topic
rmr /config/topics/topic-name
[root@hadoop2 bin]# zkCli.sh -server 192.168.210.70:2181
Connecting to 192.168.210.70:2181
2017-11-23 17:19:11,418 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2017-11-23 17:19:11,421 [myid:] - INFO [main:Environment@100] - Client environment:host.name=hadoop2
2017-11-23 17
:19:11,421 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_152
2017-11-23 17
:19:11,423 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-11-23 17
:19:11,423 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/opt/jdk1.8/jre
2017-11-23 17
:19:11,423 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/opt/zookeeper3.4.10/bin/../build/classes:/opt/zookeeper3.4.10/bin/../build/lib/*.jar:/opt/zookeeper3.4.10/bin/../lib/slf4j-log4j12-1.6.1.jar:/opt/zookeeper3.4.10/bin/../lib/slf4j-api-1.6.1.jar:/opt/zookeeper3.4.10/bin/../lib/netty-3.10.5.Final.jar:/opt/zookeeper3.4.10/bin/../lib/log4j-1.2.16.jar:/opt/zookeeper3.4.10/bin/../lib/jline-0.9.94.jar:/opt/zookeeper3.4.10/bin/../zookeeper-3.4.10.jar:/opt/zookeeper3.4.10/bin/../src/java/lib/*.jar:/opt/zookeeper3.4.10/bin/../conf:.:/opt/jdk1.8/lib/dt.jar:/opt/jdk1.8/lib/tools.jar
2017-11-23 17
:19:11,423 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2017-11-23 17:19:11,424 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-11-23 17
:19:11,424 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2017-11-23 17
:19:11,424 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2017-11-23 17
:19:11,424 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2017-11-23 17
:19:11,424 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-327.el7.x86_64
2017-11-23 17
:19:11,424 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2017-11-23 17
:19:11,424 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2017-11-23 17
:19:11,424 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/opt/kafka2.11/bin
2017-11-23 17
:19:11,425 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=192.168.210.70:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@446cdf90
Welcome to ZooKeeper!
2017-11-23 17
:19:11,446 [myid:] - INFO [main-SendThread(192.168.210.70:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 192.168.210.70/192.168.210.70:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2017-11-23 17:19:11,501 [myid:] - INFO [main-SendThread(192.168.210.70:2181):ClientCnxn$SendThread@876] - Socket connection established to 192.168.210.70/192.168.210.70:2181, initiating session
2017-11-23 17:19:11,508 [myid:] - INFO [main-SendThread(192.168.210.70:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 192.168.210.70/192.168.210.70:2181, sessionid = 0x15fe7e814520010, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: 192.168.210.70:2181(CONNECTED) 0] ls /brokers/topics
[xubin, xubin1]
[zk: 192.168.210.70:2181(CONNECTED) 1] ls /config/topics
[xubin, xubin1]
[zk: 192.168.210.70:2181(CONNECTED) 2]

注:两步全部执行才算彻底删除topic