Skip to main content

Posts

Showing posts with the label AVRO example

Apache Avro - Serialization and Deserialization

This post is in continuation with my earlier posts on Apache Avro - Introduction and Apache Avro - Generating classes from Schema . In this post, we will discuss about reading (deserialization) and writing(serialization) of Avro generated classes. "Apache Avro™ is a data serialization system." We use DatumReader<T> and DatumWriter<T> for de-serialization and serialization of data, respectively. Apache Avro formats Apache Avro supports two formats, JSON and Binary . Let's move to an example using JSON format. Employee employee = Employee . newBuilder (). setFirstName ( "Gaurav" ). setLastName ( "Mazra" ). setSex ( SEX . MALE ). build (); DatumWriter < Employee > employeeWriter = new SpecificDatumWriter <>( Employee . class ); byte [] data ; try ( ByteArrayOutputStream baos = new ByteArrayOutputStream ()) { Encoder jsonEncoder = EncoderFactory . get (). jsonEncoder ( Employee . getClassSchema (), baos

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"