zsmeijin
6/17/2019 - 1:14 AM

kafka demo

[Kafka] #Python

from kafka import KafkaProducer

kafka_producer = None

def init(app):
    server = 'xxx.xxx.xxx.xxx:xxxx'
    global kafka_producer
    kafka_producer = KafkaProducer(bootstrap_servers=[server])
def save_log(log):
    msg = jsonify_object(log)
	#kafka发送的msg必须编码为bytes,不支持str类型
    kafka_producer.send('logs', msg)
    pass

#python对象转换为json字符串
def jsonify_object(object):
    return json.dumps(object.__dict__, ensure_ascii=False).encode('utf-8')

#dict对象转换为json字符串,当包含汉字时必须设置ensure_ascii=False,否则汉字显示为编码
def jsonify_dict(dict):
    return json.dumps(dict, ensure_ascii=False)

def unjsonify_dict(str):
    return json.loads(str)