Updated Guide: Apache Avro™ with Java
This is a short guide for getting started with Apache Avro using Java. This guide only covers using Avro for data serialization;
📥 Download
Avro implementations for C, C++, C#, Java, PHP, Python, and Ruby can be downloaded from the Apache Avro™ Download page. This guide uses Avro 1.12.0, the latest version at the time of writing. For the examples in this guide, download avro-1.12.0.jar and avro-tools-1.12.0.jar.
Alternatively, if you are using Maven, add the following dependency to your pom.xml:
As well as the Avro Maven plugin (for performing code generation):
You may also build the required Avro jars from source. Building Avro is beyond the scope of this guide;
🧾 Defining a Schema
Avro schemas are defined using JSON or IDL (the latter requires an extra dependency). Schemas are composed of primitive types and complex types.
Here’s a simple schema example, student.avsc:
This schema defines a record representing a hypothetical student. At minimum, a record definition must include its type ("record"), a name ("Student"), and its fields: name, favorite_number, and favorite_color.
⚙️ Serializing and Deserializing with Code Generation
Compiling the Schema
You can generate Java classes from the schema using avro-tools:
This will generate the appropriate source files in a package based on the schema’s namespace.
👩💻 Creating Students
Avro objects can be created by constructors or builders. Builders provide schema validation and handle default values, while constructors are faster.
💾 Serializing
📂 Deserializing
Output:
🧪 Serializing and Deserializing Without Code Generation
You can also use GenericRecords to avoid code generation.
Serialize:
Deserialize:
Output:
🚀 Compiling and Running the Example
Navigate to the project directory and run:
For the generic version:
⚡ Beta Feature: Faster Code Generation
Enable faster encoding/decoding with:
No schema recompilation is required. This is a runtime toggle via a system property.
🧠 Summary
You’ve now learned how to:
-
Define a schema for a
Student -
Serialize/deserialize using Avro with and without code generation
-
Use Avro tools and Maven integration
-
Optimize performance with reuse patterns and optional beta features
Niciun comentariu:
Trimiteți un comentariu