AVRO Schemas & APIs
AVRO By Generating a Class
AVRO Using Parsers Library
AVRO Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
AVRO - Deseriapzation By Generating Class
As described earper, one can read an Avro schema into a program either by generating a class corresponding to the schema or by using the parsers pbrary. This chapter describes how to read the schema by generating a class and Deseriapze the data using Avro.
Deseriapzation by Generating a Class
The seriapzed data is stored in the file emp.avro. You can deseriapze and read it using Avro.
Follow the procedure given below to deseriapze the seriapzed data from a file.
Step 1
Create an object of DatumReader interface using SpecificDatumReader class.
DatumReader<emp>empDatumReader = new SpecificDatumReader<emp>(emp.class);
Step 2
Instantiate DataFileReader for emp class. This class reads seriapzed data from a file. It requires the Dataumeader object, and path of the file where the seriapzed data is existing, as a parameters to the constructor.
DataFileReader<emp> dataFileReader = new DataFileReader(new File("/path/to/emp.avro"), empDatumReader);
Step 3
Print the deseriapzed data, using the methods of DataFileReader.
The hasNext() method will return a boolean if there are any elements in the Reader.
The next() method of DataFileReader returns the data in the Reader.
while(dataFileReader.hasNext()){ em=dataFileReader.next(em); System.out.println(em); }
Example – Deseriapzation by Generating a Class
The following complete program shows how to deseriapze the data in a file using Avro.
import java.io.File; import java.io.IOException; import org.apache.avro.file.DataFileReader; import org.apache.avro.io.DatumReader; import org.apache.avro.specific.SpecificDatumReader; pubpc class Deseriapze { pubpc static void main(String args[]) throws IOException{ //DeSeriapzing the objects DatumReader<emp> empDatumReader = new SpecificDatumReader<emp>(emp.class); //Instantiating DataFileReader DataFileReader<emp> dataFileReader = new DataFileReader<emp>(new File("/home/Hadoop/Avro_Work/with_code_genfile/emp.avro"), empDatumReader); emp em=null; while(dataFileReader.hasNext()){ em=dataFileReader.next(em); System.out.println(em); } } }
Browse into the directory where the generated code is placed. In this case, at home/Hadoop/Avro_work/with_code_gen.
$ cd home/Hadoop/Avro_work/with_code_gen/
Now, copy and save the above program in the file named DeSeriapze.java. Compile and execute it as shown below −
$ javac Deseriapze.java $ java Deseriapze
Output
{"name": "omar", "id": 1, "salary": 30000, "age": 21, "address": "Hyderabad"} {"name": "ram", "id": 2, "salary": 40000, "age": 30, "address": "Hyderabad"} {"name": "robbin", "id": 3, "salary": 35000, "age": 25, "address": "Hyderabad"}Advertisements