主题操作类
创建主题
# 创建主题(指定分区数和副本数)
kafka-topics.sh --create \
--bootstrap-server localhost:9092 \
--topic topic-name \
--partitions 3 \
--replication-factor 2列出所有主题
kafka-topics.sh --list --bootstrap-server localhost:9092查看主题详情
# 查看单个主题详情
kafka-topics.sh --describe \
--bootstrap-server localhost:9092 \
--topic topic-name
# 查看所有主题详情
kafka-topics.sh --describe --bootstrap-server localhost:9092修改主题
# 增加分区数(只能增加不能减少)
kafka-topics.sh --alter \
--bootstrap-server localhost:9092 \
--topic topic-name \
--partitions 5
# 修改主题配置
kafka-configs.sh --alter \
--bootstrap-server localhost:9092 \
--entity-type topics \
--entity-name topic-name \
--add-config retention.ms=86400000删除主题
kafka-topics.sh --delete \
--bootstrap-server localhost:9092 \
--topic topic-name生产消息类
控制台生产者(基础)
kafka-console-producer.sh \
--bootstrap-server localhost:9092 \
--topic topic-name带 key 的生产者
kafka-console-producer.sh \
--bootstrap-server localhost:9092 \
--topic topic-name \
--property parse.key=true \
--property key.separator=:输入格式:key:value
从文件生产消息
cat messages.txt | kafka-console-producer.sh \
--bootstrap-server localhost:9092 \
--topic topic-name生产时指定 acks 和压缩
kafka-console-producer.sh \
--bootstrap-server localhost:9092 \
--topic topic-name \
--producer-property acks=all \
--producer-property compression.type=gzip消费消息类
控制台消费者(从最新开始消费)
kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic topic-name从头开始消费
kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic topic-name \
--from-beginning指定消费者组消费
kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic topic-name \
--group group-name显示 key 和 value
kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic topic-name \
--property print.key=true \
--property print.value=true \
--property key.separator=:显示消息元数据(分区、偏移量、时间戳)
kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic topic-name \
--property print.timestamp=true \
--property print.offset=true \
--property print.partition=true消费者组管理类
列出所有消费者组
kafka-consumer-groups.sh --list --bootstrap-server localhost:9092查看消费者组详情(消费进度)
kafka-consumer-groups.sh --describe \
--bootstrap-server localhost:9092 \
--group group-name重置消费者组偏移量(需先停止消费者)
# 重置到最早偏移量
kafka-consumer-groups.sh --reset-offsets \
--bootstrap-server localhost:9092 \
--group group-name \
--topic topic-name \
--to-earliest --execute
# 重置到最新偏移量
kafka-consumer-groups.sh --reset-offsets \
--bootstrap-server localhost:9092 \
--group group-name \
--topic topic-name \
--to-latest --execute
# 重置到指定时间戳(毫秒)
kafka-consumer-groups.sh --reset-offsets \
--bootstrap-server localhost:9092 \
--group group-name \
--topic topic-name \
--to-datetime 2026-01-01T00:00:00.000 \
--execute
# 重置到指定偏移量
kafka-consumer-groups.sh --reset-offsets \
--bootstrap-server localhost:9092 \
--group group-name \
--topic topic-name:0 \
--to-offset 100 --execute删除消费者组(需组内无活跃消费者)
kafka-consumer-groups.sh --delete \
--bootstrap-server localhost:9092 \
--group group-name分区管理类
查看分区详情
# 查看 topic 所有分区及 Leader/ISR 信息
kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic topic-name查看指定分区的偏移量范围
# 查看最小偏移量
kafka-run-class.sh kafka.tools.GetOffsetShell \
--bootstrap-server localhost:9092 \
--topic topic-name \
--partitions 0 \
--time -2
# 查看最大偏移量
kafka-run-class.sh kafka.tools.GetOffsetShell \
--bootstrap-server localhost:9092 \
--topic topic-name \
--partitions 0 \
--time -1增加分区(重复注意只增不减)
kafka-topics.sh --alter \
--bootstrap-server localhost:9092 \
--topic topic-name \
--partitions 10配置管理类
查看主题配置
kafka-configs.sh --describe \
--bootstrap-server localhost:9092 \
--entity-type topics \
--entity-name topic-name修改主题配置
# 修改消息保留时间(7天)
kafka-configs.sh --alter \
--bootstrap-server localhost:9092 \
--entity-type topics \
--entity-name topic-name \
--add-config retention.ms=604800000
# 修改最大消息大小
kafka-configs.sh --alter \
--bootstrap-server localhost:9092 \
--entity-type topics \
--entity-name topic-name \
--add-config max.message.bytes=10485760删除主题配置(恢复默认)
kafka-configs.sh --alter \
--bootstrap-server localhost:9092 \
--entity-type topics \
--entity-name topic-name \
--delete-config retention.ms查看 Broker 配置
kafka-configs.sh --describe \
--bootstrap-server localhost:9092 \
--entity-type brokers \
--entity-name 0Broker 管理类
查看 Broker 列表
kafka-broker-api-versions.sh --bootstrap-server localhost:9092查看 Broker 元数据
zookeeper-shell.sh localhost:2181 <<< "ls /brokers/ids"查看 Broker 磁盘使用情况
kafka-log-dirs.sh --describe \
--bootstrap-server localhost:9092 \
--broker-list 0查看 Broker 运行时配置
kafka-configs.sh --describe \
--bootstrap-server localhost:9092 \
--entity-type brokers \
--entity-name 0ACL 权限管理类
添加 ACL(生产者权限)
kafka-acls.sh --add \
--bootstrap-server localhost:9092 \
--topic topic-name \
--allow-principal User:producer_user \
--operation Write \
--operation Describe \
--operation Create添加 ACL(消费者权限)
kafka-acls.sh --add \
--bootstrap-server localhost:9092 \
--topic topic-name \
--group group-name \
--allow-principal User:consumer_user \
--operation Read \
--operation Describe查看 ACL
kafka-acls.sh --list \
--bootstrap-server localhost:9092 \
--topic topic-name删除 ACL
kafka-acls.sh --remove \
--bootstrap-server localhost:9092 \
--topic topic-name \
--allow-principal User:producer_user \
--operation Write数据迁移与副本类
重新分配分区(生成迁移计划)
kafka-reassign-partitions.sh --generate \
--bootstrap-server localhost:9092 \
--topics-to-move-json-file topics.json \
--broker-list "0,1,2"执行分区重新分配
kafka-reassign-partitions.sh --execute \
--bootstrap-server localhost:9092 \
--reassignment-json-file reassign.json查看重新分配进度
kafka-reassign-partitions.sh --verify \
--bootstrap-server localhost:9092 \
--reassignment-json-file reassign.json修改副本因子
# 1. 生成 JSON 计划
kafka-reassign-partitions.sh --generate \
--bootstrap-server localhost:9092 \
--topics-to-move-json-file topics.json \
--broker-list "0,1,2"
# 2. 修改 JSON 中的副本数为目标值
# 3. 执行计划
kafka-reassign-partitions.sh --execute \
--bootstrap-server localhost:9092 \
--reassignment-json-file increase-replication.json运维与工具类
查看 Topic 消息总数(所有分区合计)
kafka-run-class.sh kafka.tools.GetOffsetShell \
--bootstrap-server localhost:9092 \
--topic topic-name \
--time -1 \
| awk -F ':' '{sum += $3} END {print sum}'查看 Broker 日志大小
du -sh /kafka-logs/*验证 Kafka 连接
kafka-broker-api-versions.sh --bootstrap-server localhost:9092导出/导入 Topic 配置(备份用)
# 导出配置
kafka-configs.sh --describe \
--bootstrap-server localhost:9092 \
--entity-type topics \
--entity-name topic-name > topic-config.txt
# 导入配置(重建时使用)
kafka-configs.sh --alter \
--bootstrap-server localhost:9092 \
--entity-type topics \
--entity-name topic-name \
--add-config retention.ms=604800000查看 ZooKeeper 中存储的 Topic 元数据(旧版)
zookeeper-shell.sh localhost:2181 <<< "ls /brokers/topics"从文件批量操作
# 批量创建主题
while read topic; do
kafka-topics.sh --create --bootstrap-server localhost:9092 \
--topic "$topic" --partitions 3 --replication-factor 2
done < topics.txt查看未消费消息数(Lag)
kafka-consumer-groups.sh --describe \
--bootstrap-server localhost:9092 \
--group group-name \
| awk '{if ($5 > 0) print $0}'
评论