文章目录


1.TopicCommand

1.1.Topic创建

bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 3 --topic test

相关可选参数:

Kafka_TopicCommand命令详解_分布式

1.2.删除Topic

bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic test

支持正则表达式匹配Topic来进行删除,只需要将topic 用双引号包裹起来 例如: 删除以create_topic_byhand_zk为开头的topic;

bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic "create_topic_byhand_zk.*" 

.表示任意匹配除换行符 \n 之外的任何单字符。要匹配 . ,请使用 . 。·*·:匹配前面的子表达式零次或多次。要匹配 * 字符,请使用 。. : 任意字符

删除任意Topic

bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic ".*?"

1.3.Topic分区扩容

分区只可以扩容不可以缩容

副本不可以通过命令行改变,

zk方式(不推荐)

bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic topic1 --partitions 2

kafka版本 >= 2.2 支持下面方式(推荐)

单个Topic扩容

bin/kafka-topics.sh --bootstrap-server broker_host:port --alter --topic test_create_topic1 --partitions 4

批量扩容 (将所有正则表达式匹配到的Topic分区扩容到4个)

sh bin/kafka-topics.sh --topic ".*?" --bootstrap-server 172.23.248.85:9092 --alter --partitions 4

当某个Topic的分区少于指定的分区数时候,他会抛出异常;但是不会影响其他Topic正常进行;

参数说明:

Kafka_TopicCommand命令详解_bootstrap_02
虽然这里配置的是全部的分区副本分配配置,但是正在生效的是新增的分区; 比如: 以前3分区1副本是这样的

Kafka_TopicCommand命令详解_分布式_03
现在新增一个分区,–replica-assignment 2,1,3,4 ; 看这个意思好像是把0,1号分区互相换个Broker

Kafka_TopicCommand命令详解_分布式_04
但是实际上不会这样做,Controller在处理的时候会把前面3个截掉; 只取新增的分区分配方式,原来的还是不会变

Kafka_TopicCommand命令详解_bootstrap_05

1.4.查询Topic描述

1.查询单个Topic

sh bin/kafka-topics.sh --topic test --bootstrap-server xxxx:9092 --describe --exclude-internal

2.批量查询Topic(正则表达式匹配,下面是查询所有Topic)

sh bin/kafka-topics.sh --topic ".*?" --bootstrap-server xxxx:9092 --describe --exclude-internal

支持正则表达式匹配Topic,只需要将topic 用双引号包裹起来

参数说明:

Kafka_TopicCommand命令详解_bootstrap_06

1.5 查询Topic列表

1.查询所有Topic列表

sh bin/kafka-topics.sh --bootstrap-server xxxxxx:9092 --list --exclude-internal

2.查询匹配Topic列表(正则表达式)

查询test_create_开头的所有Topic列表 sh bin/kafka-topics.sh --bootstrap-server xxxxxx:9092 --list --exclude-internal --topic "test_create_.*"

Kafka_TopicCommand命令详解_bootstrap_07