【Kafka基础】消费者命令行完全指南:从基础到高级消费

Kafka消费者是消息系统的关键组成部分,掌握/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh工具的使用对于调试、测试和监控都至关重要。本文将全面介绍该工具的各种用法,帮助您高效地从Kafka消费消息。

1 基础消费模式

1.1 从最新位置消费

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic

参数解析

  • --bootstrap-server: 指定Kafka集群地址
  • --topic: 指定消费的主题名称
    特点
  • 从该消费者组最后提交的offset开始消费
  • 如果没有提交记录,则从最新消息开始

1.2 从最早位置消费

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --from-beginning

关键参数

  • --from-beginning: 从主题最早的消息开始消费
    应用场景
  • 数据回溯
  • 新消费者组初始化

2 消息元数据展示

2.1 显示消息Key

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --property print.key=true \
    --property key.separator=":"

参数说明

  • print.key=true: 显示消息key
  • key.separator: 指定key/value分隔符

2.2 显示完整元数据

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --property print.key=true \
    --property print.value=true \
    --property print.partition=true \
    --property print.offset=true \
    --property print.timestamp=true \
    --property key.separator=":" \
    --property line.separator="\n"

元数据参数

  • print.partition: 显示分区号
  • print.offset: 显示消息offset
  • print.timestamp: 显示时间戳

3 精准消费控制

3.1 指定分区消费

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --partition 1

参数说明

  • --partition: 指定消费的分区编号

3.2 指定Offset消费

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --partition 0 \
    --offset 1000

参数说明

  • --offset: 指定开始消费的offset位置

3.3 消费超时设置

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --timeout-ms 10000

参数说明

  • --timeout-ms: 设置无消息时的超时时间(毫秒)

4 消费者组管理

4.1 使用消费者组

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --group mygroup

参数说明

  • --group: 指定消费者组名称
    特点
  • 支持offset自动提交
  • 支持消费者组rebalance

4.2 手动控制offset提交

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --group mygroup \
    --property enable.auto.commit=false

参数说明

  • enable.auto.commit=false: 关闭自动提交

5 高级消费配置

5.1 限制消费消息数

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --max-messages 100

参数说明

  • --max-messages: 最大消费消息数

5.2 过滤消费

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --property filter.key.regex="test.*"

参数说明

  • filter.key.regex: 按key正则过滤

5.3 消费速率控制

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --property fetch.min.bytes=1024 \
    --property fetch.max.wait.ms=500

参数说明

6 常用命令扩展

6.1 消费多个主题

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --whitelist "topic1|topic2"

6.2 显示消息头信息

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --property print.headers=true

6.3 消费特定时间后的消息

复制代码
/export/home/kafka_zk/kafka_2.13-2.7.1/bin/kafka-console-consumer.sh \
    --bootstrap-server 192.168.10.33:9092 \
    --topic testtopic \
    --property offsets.storage=kafka \
    --property timestamp=1625097600000
相关推荐
风吹夏回4 天前
RabbitMQ 核心术语 + Python pika 方法完整讲解
分布式·python·rabbitmq
风吹夏回4 天前
RabbitMQ 三种模式入门:HelloWorld、WorkQueue、PubSub
分布式·rabbitmq·ruby
霸道流氓气质4 天前
分布式追踪与 RequestId 传播完全指南
分布式
cheems95274 天前
[RabbitMQ高级特性] 消息确认机制:从 Ready / Unacked 到 basicAck、basicReject、basicNack 的底层拆解
分布式·rabbitmq·ruby
whaledown4 天前
Kafka 与 Java 消息队列入门:用订单场景理解核心机制
java·kafka·消息队列·springboot
枫华落尽4 天前
【Hadoop01-完全分布式运行模式】
分布式
隔壁阿布都4 天前
ShedLock 分布式定时任务锁框架介绍
spring boot·分布式
文艺倾年4 天前
【强化学习】数学推导专题,20W字总结(十五)
人工智能·分布式·大模型·强化学习·vibecoding
ACP广源盛139246256734 天前
GSV9001S@ACP#1080P 级视频处理芯片,物理 AI 普及终端的高性价比选择
大数据·人工智能·分布式·嵌入式硬件·spark
guslegend4 天前
第1章:初始Kafka
分布式·kafka