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 “.*?”
更多的用法请参考正则表达式
相关配置
| 配置 | 描述 | 默认 |
| — | — | — |
| file.delete.delay.ms | topic删除被标记为–delete文件之后延迟多长时间删除正在的Log文件 | 60000 |
| delete.topic.enable | true | 是否能够删除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
".*?"
正则表达式的意思是匹配所有; 您可按需匹配
PS: 当某个Topic的分区少于指定的分区数时候,他会抛出异常;但是不会影响其他Topic正常进行;
相关可选参数
| 参数 | 描述 | 例子 |
| — | — | — |
| --replica-assignment
| 副本分区分配方式;创建topic的时候可以自己指定副本分配情况; | --replica-assignment
BrokerId-0:BrokerId-1:BrokerId-2,BrokerId-1:BrokerId-2:BrokerId-0,BrokerId-2:BrokerId-1:BrokerId-0 ; 这个意思是有三个分区和三个副本,对应分配的Broker; 逗号隔开标识分区;冒号隔开表示副本 |
PS: 虽然这里配置的是全部的分区副本分配配置,但是正在生效的是新增的分区;
比如: 以前3分区1副本是这样的
| Broker-1 | Broker-2 | Broker-3 | Broker-4 |
| — | — | — | — |
| 0 | 1 | 2 | |
现在新增一个分区,--replica-assignment
2,1,3,4 ; 看这个意思好像是把0,1号分区互相换个Broker
| Broker-1 | Broker-2 | Broker-3 | Broker-4 |
| — | — | — | — |
| 1 | 0 | 2 | 3 |
但是实际上不会这样做,Controller在处理的时候会把前面3个截掉; 只取新增的分区分配方式,原来的还是不会变
| Broker-1 | Broker-2 | Broker-3 | Broker-4 |
| — | — | — | — |
| 0 | 1 | 2 | 3 |
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 用双引号包裹起来
相关可选参数
| 参数 | 描述 | 例子 |
| — | — | — |
| --bootstrap-server
指定kafka服务 | 指定连接到的kafka服务; 如果有这个参数,则 --zookeeper
可以不需要 | –bootstrap-server localhost:9092 |
| --at-min-isr-partitions
| 查询的时候省略一些计数和配置信息 | --at-min-isr-partitions
|
| --exclude-internal
| 排除kafka内部topic,比如__consumer_offsets-*
| --exclude-internal
|
| --topics-with-overrides
| 仅显示已覆盖配置的主题,也就是单独针对Topic设置的配置覆盖默认配置;不展示分区信息 | --topics-with-overrides
|
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_.*"
相关可选参数
| 参数 | 描述 | 例子 |
| — | — | — |
| --exclude-internal
| 排除kafka内部topic,比如__consumer_offsets-*
| --exclude-internal
|
| --topic
| 可以正则表达式进行匹配,展示topic名称 | --topic
|
Config相关操作; 动态配置可以覆盖默认的静态配置;
2.1 查询配置
Topic配置查询
展示关于Topic的动静态配置
1.查询单个Topic配置(只列举动态配置)
sh bin/kafka-configs.sh --describe --bootstrap-server xxxxx:9092 --topic test_create_topic
或者
sh bin/kafka-configs.sh --describe --bootstrap-server 172.23.248.85:9092 --entity-type topics --entity-name test_create_topic
2.查询所有Topic配置(包括内部Topic)(只列举动态配置)
sh bin/kafka-configs.sh --describe --bootstrap-server 172.23.248.85:9092 --entity-type topics
3.查询Topic的详细配置(动态+静态)
只需要加上一个参数
--all
其他配置/clients/users/brokers/broker-loggers 的查询
同理 ;只需要将
--entity-type
改成对应的类型就行了 (topics/clients/users/brokers/broker-loggers)
查询kafka版本信息
sh bin/kafka-configs.sh --describe --bootstrap-server xxxx:9092 --version
所有可配置的动态配置 请看最后面的 附件 部分
2.2 增删改 配置 --alter
–alter
删除配置: --delete-config
k1=v1,k2=v2
添加/修改配置: --add-config
k1,k2
选择类型: --entity-type
(topics/clients/users/brokers/broker-
loggers)
类型名称: --entity-name
Topic添加/修改动态配置
--add-config
sh bin/kafka-configs.sh --bootstrap-server xxxxx:9092 --alter --entity-type topics --entity-name test_create_topic1 --add-config file.delete.delay.ms=222222,retention.ms=999999
Topic删除动态配置
--delete-config
sh bin/kafka-configs.sh --bootstrap-server xxxxx:9092 --alter --entity-type topics --entity-name test_create_topic1 --delete-config file.delete.delay.ms,retention.ms
添加/删除配置同时执行
sh bin/kafka-configs.sh --bootstrap-server xxxxx:9092 --alter --entity-type brokers --entity-default --add-config log.segment.bytes=788888888 --delete-config log.retention.ms