使用Avro将对象编码为Python中的字节数组 [英] Encode an object with Avro to a byte array in Python

查看:281
本文介绍了使用Avro将对象编码为Python中的字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python 2.7中,我想使用Avro将对象编码为字节数组.

In python 2.7, using Avro, I'd like to encode an object to a byte array.

我发现的所有示例都写入文件.

All examples I've found write to a file.

我尝试使用io.BytesIO(),但这给出了:

I've tried using io.BytesIO() but this gives:

AttributeError: '_io.BytesIO' object has no attribute 'write_long'

使用io.BytesIO采样

Sample using io.BytesIO

def avro_encode(raw, schema):
    writer = DatumWriter(schema)
    avro_buffer = io.BytesIO()
    writer.write(raw, avro_buffer)
    return avro_buffer.getvalue()

推荐答案

您的问题帮助我弄清楚了,谢谢.这是一个基于docs中的python示例的简单python示例:

Your question helped me figure things out, so thanks. Here's a simple python example based on the python example in the docs:

import io
import avro.schema
import avro.io

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

schema = avro.schema.parse(test_schema)
writer = avro.io.DatumWriter(schema)

bytes_writer = io.BytesIO()
encoder = avro.io.BinaryEncoder(bytes_writer)
writer.write({"name": "Alyssa", "favorite_number": 256}, encoder)
writer.write({"name": "Ben", "favorite_number": 7, "favorite_color": "red"}, encoder)

raw_bytes = bytes_writer.getvalue()
print(len(raw_bytes))
print(type(raw_bytes))

bytes_reader = io.BytesIO(raw_bytes)
decoder = avro.io.BinaryDecoder(bytes_reader)
reader = avro.io.DatumReader(schema)
user1 = reader.read(decoder)
user2 = reader.read(decoder)

print(user1)
print(user2)

这篇关于使用Avro将对象编码为Python中的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆