import avro.schema
from avro.datafile import DataFileReader, DataFileWriter
schema_string = """{"namespace": "example.avro",
 "type": "record",
 "name": "KeyValue",
 "fields": [
     {"name": "key", "type": "string"},
     {"name": "value",  "type": ["int", "null"]}
 ]
}"""
schema = avro.schema.parse(schema_string)
with open('kv.avro', 'w') as f:
    with DataFileWriter(f, DatumWriter(), schema) as wrt:
        wrt.append({'key': 'foo', 'value': -1})
wrt.append({'key': 'bar', 'value': 1})
Как прочитать файл Avro в PySpark