操作系統 : CentOS7.3.1611_x64
Python 版本 : 3.6.8
kafka 版本 : 2.3.1
本文記錄python訪問kafka的簡單使用,是入門教程,高階讀者請直接忽略。
下載並啓動kafka
kafka官方網址: http://kafka.apache.org/
下載並解壓kafka :
http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.3.1/kafka_2.12-2.3.1.tgz tar zxvf kafka_2.12-2.3.1.tgz cd kafka_2.12-2.3.1/
啓動zookeeper:
bin/zookeeper-server-start.sh config/zookeeper.properties
啓動kafka :
bin/kafka-server-start.sh config/server.properties
控制檯測試:
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
使用python訪問kafka
安裝依賴庫:
pip install kafka
Producer示例代碼:
https://github.com/mike-zhang/pyExamples/blob/master/mqOpt/kafkaTest1/producerTest1.py
Consumer示例代碼:
https://github.com/mike-zhang/pyExamples/blob/master/mqOpt/kafkaTest1/consumerTest1.py
本文github地址:
https://github.com/mike-zhang/mikeBlogEssays/blob/master/2019/20191128_python訪問kafka.rst
歡迎補充