1 topic相关

1.1 列出集群所有可用topic

1
2
3
$ bin/kafka-topics.sh --list \--zookeeper localhost:2181
__consumer_offsets
tp-test

1.2 查看指定topic信息

1
2
3
$ bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic tp-test
Topic:tp-test PartitionCount:1 ReplicationFactor:1 Configs:
Topic: tp-test Partition: 0 Leader: 1 Replicas: 1 Isr: 1

1.3 创建topic

1
2
3
4
5
6
$ bin/kafka-topics.sh --create \
--zookeeper localhost:2181 \
--replication-factor 1 \ # 备份因子
--partitions 1 \ # 分区数
--topic topic01 # topic名
Created topic "topic01".

1.4 增加分区数

  • 将topic01的分区数增加至5(只能增加不能减少)
1
2
3
4
5
$ bin/kafka-topics.sh \
--zookeeper localhost:2181 \
--alter \
--topic topic01 \
--partitions 5
  • 查看结果
1
2
3
4
5
6
7
$ bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic topic01
Topic:topic01 PartitionCount:5 ReplicationFactor:1 Configs:
Topic: topic01 Partition: 0 Leader: 1 Replicas: 1 Isr: 1
Topic: topic01 Partition: 1 Leader: 1 Replicas: 1 Isr: 1
Topic: topic01 Partition: 2 Leader: 1 Replicas: 1 Isr: 1
Topic: topic01 Partition: 3 Leader: 1 Replicas: 1 Isr: 1
Topic: topic01 Partition: 4 Leader: 1 Replicas: 1 Isr: 1

1.5 删除topic

要使用命令真正彻底删除topic,需要在server.properties中启用delete.topic.enable。该选项默认是注释掉的。

1
2
3
4
bin/kafka-topics.sh \
--delete \
--zookeeper localhost:2181 \
--topic topic01

如果命令删除有问题的话,也可以直接使用以下比较暴力的方式:

  • 从zookeeper删除节点/brokers/topics/<topic_name>
  • 从磁盘上删除partition信息