python kafka(一)生产和消费
kafka-python(一)生产和消费
python 连接kafka组件安装:
pip install kafka-python
Producer(生产者)
kafka-python 生产数据,需要导入:KafkaProducer
from kafka import KafkaProducer
创建KafkaProducer连接实例
producer = KafkaProducer(bootstrap_servers=["192.168.11.128:9092"])
将信息推送到主题(topic)中,推送的消息(message)最好转成json格式,或者bytes类型
producer.send(topic,data)
写一个简单的示例,如下:
#encoding:utf-8 from kafka import KafkaProducer import json import time import random topic = "newOrder" producer = KafkaProducer(bootstrap_servers=["192.168.11.128:9092"]) for i in range(30): time.sleep(1) username = ["jcTang","libai","tufu","tumu"] phone = ["151***4481","159***6629","189***7891","131***5681"] random_num = random.randint(0,3) orderinfo={ "orderNo":"no"+str(time.time()).replace(".","")[:10], "username":username[random_num], "phone":phone[random_num], "goodid":random.randint(1,100), "price":random.randint(1,100), "buyNum":random.randint(1,20), "allPrice":random.randint(1,10000), "remark":"测试kafka" } data = json.dumps(orderinfo,ensure_ascii=True).encode("utf-8") print(data) producer.send(topic,data) producer.close()
Consumer(消费者)
kafka-python 消费数据,需要导入:KafkaConsumer
from kafka import KafkaConsumer
创建KafkaConsumer连接实例
consumer = KafkaConsumer( "newOrder", bootstrap_servers=["192.168.11.128:9092"], )
写一个简单的示例,如下:
#encoding:utf-8
from kafka import KafkaConsumer
import json
consumer = KafkaConsumer(
"newOrder",
bootstrap_servers=["192.168.11.128:9092"],
)
for i in consumer:
print(json.loads(i.value))
看看运行效果: