python kafka 用例

https://kafka-python.readthedocs.io/en/master/usage.html

code:

from kafka import KafkaProducer
import json

'''
 生产者demo
 向test_lyl2主题中循环写入10条json数据
 注意事项:要写入json数据需加上value_serializer参数,如下代码
'''
producer = KafkaProducer(
    value_serializer=lambda v: json.dumps(v).encode('utf-8'),
    bootstrap_servers=['127.0.0.1:9092']
)
for i in range(10000):
    data = {
        "name": "李四",
        "age": 23,
        "gender": "男",
        "id": i
    }
    producer.send('test', data)
producer.flush()

producer.close()

 

from kafka import KafkaConsumer
import json

'''
 消费者demo
 消费test_lyl2主题中的数据
 注意事项:如需以json格式读取数据需加上value_deserializer参数
'''


consumer = KafkaConsumer('test',group_id="lyl-gid2",
       bootstrap_servers=['127.0.0.1:9092'],
       auto_offset_reset='earliest',value_deserializer=json.loads
       )
for message in consumer:
 print(message.value)

你可能感兴趣的:(python)