python kafka 发送json 分批_如何从python客户端发送JSON对象到kafka

1586010002-jmsa.png

I have a simple JSON object like the following

d = { 'tag ': 'blah',

'name' : 'sam',

'score':

{'row1': 100,

'row2': 200

}

}

The following is my python code which is sending messages to Kafka

from kafka import SimpleProducer, KafkaClient

import json

# To send messages synchronously

kafka = KafkaClient('10.20.30.12:9092')

producer = SimpleProducer(kafka)

jd = json.dumps(d)

producer.send_messages(b'message1',jd)

I see in the storm logs that the message is being received but its throwing

Transformation null for tuple { json structure in here }

not sure what needs to be done in order to fix this ?..

解决方案

The below is my code for producer to kafka. The only thing i did differently was to use yaml.safe_load to load the json content. It returns the contents as strings instead of unicode. The following is the snippet

with open('smaller_test_prod.txt') as f:

for line in f:

d = yaml.safe_load(line)

jd = json.dumps(d)

producer.send_messages(b'zeus_metrics',jd)

In here every line is a json data stored in a file.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值