我最近尝试使用python向Kafka发送消息。当使用简单字节消息时,它可以工作。但是现在,我有一个json数据,需要发送到Kafka主题,然后由Java应用程序使用。
我试图了解如何将json转换为byteArray(这就是Java应用程序期望的有效负载)。所以,我想出了下面的python脚本。但是它失败了,因为json中有几个布尔变量,我得到了一个类型错误,因为Jsontrue和PythonTrue在情况下是不同的。我试图用单引号将json括起来,但是再次得到了‘EOL同时扫描字符串文字’的错误。只有当我修复了这个错误,我才知道我是否能够将这些数据发送给Kafka,所以到目前为止,我还在挣扎于转换部分。下面是我的代码和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脚本:#!/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)
在我发布消息之前,消费者一直在使用消息,比如: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()
最新情况:
我在json字符串中添加了三元引号,而生产者代码现在没有出现任何错误。但消费者并不是在消费信息。至少,它没有像预期的那样输出它们。