Kafka topics

Apache Kafka เบื้องต้น

Mike Metzger

Data Engineering Consultant

Topic คืออะไร?

  • Topic คือการจัดกลุ่มเหตุการณ์เชิงตรรกะ
    • คล้ายกับตารางในฐานข้อมูลเชิงสัมพันธ์
    • บันทึกเหตุการณ์
    • ข้อความไม่สามารถเปลี่ยนแปลงได้
    • ข้อความในหัวข้อสามารถอ่านหรือเขียนได้ แต่ไม่สามารถแก้ไขได้
    • ข้อความสามารถถูกลบออกตามอายุได้

ภาพประกอบ Kafka topic

Apache Kafka เบื้องต้น

การสร้าง topic

  • มีหลายวิธีในการสร้าง topic
  • จะใช้สคริปต์ bin/kafka-topics.sh
    bin/kafka-topics.sh 
        --bootstrap-server <server> 
        --topic <topicname>
        --create
    
  • ตัวอย่าง
    $ bin/kafka-topics.sh --bootstrap-server localhost:9092 \
    --topic orders --create
    
Created topic orders
Apache Kafka เบื้องต้น

รูปแบบอื่น ๆ

  • อาร์กิวเมนต์เพิ่มเติมสำหรับ bin/kafka-topics.sh --create

    • --replication-factor <x>
      • กำหนด replication factor ของ topic
    • --partitions <x>
      • ระบุจำนวน partition ด้วยตนเอง
  • ตัวอย่าง

    $ bin\kafka-topics.sh --bootstrap-server localhost:9092 \
    --topic orders --create --replication-factor 3 \
    --partitions 3
    
Created topic orders
Apache Kafka เบื้องต้น

--describe

  • bin/kafka-topics.sh --bootstrap-server <server> --topic <topicname> --describe

    • ดูรายละเอียดการกำหนดค่าของ topic
  • ตัวอย่าง

    $ bin/kafka-topics.sh --bootstrap-server localhost:9092 \
       --topic orders --describe
    
Topic: orders-new TopicId: <topicid> PartitionCount: 3 ReplicationFactor: 1    
  Configs:
    Topic: orders-new    Partition: 0    Leader: 0    Replicas: 0    Isr: 0
    Topic: orders-new    Partition: 1    Leader: 0    Replicas: 0    Isr: 0
    Topic: orders-new    Partition: 2    Leader: 0    Replicas: 0    Isr: 0
Apache Kafka เบื้องต้น

การลบ topic

  • bin/kafka-topics.sh --bootstrap-server <server> --topic <topicname> --delete
    • ลบ topic ออกจาก Kafka server
    • การลบ topic จะลบข้อความทั้งหมดใน topic นั้นด้วย
  • ตัวอย่าง
    $ bin/kafka-topics.sh --bootstrap-server localhost:9092
      --topic orders --delete
    
$
Apache Kafka เบื้องต้น

มาฝึกกันเถอะ!

Apache Kafka เบื้องต้น

Preparing Video For Download...