When entering the world of Apache Kafka, Apache Spark and data streams, sooner or later you will find mentioning of another Apache project; namely Apache AVRO. So ….
What is Apache Avro ?
Avro is a remote procedure call and data serialization framework developed within Apache’s Hadoop project (source: wikipedia). It is much the same as Apache Thrift and Google Protocol Buffers. Probably the main reason for Avro to gain in popularity is due to the fact that Hadoop-based big data platforms natively support serialization and deserialization of data in Avro format. Avro is based upon JSON based schemas and messages can be sent in both JSON and binary format. If binary is used then the schema is sent together with the actual data,
Playing with Apache Avro from the command line
So first let’s create a Avro schema “location.avsc” for the data records
{"namespace": "nl.rubix.avro", "type": "record", "name": "Location", "fields": [ {"name": "vehicle_id", "type": "string", "doc" : "id of the vehicle"}, {"name": "timestamp", "type": "long", "doc" : "time in seconds"}, {"name": "latitude", "type": "double"}, {"name": "longtitude", "type": "double"} ], "doc:" : "A schema for vehicle movement events" }
And we have this example file “location1.json” with a valid data record
{"vehicle_id": "1", "timestamp": 1476005672, "latitude": 51.687402, "longtitude": 5.307759}
Working with the Avro tools
Download the latest version of the Avro tools (currently 1.8.1) from the Avro Releases page.
$ java -jar avro-tools-1.8.1.jar Version 1.8.1 of Apache Avro Copyright 2010-2015 The Apache Software Foundation This product includes software developed at The Apache Software Foundation (http://www.apache.org/). ---------------- Available tools: cat extracts samples from files compile Generates Java code for the given schema. concat Concatenates avro files without re-compressing. fragtojson Renders a binary-encoded Avro datum as JSON. fromjson Reads JSON records and writes an Avro data file. fromtext Imports a text file into an avro data file. getmeta Prints out the metadata of an Avro data file. getschema Prints out schema of an Avro data file. idl Generates a JSON schema from an Avro IDL file idl2schemata Extract JSON schemata of the types from an Avro IDL file induce Induce schema/protocol from Java class/interface via reflection. jsontofrag Renders a JSON-encoded Avro datum as binary. random Creates a file with randomly generated instances of a schema. recodec Alters the codec of a data file. repair Recovers data from a corrupt Avro Data file rpcprotocol Output the protocol of a RPC service rpcreceive Opens an RPC Server and listens for one message. rpcsend Sends a single RPC message. tether Run a tethered mapreduce job. tojson Dumps an Avro data file as JSON, record per line or pretty. totext Converts an Avro data file to a text file. totrevni Converts an Avro data file to a Trevni file. trevni_meta Dumps a Trevni file's metadata as JSON. trevni_random Create a Trevni file filled with random instances of a schema. trevni_tojson Dumps a Trevni file as JSON.
Generate data record from JSON to Avro
$ java -jar avro-tools-1.8.1.jar fromjson --schema-file location.avsc location1.json > location.avro
The result is an output “location.avro” with the Avro binary. The interesting about Avro is that is encapsulates both the schema and the content in it’s binary message.
Objavro.schema#####ype":"record","name":"Location","namespace":"nl.rubix.avro","fields":[{"name":"vehicle_id","type":"string","doc":"id of the vehicle"},{"name":"timestamp","type":"long","doc":"time in seconds"},{"name":"latitude","type":"double"},{"name":"longtitude","type":"double"}],"doc:":"A schema for vehicle movement events"}avro.codenull~##5############.1м##
Retrieving the JSON message from Avro data
$ java -jar avro-tools-1.8.1.jar tojson location.avro > location_output.json
Retrieving the Avro schema from Avro data
And because the schema is present in the data we can retrieve the schema as well.
$ java -jar avro-tools-1.8.1.jar getschema location.avro > location_output.avsc
References:
- Apache Avro – Package org.apache.avro.tool 1.8.1
- Michael G. Noll – Reading and Writing Avro Files From the Command Line