Kafka consumers

Apache Kafka เบื้องต้น

Mike Metzger

Data Engineering Consultant

Consumer คืออะไร?

  • อ่านข้อความจาก Kafka topics
    • บางครั้งเรียกว่า subscribers
  • Kafka ส่งข้อความที่เก็บใน topics ให้ consumers
  • มี consumers ได้หลายตัว
  • Consumer อ่านได้จากหนึ่งหรือหลาย topics
Apache Kafka เบื้องต้น

ประเภทของ consumers

  • Consumer มีหลายประเภท
    • Command-line
      • 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
  • โดยค่าเริ่มต้น จะเปิดการเชื่อมต่อแสดงข้อความแบบ real-time และจำตำแหน่งไว้
  • กด Ctrl-C เพื่อออก
Apache Kafka เบื้องต้น

อาร์กิวเมนต์เสริม

  • --from-beginning
    • สั่งให้ Kafka ส่งข้อความทั้งหมดใน topic แม้จะเคยอ่านไปแล้ว
  • --max-messages <number>
    • อ่านข้อความได้สูงสุดตามจำนวนที่กำหนด แล้วออกจากโปรแกรม
Apache Kafka เบื้องต้น

ตัวอย่างแบบ interactive

  • ตัวอย่างแบบ interactive
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 เบื้องต้น

ตัวอย่างแบบ non-interactive

  • แบบ non-interactive
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...