Apache Avro Format Examples - Complete Reference Guide
Comprehensive examples of Apache Avro data format with schemas, data encoding, and real-world use cases for Kafka and Hadoop ecosystems.
Simple Avro Record Format
The most basic Apache Avro format example shows a simple user record with primitive types. This format is commonly used in microservices and data streaming applications.
Avro Schema:
{ "type": "record", "name": "User", "namespace": "com.example.users", "fields": [ { "name": "id", "type": "int" }, { "name": "username", "type": "string" }, { "name": "email", "type": "string" }, { "name": "age", "type": "int" }, { "name": "active", "type": "boolean" } ] }
Data in Avro Format (JSON Encoding):
{ "id": 1001, "username": "elara_quinn", "email": "[email protected]", "age": 28, "active": true }
Note: In production, Avro data is usually stored in binary, and the JSON encoding shown above is mostly for reading and testing. The binary form leaves out the field names and quotes because the schema already supplies them. This record is 93 bytes as minified JSON and 40 bytes in Avro binary.
Nested Record Format
Complex Avro format with nested records, commonly used for representing structured data like customer profiles with address information in Apache Kafka systems.
Avro Schema with Nested Record:
{ "type": "record", "name": "Customer", "namespace": "com.example.customers", "fields": [ { "name": "customerId", "type": "long" }, { "name": "name", "type": "string" }, { "name": "address", "type": { "type": "record", "name": "Address", "fields": [ { "name": "street", "type": "string" }, { "name": "city", "type": "string" }, { "name": "zipCode", "type": "string" } ] } } ] }
Data Example:
{ "customerId": 100234567890, "name": "Jane Smith", "address": { "street": "123 Main Street", "city": "San Francisco", "zipCode": "94102" } }
Avro Array Format
This example shows how arrays are represented in Avro format, useful for collections like order items, tags, or product lists in e-commerce and inventory systems.
Avro Schema with Array:
{ "type": "record", "name": "Order", "namespace": "com.example.orders", "fields": [ { "name": "orderId", "type": "string" }, { "name": "total", "type": "double" }, { "name": "items", "type": { "type": "array", "items": { "type": "record", "name": "OrderItem", "fields": [ { "name": "productId", "type": "string" }, { "name": "quantity", "type": "int" }, { "name": "price", "type": "double" } ] } } } ] }
Data Example:
{ "orderId": "ORD-2024-001", "total": 149.97, "items": [ { "productId": "PROD-123", "quantity": 2, "price": 49.99 }, { "productId": "PROD-456", "quantity": 1, "price": 49.99 } ] }
Union Type Format (Optional Fields)
A union lets a field hold one of several types. The usual case is an optional field: ["null", "double"] with "default": null. Note that null comes first, because Avro libraries expect a union's default to match its first branch. Adding a field like this is also the safe way to grow a schema over time.
Avro Schema with Union Types:
{ "type": "record", "name": "Product", "namespace": "com.example.products", "fields": [ { "name": "productId", "type": "string" }, { "name": "name", "type": "string" }, { "name": "price", "type": "double" }, { "name": "discount", "type": ["null", "double"], "default": null }, { "name": "description", "type": ["null", "string"], "default": null } ] }
Data Example (optional fields filled in):
{ "productId": "LAPTOP-2024", "name": "Professional Laptop", "price": 999.99, "discount": 50.00, "description": "High-performance laptop with 16GB RAM" }
Data Example (optional fields set to null):
{ "productId": "MOUSE-2024", "name": "Wireless Mouse", "price": 29.99, "discount": null, "description": null }
The two records above are plain JSON, which is what most libraries (avsc, fastavro) accept when you hand them an object. Avro's own JSON encoding, the one avro-tools fromjson and tojson use, is stricter: a union value that isn't null is wrapped in an object named after its branch type. A null stays a bare null, so the second record looks the same in both forms.
The Same Record in Avro JSON Encoding:
{
"productId": "LAPTOP-2024",
"name": "Professional Laptop",
"price": 999.99,
"discount": { "double": 50.0 },
"description": { "string": "High-performance laptop with 16GB RAM" }
}Enum Type Format
Avro enums define a fixed set of allowed values, perfect for status fields, categories, or any field with a predefined set of options. This ensures data quality and validation.
Avro Schema with Enum:
{ "type": "record", "name": "Transaction", "namespace": "com.example.transactions", "fields": [ { "name": "transactionId", "type": "string" }, { "name": "amount", "type": "double" }, { "name": "status", "type": { "type": "enum", "name": "Status", "symbols": ["PENDING", "COMPLETED", "FAILED", "CANCELLED"] } } ] }
Data Example:
{ "transactionId": "TXN-2024-12345", "amount": 250.75, "status": "COMPLETED" }
Kafka Event Message Format
A complete example of an Avro-formatted event message for Apache Kafka, including metadata fields commonly used in event-driven architectures and data streaming pipelines.
Kafka Event Schema:
{ "type": "record", "name": "UserEvent", "namespace": "com.example.events", "fields": [ { "name": "eventId", "type": "string" }, { "name": "eventType", "type": "string" }, { "name": "timestamp", "type": "long" }, { "name": "userId", "type": "string" }, { "name": "action", "type": "string" }, { "name": "metadata", "type": { "type": "map", "values": "string" } } ] }
Event Data Example:
{ "eventId": "evt-550e8400-e29b-41d4-a716-446655440000", "eventType": "USER_LOGIN", "timestamp": 1704067200000, "userId": "user-12345", "action": "login", "metadata": { "ipAddress": "192.168.1.100", "userAgent": "Mozilla/5.0", "sessionId": "sess-abc123" } }
Understanding Avro Data Format
What is Apache Avro Format?
Apache Avro is a data serialization format that combines a schema with the data. Unlike JSON or XML, Avro stores data in a compact binary format alongside a schema definition, making it highly efficient for data storage and transmission in distributed systems.
The Avro format consists of two main components: the schema (which defines the structure) and the data (which follows that structure). This separation enables schema evolution, where you can modify schemas over time while maintaining compatibility with existing data.
Binary vs JSON Encoding
Avro supports two encoding formats:
- Binary encoding: Compact, fast, and efficient for production use. Data is serialized in a space-efficient binary format that requires the schema to be decoded.
- JSON encoding: Human-readable format used for testing, debugging, and development. The examples on this page use JSON encoding for clarity.
Kafka setups that use the Confluent Schema Registry send binary: one magic byte, a 4-byte schema ID, then the encoded record. For the User record in Example 1 that comes to 45 bytes, against 93 for the same data as minified JSON.
Common Use Cases for Avro Format
- Apache Kafka: one of the most common formats for Kafka messages, usually paired with a Schema Registry so producers and consumers agree on the schema
- Hadoop Ecosystem: Used for storing data in HDFS, Hive, Spark, and other big data tools
- Data Pipelines: ETL processes use Avro for efficient data transfer between systems
- Microservices: Service-to-service communication with schema validation and evolution support
- Data Lakes: Long-term storage of structured data with schema versioning
Frequently Asked Questions
What is the difference between Avro format and JSON format?
JSON carries its field names in every record and is read as text. Avro keeps the field names in a separate schema and writes the values in a compact binary form, so a reader needs that schema to make sense of the bytes. How much smaller it gets depends on your data: the User record in Example 1 is 93 bytes as minified JSON and 40 bytes in Avro binary, while a record that is mostly long text strings shrinks much less. Avro is common in Kafka and Hadoop pipelines because of that size saving and the schema checks that come with it.
How do I create data in Avro format?
You need two things: a schema and records that match it. If you already have JSON, the JSON to Avro converter builds both for you, and the Avro Schema Generator gives you just the schema from a sample record. Either way, check the result with the Avro Schema Validator before you register it anywhere.
Can I read Avro format data without the schema?
Not the binary data on its own, because the bytes carry no field names or types. In practice the schema is usually close by: an Avro container file (.avro) always stores the writer's schema in its header, and a Kafka message written with Confluent's serializer starts with a schema ID that consumers look up in the Schema Registry.
What are Avro primitive types?
There are eight: null, boolean, int (32-bit), long (64-bit), float, double, bytes and string. On top of those come the complex types: record, enum, array, map, union and fixed (a byte array of set length). Logical types such as date, timestamp-millis, uuid and decimal are annotations on one of these, for example date is stored as an int and timestamp-millis as a long.
How does Avro handle schema evolution?
Avro reads data written with one schema (the writer's) using another (the reader's) and resolves the differences by field name. A field the reader adds needs a default, which is used when old data doesn't have it. A field the writer has but the reader doesn't is simply skipped. Renaming a field breaks that matching unless the reader lists the old name in aliases. Some type changes are allowed as promotions: int to long, float or double; long to float or double; float to double; and string to bytes or back. The BACKWARD, FORWARD and FULL modes you see in a Schema Registry are its own checks of these rules before it accepts a new schema version.
What tools work with Avro format?
On this site you can turn JSON into Avro with JSON to Avro, go back with Avro to JSON, tidy a schema with the Avro Formatter, check it with the Avro Schema Validator, compare two versions with the Avro Schema Compatibility Checker, and start from a spreadsheet with CSV to Avro. Outside the browser, the usual choices are avro-tools for Java, fastavro for Python and avsc for Node.js.
Avro tools on this site: JSON to Avro, Avro to JSON, Avro Schema Generator, Avro Formatter, Avro Schema Validator, Avro Schema Compatibility Checker and CSV to Avro. The rules for reading one schema with another are in the Avro specification.
Related Tools
JSON to Avro
Convert JSON data to Apache Avro format with automatic schema generation
Avro to JSON
Convert Apache Avro data to JSON format with schema validation
Avro Fixer
Fix and repair broken Avro schemas with automatic syntax error correction
Avro Schema Generator
Generate Apache Avro schemas from JSON data with automatic type inference
Avro Schema Validator
Validate Apache Avro schemas with syntax and structure checking for Kafka
Avro Formatter
Format and beautify Apache Avro schemas with proper indentation