Convert Parquet to Avro with Python
Run this Python script locally with Apache Arrow. The script reads your local file rather than sending it to this website.
Before you start
- Install Python 3 and create an isolated environment using the commands below.
- Place input.parquet in your working folder. Use a new output filename and keep the original.
- Save the Python code as task.py. On macOS/Linux run .venv/bin/python task.py; on Windows PowerShell run .venv\Scripts\python.exe task.py.
python3 -m venv .venv
.venv/bin/python -m pip install pyarrow fastavropy -m venv .venv
.venv\Scripts\python.exe -m pip install pyarrow fastavroimport pyarrow.parquet as pq
table = pq.read_table("input.parquet")
import json
import re
from fastavro import writer
if any(not re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*", name) for name in table.column_names):
raise ValueError("Rename columns to valid Avro field names first")
schema = {"type": "record", "name": "Row", "fields": [
{"name": name, "type": ["null", "string"], "default": None}
for name in table.column_names
]}
def text(value):
if value is None:
return None
return json.dumps(value, ensure_ascii=False, default=str) if isinstance(value, (list, dict)) else str(value)
records = ({name: text(row[name]) for name in table.column_names} for row in table.to_pylist())
with open("output.avro", "wb") as output:
writer(output, schema, records, codec="deflate")
print("Saved output.avro")Limits and output
- These examples load the complete table in memory. Use small representative files first; use an explicit Arrow schema when inferred types do not match your intended values.
- The CLI/library examples use their own type mappings, which can differ from this website’s text-column conversion. Keep the source and compare nulls, identifiers, timestamps, decimals and nested values.
- Creates a Deflate-compressed Avro object container with nullable string fields. Nested values become JSON strings. Invalid Avro field names must be renamed before running this script.
Documentation: Apache Arrow Parquet documentation; fastavro documentation