gpt4 book ai didi

Python - 无模式 Apache Avro 数据序列化

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:56 26 4
gpt4 key购买 nike

我正在尝试使用 python 2.7 和 Apache Avro(python 客户端)通过 kafka 代理交换序列化消息。我想知道是否有一种方法可以在不创建模式的情况下交换消息。

这是代码(使用架构,sensor.avsc,我想避免的事情):

from kafka import SimpleProducer, KafkaClient
import avro.schema
import io, random
from avro.io import DatumWriter

# To send messages synchronously
kafka = KafkaClient('localhost:9092')
producer = SimpleProducer(kafka, async = False)

# Kafka topic
topic = "sensor_network_01"

# Path to user.avsc avro schema that i don't want
schema_path="sensor.avsc"
schema = avro.schema.parse(open(schema_path).read())


for i in xrange(100):
writer = avro.io.DatumWriter(schema)
bytes_writer = io.BytesIO()
encoder = avro.io.BinaryEncoder(bytes_writer)
# creation of random data
writer.write({"sensor_network_name": "Sensor_1", "value": random.randint(0,10), "threshold_value":10 }, encoder)

raw_bytes = bytes_writer.getvalue()
producer.send_messages(topic, raw_bytes)

这是sensor.avsc文件:

{
"namespace": "sensors.avro",
"type": "record",
"name": "Sensor",
"fields": [
{"name": "sensor_network_name", "type": "string"},
{"name": "value", "type": ["int", "null"]},
{"name": "threshold_value", "type": ["int", "null"]}
]
}

最佳答案

这段代码:

import avro.schema
import io, random
from avro.io import DatumWriter, DatumReader
import avro.io

# Path to user.avsc avro schema
schema_path="user.avsc"
schema = avro.schema.Parse(open(schema_path).read())


for i in xrange(1):
writer = avro.io.DatumWriter(schema)
bytes_writer = io.BytesIO()
encoder = avro.io.BinaryEncoder(bytes_writer)
writer.write({"name": "123", "favorite_color": "111", "favorite_number": random.randint(0,10)}, encoder)
raw_bytes = bytes_writer.getvalue()

print(raw_bytes)

bytes_reader = io.BytesIO(raw_bytes)
decoder = avro.io.BinaryDecoder(bytes_reader)
reader = avro.io.DatumReader(schema)
user1 = reader.read(decoder)
print(" USER = {}".format(user1))

用于处理此模式

{"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
{"name": "name", "type": "string"},
{"name": "favorite_number", "type": ["int", "null"]},
{"name": "favorite_color", "type": ["string", "null"]}
]
}

就是你所需要的。

信用归this gist

关于Python - 无模式 Apache Avro 数据序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36793657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com