I have the following python3 avro program:
import avro.schema
import json
from avro.datafile import DataFileReader, DataFileWriter
from avro.io import DatumReader, DatumWriter
write_schema = avro.schema.parse(json.dumps({
"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
{"name": "name", "type": "string"},
{"name": "favorite_number", "type": ["int", "null"]},
{"name": "favorite_color", "type": ["string", "null"]}
]
}))
writer = DataFileWriter(open("users.avro", "wb"), DatumWriter(), write_schema)
writer.append({"name": "Alyssa", "favorite_number": 256})
writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"})
writer.close()
read_schema = avro.schema.parse(json.dumps({
"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
{"name": "first_name", "type": "string", "default": "", "aliases": ["name"]},
{"name": "favorite_number", "type": ["int", "null"]},
{"name": "favorite_color", "type": ["string", "null"]}
]
}))
reader = DataFileReader(open("users.avro", "rb"), DatumReader(write_schema, read_schema))
new_schema = reader.get_meta("avro.schema")
users = []
for user in reader:
users.append(user)
reader.close()
The contents of users
follows:
[{'favorite_color': None, 'favorite_number': 256, 'first_name': ''},
{'favorite_color': 'red', 'favorite_number': 7, 'first_name': ''}]
I would have thought that it would have 'Ben' and 'Alyssa' in the first_name
field. How do aliases in this library work? Is this following the specification?
Sadly I have now discovered that the python version of the standard avro package doesn't implement aliasing (https://issues.apache.org/jira/browse/AVRO-1303). As this has been around since 2013/2014, I don't expect a standard fix anytime soon. More promising is the fastavro project which is working on a fix.