【Kafka基础】topics命令行操作大全:高级命令解析(1)

发布于:2025-04-08 ⋅ 阅读:(36) ⋅ 点赞:(0)

1 创建压缩主题(Log Compaction)

/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-topics.sh --create \
    --bootstrap-server 192.168.10.33:9092 \
    --topic comtopic \
    --partitions 3 \
    --replication-factor 2 \
    --config cleanup.policy=compact \
    --config min.cleanable.dirty.ratio=0.5 \
    --config segment.ms=3600000
参数说明
  • cleanup.policy=compact:启用日志压缩
  • min.cleanable.dirty.ratio:控制压缩频率
  • segment.ms:日志段滚动时间(毫秒)

2 查看主题的消息量统计

/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-run-class.sh kafka.tools.GetOffsetShell \
    --broker-list 192.168.10.33:9092 \
    --topic testtopic1 \
    --time -1
参数说明
  • --time -1:获取最新的偏移量(-2表示最早的)
  • 输出格式:topic:partition:offset

3 查看主题的磁盘使用情况

/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-log-dirs.sh --describe \
    --bootstrap-server 192.168.10.33:9092 \
    --topic-list testtopic1
参数说明
  • --topic-list:指定要检查的主题列表

4 迁移分区副本(重新分配分区)

/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-reassign-partitions.sh --generate \
    --bootstrap-server 192.168.10.33:9092 \
    --topics-to-move-json-file topics.json \
    --broker-list "1,2,3"
参数说明
  • --generate:生成迁移计划
  • --topics-to-move-json-file:包含要迁移主题的JSON文件
  • --broker-list:目标broker列表

5 验证分区重新分配进度

/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-reassign-partitions.sh --verify \
    --bootstrap-server 192.168.10.33:9092 \
    --reassignment-json-file reassignment.json
参数说明
  • --verify:验证迁移状态
  • --reassignment-json-file:迁移计划文件

6 查看消费者组偏移量

/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-consumer-groups.sh --describe \
    --bootstrap-server 192.168.10.33:9092 \
    --group testgroup \
    --offsets
参数说明
  • --group:消费者组名称
  • --offsets:显示偏移量信息