Chuyển tới nội dung chính

Demo

Ví dụ này minh họa cách kết nối đến FPT Kafka bằng Python để gửi message từ producer và đọc message bằng consumer. Bạn cần đã tạo topic, credential và ACL phù hợp trước khi thực hiện. Tham khảo thêm tài liệu trong mục Document trên menu.

Phần 1: Producer gửi message vào topic

  1. Tạo file producer.py.

    Màn hình tạo file producer.py

  2. Trong file producer.py, điền các thông tin sau:

    Nội dung file producer.py với các tham số kết nối Kafka

    • sasl_plain_usernamesasl_plain_password: Username và password của credential đã tạo
    • topic: Tên topic đã tạo và đã cấp quyền Write
    • bootstrap.servers: Thông tin cluster do admin cấp
  3. Mở terminal và chạy lệnh:

    python .\producer.py

    Kết quả mong đợi:

    Kết quả terminal sau khi producer gửi message thành công

    Nếu output đúng, producer đã gửi message thành công vào các partition của topic.

Phần 2: Consumer đọc message

  1. Tạo file consumer.py.

    Màn hình tạo file consumer.py

    Cấu trúc file consumer.py

  2. Trong file consumer.py, điền các thông tin sau:

    Nội dung file consumer.py với các tham số kết nối và consumer group

    • group_name: Tên consumer group đã tạo và đã cấp quyền Read topic
    • sasl_plain_usernamesasl_plain_password: Username và password của credential đã tạo
    • topic: Tên topic đã cấp quyền Write và Read
    • bootstrap.servers: Thông tin cluster do admin cấp
  3. Mở terminal và chạy lệnh:

    python .\consumer.py

    Kết quả mong đợi:

    Kết quả terminal sau khi consumer đọc message từ topic thành công