kafka
mycode
1 from kafka import KafkaProducer 2 import time 3 producer = KafkaProducer(bootstrap_servers=["1.1.13.6:9092"]) 4 5 i = 20 6 while True: 7 i += 1 8 msg = "producer1+%d" % i 9 print(msg) 10 producer.send('huzh_topic', key=bytes(str(i)), value=msg.encode('utf-8')) 11 time.sleep(5) 12 13 producer.close()
consumer
1 from kafka import KafkaConsumer 2 3 consumer = KafkaConsumer("huzh_topic", bootstrap_servers=["1.1.13.6:9092"], auto_offset_reset='latest') 4 for msg in consumer: 5 key = msg.key.decode(encoding="utf-8") 6 value = msg.value.decode(encoding="utf-8") 7 print("%s-%d-%d key=%s value=%s" % (msg.topic, msg.partition, msg.offset, key, value))
https://blog.csdn.net/YHYR_YCY/article/details/85016112
、