how to send Json file to kafka from Python
·
Answer a question
I have a json file like below which I want to send to kafka from python.
Json File
filename = 'External_Risk_{}.json'.format(date.today().strftime("%Y%m%d"))
d =df.to_json(filename, orient='records')
Sending to Kafka
from kafka import SimpleProducer, KafkaClient
import json
from kafka import KafkaProducer
producer =KafkaProducer(bootstrap_servers='xxx.xxx.xxx.xxx')
jd = json.dumps(d)
producer.send_messages(b'message1',jd)
But it not working . What is correct way of sending a json file to Kafka.
Answers
You should be asking how to load a file to a string, then you're just sending a string to Kafka
import json
from kafka import KafkaProducer
producer =KafkaProducer(bootstrap_servers='xxx.xxx.xxx.xxx')
with open(filename) as f:
data = json.load(f)
producer.send_message(topic, data.encode('utf-8')
更多推荐

所有评论(0)