python3操作Kafka

# -- coding: UTF-8
import datetime
import json
import time
from kafka import KafkaProducer

producer=KafkaProducer(bootstrap_servers='192.168.10.10:9092')
for i in range(111):
    future = producer.send('test', json.dumps(
        {"method": "get", "step": i, "type": "test", "testName": "kafka",
         "cid": "{0}".format(datetime.datetime.now().strftime('%Y%m%d%H%M%S')),
         "info": "demo{}".format(1)}).encode())
    record_metadata = future.get(timeout=10)
    print(record_metadata, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    time.sleep(3)
from kafka import KafkaConsumer

consumer = KafkaConsumer('test', bootstrap_servers=['192.168.10.10:9092'], auto_offset_reset='earliest')

for message in consumer:
    print(message)
    print("%s:%d:%d: key=%s value=%s" % (message.topic, message.partition,
                                         message.offset, message.key,
                                         message.value))

  

posted @ 2019-11-11 20:35  小学弟-  阅读(4011)  评论(0编辑  收藏  举报