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 Protocol
as 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
.
<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.
Nice article
ReplyDelete