使用pykafka来进行消息生产
1 #coding:utf8 2 from pykafka import KafkaClient 3 import json 4 5 client = KafkaClient(hosts='192.168.1.1:9092') 6 7 topic = client.topics['perfin'] 8 producer = topic.get_producer(use_rdkafka=False) 9 producer.start() 10 11 msg_dict = { 12 "sleep_time": 10, 13 "db_config" : { 14 "database" : "test", 15 "host" : "192.168.137.12", 16 "user" : "root", 17 "password" : "root" 18 }, 19 "table" : "msg", 20 "msg" : "Hello World" 21 } 22 msg = json.dumps(msg_dict) 23 producer.produce(msg) 24 producer.stop()