一、Python编写kafka生产者代码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2019/7/11 9:21
# @Author : ShenJianFen
import datetime
import json
import time
import uuid
from kafka import KafkaProducer
from kafka.errors import KafkaError
# kafka生产者脚本 kafka-console-consumer --zookeeper localhost:2181 --topic test_topic --from-beginning
producer = KafkaProducer(bootstrap_servers='master1:9092,master2:9092,reserve1:9092')
topic = 'ffff'
def test():
print('begin')
try:
for i in range(100):
dic = {}
dic['id'] = i
dic['uuid'] = str(uuid.uuid4().hex)
dic['time'] = datetime.datetime.now().strftime("%Y%m%d %H:%M:%S")
producer.send(topic, json.dumps(dic).encode())
print("send:" + json.dumps(dic))
time.sleep(0