简体   繁体   English

发送json作为字节数组以使用Python发送到kafka

[英]Send a json as bytearray to send to kafka with Python

I recently tried to use python to send messages to Kafka. 我最近尝试使用python将消息发送到Kafka。 When using simple byte messages, it works. 使用简单字节消息时,它可以工作。 But now, I have a json data, that I need to send to Kafka topic which will then be consumed by a Java application. 但是现在,我有一个json数据,我需要将其发送到Kafka主题,然后由Java应用程序使用。

I tried to find out how to convert json to byteArray (that is what the Java application is expecting as the payload). 我试图找出如何将json转换为byteArray(这是Java应用程序期望的有效负载)。 So, I came up with the below python script. 因此,我想出了以下python脚本。 But it failed as there are a few boolean variables in the json and I am getting a type error as Json true and Python True are different in case. 但是它失败了,因为json中有一些布尔变量,并且由于Json true和Python True不同,我收到类型错误。 I tried to enclose the json in single quotes, but again I got the error 'EOL while scanning string literal'. 我尝试将json括在单引号中,但再次出现错误“扫描字符串文字时出现EOL”。 Only once I fix this error, will I know whether I am able to send this data to Kafka or not, so as of now I am struggling with the conversion part. 只有修复了此错误之后,我才能知道是否能够将此数据发送到Kafka,因此,到目前为止,我一直在努力处理转换部分。 Below is my code and the json. 下面是我的代码和json。

Json: 杰森:

{
"header": {
"activityId": "550",
"timeStamp": "1490093093000",
"sequencingId": 1
},
"queueId": "604",
"contextRef": "SLIP.UPDATE"
,
"state": {
"slips": [{
  "id": "550",
  "creationDate": "2017-01-30T14:14:14.000+0000",
  "accountRef": "1",
  "customerRef": "2",
  "source": {
    "channelRef": "K"
  },
  "receipt": "O/0000002/0000487",
  "isSettled": true,
  "isConfirmed": true,
  "lines": {
    "number": 1,
    "win": 1,
    "lose": 0,
    "voided": 0
  }
}]
}
}

Python script: Python脚本:

#!/usr/bin/python

from kafka import KafkaProducer

KAFKA_TOPIC = 'slips'
KAFKA_BROKERS = '172.17.0.1:9092'

producer = KafkaProducer(value_serializer=lambda v:json.dumps(v).encode('utf-8'),bootstrap_servers=KAFKA_BROKERS)

messages = '{
"header": {
"activityId": "550",
"timeStamp": "1490093093000",
"sequencingId": 1
},
"queueId": "604",
"contextRef": "SLIP.UPDATE"
},
"state": {
"slips": [{
"id": "550",
"creationDate": "2017-01-30T14:14:14.000+0000",
"accountRef": "1",
"customerRef": "2",
"source": {
"channelRef": "K"
},
"receipt": "O/0000002/0000487",
"isSettled": true,
"isConfirmed": true,
"lines": {
"number": 1,
"win": 1,
"lose": 0,
"voided": 0
 }
}]
}
}'

info_as_json = json.loads(messages)

producer.send(KAFKA_TOPIC, info_as_json)

The consumer was consuming messages until I was publishing message like : 在我发布消息之前,使用者一直在消耗消息:

messages = [b'hello kafka', b'I am sending', b'3 test messages']

Consumer: 消费者:

#!/usr/bin/python

import sys
from kafka import KafkaConsumer

KAFKA_TOPIC = 'slips'
KAFKA_BROKERS = '172.17.0.1:9092'

consumer = KafkaConsumer(bootstrap_servers=KAFKA_BROKERS,auto_offset_reset='earliest')

consumer.subscribe([KAFKA_TOPIC])
try:
    for message in consumer:
        print(message.value)
except KeyboardInterrupt:
    sys.exit()

Update: 更新:

I added the triple quotes in the json string and the producer code doesn't give any error now. 我在json字符串中添加了三引号,并且生产者代码现在不给出任何错误。 But the consumer is not consuming the messages. 但是,消费者并没有消费消息。 At least, it is not printing them as I would expect. 至少,它没有像我期望的那样打印它们。

Finally, I was able to consume messages. 最后,我能够使用消息。 It seems there was a problem with the producer. 生产者似乎有问题。 i went through some posts on StackOverflow and then added the below two changes in my producer code and it just worked. 我在StackOverflow上浏览了一些帖子,然后在我的生产者代码中添加了以下两项更改,它才起作用。

1) linger_ms=10 while initializing the producer 1)初始化生产者时linger_ms = 10

producer = KafkaProducer(value_serializer=lambda v:json.dumps(v).encode('utf-8'),bootstrap_servers=KAFKA_BROKERS, linger_ms=10)

2) flushing after sending the message 2)发送消息后冲洗

producer.flush()

I am yet to find why my producer was working without these changes for simple byte messages and not for the json. 我还没有找到为什么我的生产者在没有这些更改的情况下为简单字节消息而不是为json工作的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM