Kafka_TopicCommand命令详解

33 篇文章 0 订阅
17 篇文章 0 订阅
本文档详细介绍了Kafka中Topic的管理操作,包括创建、删除、分区扩容、查询描述和查询列表。提供了相应的命令行示例,如使用`kafka-topics.sh`脚本进行操作,并解释了相关参数的意义和注意事项,如分区只能扩容不能缩容,以及查询Topic描述和列表的方法。
摘要由CSDN通过智能技术生成

1.TopicCommand

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

相关可选参数:

在这里插入图片描述

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正常进行;

参数说明:

在这里插入图片描述
虽然这里配置的是全部的分区副本分配配置,但是正在生效的是新增的分区; 比如: 以前3分区1副本是这样的

在这里插入图片描述
现在新增一个分区,–replica-assignment 2,1,3,4 ; 看这个意思好像是把0,1号分区互相换个Broker

在这里插入图片描述
但是实际上不会这样做,Controller在处理的时候会把前面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 用双引号包裹起来

参数说明:

在这里插入图片描述

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_export 是一个用于将 Kafka 消息导出到其他存储系统的工具。它是基于 Kafka Connect 框架实现的。在启动 kafka_export 之前,需要先安装 Kafka Connect。 启动 kafka_export 的命令格式如下: ``` ./bin/connect-distributed.sh ./config/connect-distributed.properties ``` 其中,`connect-distributed.sh` 是 Kafka Connect 提供的启动脚本,`connect-distributed.properties` 是 Kafka Connect 的配置文件,需要根据实际情况进行修改。 在启动 kafka_export 之前,需要在 `connect-distributed.properties` 文件中添加以下配置信息: ``` # 指定 kafka_export 的插件路径 plugin.path=/path/to/kafka_export/plugins # 指定 kafka_export 的配置文件 rest.extensions=com.github.jcustenborder.kafka.connect.rest.RestExtension ``` 其中,`/path/to/kafka_export/plugins` 是 kafka_export 插件的路径,`com.github.jcustenborder.kafka.connect.rest.RestExtension` 是 kafka_export 的配置文件。 启动 kafka_export 之后,可以通过 REST API 来配置和管理 kafka_export。例如,可以使用以下命令来创建一个新的导出任务: ``` curl -X POST \ http://localhost:8083/connectors \ -H 'Content-Type: application/json' \ -d '{ "name": "export-task", "config": { "connector.class": "com.github.jcustenborder.kafka.connect.export.ExportSinkConnector", "tasks.max": "1", "topics": "test-topic", "connection.url": "jdbc:mysql://localhost:3306/test", "connection.user": "root", "connection.password": "password", "table.name.format": "test_table" } }' ``` 这个命令将会创建一个名为 `export-task` 的导出任务,将 `test-topic` 主题的消息导出到名为 `test_table` 的 MySQL 表中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值