Skip to content

Instantly share code, notes, and snippets.

@lmmx
Created August 29, 2025 00:07
Show Gist options
  • Save lmmx/d7584ed625f8279b9859614b7ace2cde to your computer and use it in GitHub Desktop.
Save lmmx/d7584ed625f8279b9859614b7ace2cde to your computer and use it in GitHub Desktop.
import polars as pl
import fastavro
from pathlib import Path
# Define an Avro schema with both record (struct) and map
schema = {
"type": "record",
"name": "Inventory",
"fields": [
{"name": "id", "type": "int"},
{"name": "info", "type": {
"type": "record",
"name": "UserInfo",
"fields": [
{"name": "name", "type": "string"},
{"name": "age", "type": "int"}
]
}},
{"name": "items", "type": {"type": "map", "values": "int"}}
]
}
# Example records
records = [
{"id": 1, "info": {"name": "Alice", "age": 30}, "items": {"apples": 10, "bananas": 25}},
{"id": 2, "info": {"name": "Bob", "age": 40}, "items": {"oranges": 5}},
]
# Write them to Avro using fastavro
out_path = Path("map_record_demo.avro")
with out_path.open("wb") as out:
fastavro.writer(out, schema, records)
# Read back with Polars
df = pl.read_avro(out_path)
print("Polars schema:", df.schema)
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment