Apache Avro
≈ 5 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
Avro is a row-oriented remote procedure call and data serialization framework developed within Apache's Hadoop project. It uses JSON for defining data types and protocols, and serializes data in a compact binary format. Its primary use is in Apache Hadoop, where it can provide both a serialization format for persistent data, and a wire format for communication between Hadoop nodes, and from client programs to the Hadoop services. Avro uses a schema to structure the data that is being encoded. It has two different types of schema languages: one for human editing (Avro IDL) and another which is more machine-readable based on JSON. It is similar to Thrift and Protocol Buffers, but does not require running a code-generation program when a schema changes (unless desired for statically-typed languages). Apache Spark SQL can access Avro as a data source.
Avro is a row-oriented remote procedure call and data serialization framework developed within Apache's Hadoop project. It uses JSON for defining data types and protocols, and serializes data in a compact binary format. Its primary use is in Apache Hadoop, where it can provide both a serialization format for persistent data, and a wire format for communication between Hadoop nodes, and from client programs to the Hadoop services. Avro uses a schema to structure the data that is being encoded. It has two different types of schema languages: one for human editing (Avro IDL) and another which is more machine-readable based on JSON. It is similar to Thrift and Protocol Buffers, but does not require running a code-generation program when a schema changes (unless desired for statically-typed languages). Apache Spark SQL can access Avro as a data source.
Avro Object Container File
An Avro Object Container File consists of: A file header, followed by one or more file data blocks. A file header consists of: Four bytes, ASCII 'O', 'b', 'j', followed by the Avro version number which is 1 (0x01) (Binary values 0x4F 0x62 0x6A 0x01). File metadata, including the schema definition. The 16-byte, randomly-generated sync marker for this file. For data blocks Avro specifies two serialization encodings: binary and JSON. Most applications will use the binary encoding, as it is smaller and faster. For debugging and web-based applications, the JSON encoding may sometimes be appropriate. { "namespace": "example.avro", "type": "record", "name": "User", "fields": [ {"name": "name", "type": "string"}, {"name": "favorite_number", "type": ["null", "int"]}, {"name": "favorite_color", "type": ["null", "string"]} ] } import avro.schema from avro.datafile import DataFileReader, DataFileWriter from avro.io import DatumReader, DatumWriter Need to know the schema to write. According to 1.8.2 of Apache Avro schema = avro.schema.parse(open("user.avsc", "rb").read()) writer = DataFileWriter(open("users.avro", "wb"), DatumWriter(), schema) writer.append({"name": "Alyssa", "favorite_number": 256}) writer.append({"name": "Ben", "favorite_number": 8, "favorite_color": "red"}) writer.close() File "users.avro" will contain the schema in JSON and a compact binary representation of the data: $ od -v -t x1z users.avro 0000000 4f 62 6a 01 04 14 61 76 72 6f 2e 63 6f 64 65 63 >Obj...avro.codec .null.avro.schem a..{"type": "rec ord", "name": "U ser", "namespace ": "example.avro ", "fields": [{" type": "string", "name": "name"} , {"type": ["int ", "null"], "nam e": "favorite_nu mber"}, {"type": ["string", "nul l"], "name": "fa vorite_color"}]} ......GTb.h...B. $.,.Alyssa.....B en....red.....GT b.h...B.$ Deserialization: The schema is embedded in the data file reader = DataFileReader(open("users.avro", "rb"), DatumReader()) for user in reader: print(user) reader.close() This outputs: {'name': 'Alyssa', 'favorite_number': 256, 'favorite_color': None} {'name': 'Ben', 'favorite_number': 8, 'favorite_color': 'red'}
Avro IDL
In addition to supporting JSON for type and protocol definitions, Avro includes experimental support for an alternative interface description language (IDL) syntax known as Avro IDL. Previously known as GenAvro, this format is designed to ease adoption by users familiar with more traditional IDLs and programming languages, with a syntax similar to C/C++, Protocol Buffers and others.
Logo
The original Apache Avro logo was from the defunct British aircraft manufacturer Avro (originally A.V. Roe and Company). The Apache Avro logo was updated to an original design in late 2023.
See also
Comparison of data serialization formats Apache Thrift Protocol Buffers Etch (protocol) Internet Communications Engine MessagePack CBOR
References
Further reading
Text derived from https://en.wikipedia.org/w/index.php?oldid=1371118393 under CC BY-SA 4.0; modifications have been made.