Introduction
Apache Avro is a binary data format from the Hadoop project, first released in 2009. Records are described by a schema written in JSON, and the binary data holds only the values, in schema order, with no field names or type bytes. That makes it compact, but the schema is needed to read it.
An .avro file is an object container file: it carries its schema in the
header, so any Avro reader can open it. Avro is widely used with Kafka and in data
lakes, where readers can also handle data written with an older or newer version of the
schema.
Structure
The file starts with the four bytes Obj and 01. Then comes
a metadata map, which holds the schema as JSON under avro.schema and the
compression codec under avro.codec (null,
deflate, snappy, zstandard and others), and a
random 16-byte sync marker.
The rest is a run of data blocks. Each gives the number of records and the size in bytes, then the records, compressed with the codec, then the sync marker again, which lets a reader find block boundaries when it starts in the middle of a file. Integers and lengths are written as variable-length zig-zag numbers.
0MagicObjand014 bytes4Metadata the schema, the codec and other keysvaries…Sync marker16 bytes…Data blocks record count, size, records, sync markerrepeated
The schemas and encoding page gives every type, the binary encoding and the codecs.
In the explorer
Drop an .avro file on this site. The structure tree shows the magic, every
metadata entry (the schema, the codec and any other key), the sync marker, and each block
with its object count, data size, data and sync marker. When the data is stored uncompressed
every record opens into its fields, unions into their branch index and value, arrays and
maps into their blocks; compressed blocks (deflate, bzip2, Snappy, Zstandard, xz) are decoded
too and their records are in the Records table, whatever the codec. The schema is read and
checked against the rules on the encoding page
(names, namespaces, unions, enums, fixed sizes, defaults, logical types and their underlying
types); the Schema table lists every record, field and enum, and the parsing canonical
form and CRC-64-AVRO fingerprint are shown. Logical types are shown as dates, times,
timestamps, decimals, UUIDs and durations. The records also open as a JSON lines file of their
own. A damaged block, a wrong sync marker or a record that does not fit the schema is
reported and the rest of the file is still shown.
Not described
This page covers the object container file and the single-object encoding. Avro IDL (.avdl),
protocol files (.avpr) and the RPC wire format are separate formats and are not claimed here.
The reference follows the Apache Avro 1.12.0 specification and its IDL Language page.
The specification says nothing about the optional codecs beyond what the encoding page quotes. No reference gap is known after the third re-check: name syntax, namespaces, union restrictions, alias qualification, the decimal precision limit and matching rule, uuid per RFC 4122, and the 1.12.0 union default wording are now on the encoding page.
Known, not yet covered
- Viewer: all six codecs are decoded:
null,deflate,bzip2,snappy(with its CRC-32 checked),zstandardandxz(the explorer’s XZ decoder, given the size that the xz index at the end of the block lists, with the block’s CRC and index checks). - Viewer: data is read with the schema in the file, the writer’s schema, which the viewer shows in full (the Schema table and the Schema as JSON text). Resolution against a reader schema is not file content: the reader schema belongs to the program that reads and is not stored in the file, so there is nothing in a file to resolve. Likewise the sort order, the MD5 and SHA-256 fingerprints and the JSON encoding of values are things a program applies; the Records table and the JSON lines entry show unions in the JSON form and bytes as hex, and only the CRC-64-AVRO fingerprint and the parsing canonical form are shown.
- Viewer: a single-object message (
C3 01, the fingerprint, the value) carries no schema. If the same file also holds a JSON schema after the message whose fingerprint matches, the value is decoded with it; otherwise the value is shown as labelled bytes with the fingerprint, because the schema is stored elsewhere (a registry) and so is not in the file. - Viewer: every block is checked when the file is opened and every record is reachable, whatever the size of the block or of the file. A block that is stored in more than 64 MiB, or decodes to more than 64 MiB, is not held decoded: it is read in windows, with the null codec by offset and with deflate, bzip2, xz, zstandard and snappy by the explorer’s windowed decoders (a Snappy block’s CRC-32 is carried through the pieces and checked at the end). The check pass decodes the block once, record by record, and keeps a table of where every 2,048th record starts; the Records table and the JSON lines then read from there. There is no limit on the size of one block. The limits that remain are narrow: a Snappy stream in a block of this size whose copy reaches back more than 1 MiB, an xz block that is not made of LZMA2 chunks, and a Zstandard block that names a dictionary are reported as not readable in pieces; and one single record of more than 256 MiB is reported, as its fields would not fit the window.
- Viewer: values outside the range of their logical type are shown as the raw number with a
note, never as a wrong time: a
time-millisortime-microsoutside one day, a day number or timestamp beyond the dates a browser can show (every digit of the long is kept). Adecimalorbig-decimalwith a negative scale is written out with its zeros (unscaled 5, scale −2 is 500), or in exponent form beyond 1,000 digits. A map with a repeated key (the specification says keys are unique) shows every entry in the tree and the JSON lines text, and the Records table marks it. - Viewer: a block whose codec checksum fails (a wrong Snappy CRC-32, or an xz/bzip2/zstandard end check) counts as a block with problems: the Check row names it, the file’s check status is “damaged” and the Blocks table says “decoded, but …”. In the JSON lines of a damaged block every record before the damage is written first and the codec’s own message (the CRC-32, not “runs past the end”) is the error. One limit remains: the JSON lines of a block read in windows are made from the explorer’s windowed source, which keeps whole 256 KiB pieces, so when a stream is damaged in the middle (not a checksum at the end) up to about 256 KiB of good records just before the damage (a few hundred records of 1 KB) may be missing from them although the Records table lists them.
Links
Software
- Avro Tools – the command-line tools from the Apache Avro project, which print files as JSON
- DuckDB – analytical database that reads Avro files with its avro extension (Windows, macOS, Linux)
- Apache Spark – distributed data processing engine that reads and writes Avro
Developers
- Apache Avro – the official libraries (Java, C, C++, C#, Python, Rust and more)
- fastavro – fast Avro reader and writer (Python)
Reference
- Apache Avro documentation – with the specification of the object container file