简体   繁体   中英

How to use kafka-python to send customized payload? (between two ubuntu machines under same WIFI network)

How to use kafka-python to send customized payload?

I have "two ubuntu machines", and both are under same WIFI network(one address is 172.20.10.2, the other is 172.20.10.7), I can use deepstream test4 python script successfully transmit the detected bounding box info through kafka by use the above ip. But I want the customized payload... Thus, I tried some kafka-python scrpit. For producer:

from time import sleep
from json import dumps
from kafka import KafkaProducer

producer = KafkaProducer(bootstrap_servers=['172.20.10.7:9092'])
for e in range(100):
    data = {'number' : e}
    producer.send('demo01', value=data)
    sleep(1)

For consumer:

from kafka import KafkaConsumer
from json import loads

consumer = KafkaConsumer(
    'demo01',
     bootstrap_servers=['172.20.10.7:9092'],
     auto_offset_reset='earliest',
     enable_auto_commit=True,
     group_id='my-group',
     value_deserializer=lambda x: loads(x.decode('utf-8')))

for message in consumer:
    print(message.value)

did not work... So need some suggestion or the executable code if possible!

如果你想json.loads ,你需要将生产者数据序列化为 JSON

producer.send('demo01', value=json.dumps(data).encode('utf8'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM