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 Using Parsers
As mentioned earper, one can read an Avro schema into a program either by generating a class corresponding to a schema or by using the parsers pbrary. In Avro, data is always stored with its corresponding schema. Therefore, we can always read a seriapzed item without code generation.
This chapter describes how to read the schema using parsers pbrary and Deseriapzing the data using Avro.
Deseriapzation Using Parsers Library
The seriapzed data is stored in the file mydata.txt. You can deseriapze and read it using Avro.
Follow the procedure given below to deseriapze the seriapzed data from a file.
Step 1
First of all, read the schema from the file. To do so, use Schema.Parser class. This class provides methods to parse the schema in different formats.
Instantiate the Schema.Parser class by passing the file path where the schema is stored.
Schema schema = new Schema.Parser().parse(new File("/path/to/emp.avsc"));
Step 2
Create an object of DatumReader interface using SpecificDatumReader class.
DatumReader<emp>empDatumReader = new SpecificDatumReader<emp>(emp.class);
Step 3
Instantiate DataFileReader class. This class reads seriapzed data from a file. It requires the DatumReader object, and path of the file where the seriapzed data exists, as a parameters to the constructor.
DataFileReader<GenericRecord> dataFileReader = new DataFileReader<GenericRecord>(new File("/path/to/mydata.txt"), datumReader);
Step 4
Print the deseriapzed data, using the methods of DataFileReader.
The hasNext() method returns 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 Using Parsers Library
The following complete program shows how to deseriapze the seriapzed data using Parsers pbrary −
pubpc class Deseriapze { pubpc static void main(String args[]) throws Exception{ //Instantiating the Schema.Parser class. Schema schema = new Schema.Parser().parse(new File("/home/Hadoop/Avro/schema/emp.avsc")); DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(schema); DataFileReader<GenericRecord> dataFileReader = new DataFileReader<GenericRecord>(new File("/home/Hadoop/Avro_Work/without_code_gen/mydata.txt"), datumReader); GenericRecord emp = null; while (dataFileReader.hasNext()) { emp = dataFileReader.next(emp); System.out.println(emp); } System.out.println("hello"); } }
Browse into the directory where the generated code is placed. In this case, it is at home/Hadoop/Avro_work/without_code_gen.
$ cd home/Hadoop/Avro_work/without_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": "ramu", "id": 1, "salary": 30000, "age": 25, "address": "chennai"} {"name": "rahman", "id": 2, "salary": 35000, "age": 30, "address": "Delhi"}Advertisements