Apache AVRO - Generating classes from Schema

We will start with various ways to generate classes from Avro schema and end with example code.

Previous posts

1. Apache Avro schema

There are two ways to generate AVRO classes from Schema.

Let's generate Avro schema first to be used later in pragmatic generation or using maven plugin.

{
"type" : "record",
"name" : "Employee",
"namespace" : "cc.gaurav.blog.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"]
}
}]
}

1.1. Pragmatically generating schema

Classes can be generated for schema using SchemaCompiler. It has two constructors, one take Protocolas an argument and other take Schema as an argument.

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

public static void main(String[] args) {
try {
// Point 1
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);
}
}
}

1.2. 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.

<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>${avro.version}</version>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
<goal>protocol</goal>
<goal>idl-protocol</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

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.



Tags: Apache AVRO, Apache AVRO schema, Apache AVRO auto generation classes from Schema

← Back home