Reputation: 171
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.
I've tried using io.BytesIO() but this gives:
AttributeError: '_io.BytesIO' object has no attribute 'write_long'
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()
Upvotes: 17
Views: 22579
Reputation: 384
Using fastavro in 2025 (https://fastavro.readthedocs.io/en/latest/writer.html)
schema_json = {
"type": "record",
"name": "User",
"namespace": "com.test",
"fields": [
{"name": "person_name", "type": "string"},
{"name": "age", "type": "int"},
{"name": "email", "type": "string"}
]
}
data_user_1 = {"person_name": "John Doe", "email": "[email protected]", "age": 30}
data_user_2 = {"person_name": "Jane Doe", "email": "[email protected]", "age": 25}
parsed_schema = parse_schema(schema_json)
bytes_io = BytesIO()
writer(bytes_io, parsed_schema, [data_user_1, data_user_2])
Upvotes: 0
Reputation: 679
Using import avro
library we can't write the avro file with the schema.
To overcome this problem use fastavro
e.g.
import io
import fastavro
data = [{"name": "Shravan", "favorite_number": 256}, {"name": "Ram", "favorite_number": 7, "favorite_color": "red"}]
bytes_writer = io.BytesIO()
fastavro.writer(bytes_writer, get_avro_schema(), data)
print(bytes_writer.get_value())
Upvotes: -2
Reputation: 2762
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)
Upvotes: 47