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

  • 與其他 Kafka 工具一起放在 bin/ 資料夾
    • bin/kafka-console-consumer.sh
  • 有多個選項,部分為必填
  • 必填選項與 kafka-console-producer 相同:
    • --topic
    • --bootstrap-server
  • 預設會開啟連線,以即時顯示訊息並維持位置。
  • 使用 Ctrl-C 離開
Apache Kafka 入門

Optional arguments

  • --from-beginning
    • Tells the Kafka system to send all messages in topic, even ones that may have been already read.
  • --max-messages <number>
    • Used to read up to a maximum number of messages before exiting
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 入門

一起來練習吧!

Apache Kafka 入門

Preparing Video For Download...