Apache Avro - Generating classes from Schema

This post is in continuation to my previous post on Apache Avro - Introduction. In this post, we will discuss about generating classes from Schema.

How to create Apache Avro schema?

There are two ways to generate AVRO classes from Schema.

  • Pragmatically generating schema
  • Using maven Avro plugin

Consider we have following schema in "src/main/avro"

  "type" : "record",
  "name" : "Employee",
  "namespace" : "com.gauravbytes.avro",
  "doc" : "Schema to hold employee object",
  "fields" : [{
    "name" : "firstName",
    "type" : "string"
    "name" : "lastName",
    "type" : "string"
    "name" : "sex", 
    "type" : {
      "name" : "SEX",
      "type" : "enum",
      "symbols" : ["MALE", "FEMALE"]

Pragmatically generating classes

Classes can be generated for schema using SchemaCompiler.

public class PragmaticSchemaGeneration {
 private static final Logger LOGGER = LoggerFactory.getLogger(PragmaticSchemaGeneration.class);

 public static void main(String[] args) {
  try {
   SpecificCompiler compiler = new SpecificCompiler(new Schema.Parser().parse(new File("src/main/avro/employee.avsc")));
   compiler.compileToDestination(new File("src/main/avro"), new File("src/main/java"));
  } catch (IOException e) {
   LOGGER.error("Exception occurred parsing schema: ", e);

At line number 6, we create the object of SpecificComplier. It has two constructor, one take Protocolas an argument and other take Schema as an argument.

Using Maven plugin to generate schema

There is maven plugin which can generate schema for you. You need to add following configuration to your pom.xml.


This is how we can generate classes from Avro schema. I hope you find this post informative and helpful. You can find the full project on Github.

1 comment :