{"library":"avro","code":"import avro.schema\nfrom avro.datafile import DataFileReader, DataFileWriter\nfrom avro.io import DatumReader, DatumWriter\nimport io\n\n# Define schema\nschema_str = '''\n{\n    \"type\": \"record\",\n    \"name\": \"User\",\n    \"fields\": [\n        {\"name\": \"name\", \"type\": \"string\"},\n        {\"name\": \"favorite_number\", \"type\": [\"int\", \"null\"]},\n        {\"name\": \"favorite_color\", \"type\": [\"string\", \"null\"]}\n    ]\n}\n'''\nschema = avro.schema.parse(schema_str)\n\n# Prepare data\nusers = [\n    {\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": \"red\"},\n    {\"name\": \"Ben\", \"favorite_number\": 7, \"favorite_color\": \"blue\"},\n    {\"name\": \"Charlie\", \"favorite_number\": null, \"favorite_color\": \"green\"},\n    {\"name\": \"David\", \"favorite_number\": 42, \"favorite_color\": null}\n]\n\n# Write data to an in-memory Avro file\n# Using io.BytesIO for an in-memory file-like object\noutput_stream = io.BytesIO()\nwriter = DataFileWriter(output_stream, DatumWriter(), schema)\nfor user in users:\n    writer.append(user)\nwriter.close()\n\n# Reset stream position to read from the beginning\noutput_stream.seek(0)\n\n# Read data from the in-memory Avro file\nreader = DataFileReader(output_stream, DatumReader())\nprint(\"Reading Avro data:\")\nfor user in reader:\n    print(user)\nreader.close()\n\noutput_stream.close()","lang":"python","description":"This quickstart demonstrates how to define an Avro schema, serialize Python dictionaries (records) into an Avro data file (here, in-memory using `io.BytesIO`), and then deserialize them back into Python dictionaries. It uses `avro.schema.parse` to load the schema, `DataFileWriter` and `DatumWriter` to write, and `DataFileReader` and `DatumReader` to read.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}