Kafka 消费者

Apache Kafka 入门

Mike Metzger

Data Engineering Consultant

什么是消费者?

  • 从 Kafka 主题读取消息
    • 也称订阅者
  • Kafka 将主题中的消息提供给消费者
  • 可有多个消费者
  • 消费者可读取一个或多个主题
Apache Kafka 入门

消费者类型

  • 消费者种类多样
    • 命令行
      • kafka-console-consumer.sh
    • Python
    • Java
    • 其他多种语言
    • Kafka Connect
Apache Kafka 入门

kafka-console-consumer.sh

  • 位于 bin/ 目录,与其他 Kafka 工具一起
    • bin/kafka-console-consumer.sh
  • 有多种选项,部分必需
  • 必需选项与 kafka-console-producer 相同:
    • --topic
    • --bootstrap-server
  • 默认打开连接,实时显示消息并维护位点
  • 使用 Ctrl-C 退出
Apache Kafka 入门

可选参数

  • --from-beginning
    • 指示 Kafka 从头发送该主题的所有消息,即使可能已读过
  • --max-messages <number>
    • 在读取至多指定数量的消息后退出
Apache Kafka 入门

交互式示例

  • 交互式示例
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 \
    --topic testing --from-beginning
This is the first message
This is the second message
This is the third message
<ctrl+c to exit>
Apache Kafka 入门

非交互式示例

  • 非交互式
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 /
  --topic testing --from-beginning --max-messages 2
This is the first message
This is the second message
<automatically exits>
Apache Kafka 入门

Passons à la pratique !

Apache Kafka 入门

Preparing Video For Download...