安裝執行環境
apt-get install openjdk-8-jre
pip3 install kafka
啟動kafka
bin/zookeeper-server-start.sh config/zookeeper.properties
bin/kafka-server-start.sh config/server.properties
啟動生產者:
1.從終端啟動
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
#開啟了乙個名為test的topic
2.從python3啟動
>>> from kafka import kafkaproducer
>>> producer = kafkaproducer(bootstrap_servers='localhost:9092')
>>> for _ in range(100):
... producer.send('test',b'some_message_bytes') #指定傳送給名為test的topic
啟動消費者:
1.從終端啟動
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
2.從python3啟動
>>> from kafka import kafkaconsumer
>>> consumer=kafkaconsumer('test',bootstrap_servers=['localhost:9092'])
>>> for msg in consumer:
... recv = "%s:%d:%d: key=%s value=%s" %(msg.topic,msg.partition,msg.offset,msg.key,msg.value)
... print(recv)
...test:0:222: key=none value=b'some_message_bytes'
test:0:223: key=none value=b'some_message_bytes'
test:0:224: key=none value=b'some_message_bytes'
test:0:225: key=none value=b'some_message_bytes'
。。。。
測試可用,要先啟動消費者再啟動生產者,topic一定要一致
部分參考**
kafka與zookeeper簡介
kafka中bootstrap-server、broker-list和zookeeper的區別
有空可以看一下極客學院的課程
ubuntu使用錯誤集總
q1 yl lenovo sudo apt get install synaptic 正在讀取軟體包列表.有錯誤!e encountered a section with no package header e problem with mergelist var lib apt lists cn....
Ubuntu系統下apt apt 相關命令總結
apt get是一條 linux命令 適用於deb包管理式的作業系統,主要用於自動從網際網路的軟體倉庫中搜尋 安裝 公升級 解除安裝軟體或作業系統,常用的命令總結如下 sudo apt get install package 安裝包 sudo apt get install package rein...
php環境下使用kafka
a.安裝librdkafka 庫 git clone configure make sudo make install b.安裝php rdkafka 擴充套件 git clone 生成configure檔案 phpize 編譯安裝 configure with php config usr loc...