1

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.

Rahul rajan
  • 1,186
  • 4
  • 18
  • 32
  • Why are you using pandas? If you have a dataframe, you should be sending each row separately, not the whole thing at once – OneCricketeer Nov 22 '19 at 06:56
  • @cricket_007, the expected output inside kakfa is json and I am creating the json after many data manipulations using pandas. So should I be using your method to send the dataframe to kakfa. – Rahul rajan Nov 22 '19 at 09:33

3 Answers3

2

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')
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

Try this

from confluent_kafka import Producer
import json

p = Producer({'bootstrap.servers': 'localhost:9092'})

p.produce('topic', json.dumps({"demo": "message"}))
sky
  • 260
  • 3
  • 12
1

You can send a dict by simply doing like that:

producer = KafkaProducer(
    acks='all',
    bootstrap_servers=['localhost:9092'],
    value_serializer=lambda v: bytes(json.dumps(v, default=str).encode('utf-8'))
)

I call bytes() method before returning the json in the lambda

Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69