AVRO Reference API – Classes and Methods
1. Objective – Avro Reference API
Today, in this Apache Avro tutorial, we will see Avro reference API. There are some classes and methods which we use in the serialization as well as deserialization of Avro schemas. So, in this Avro article, “AVRO Reference API: Classes and Methods” we will learn all such important classes and methods in detail.
So, let’s begin with Introduction to AVRO Reference API.
2. What is AVRO Reference API
So, here is the list of some important classes and methods or AVRO Reference API:
a. SpecificDatumWriter Class
The class “SpecificDatumWriter Class” belongs to org.apache.avro.specific package. In order to convert Java objects into an in-memory serialized format, this class implements an interface called DatumWriter interface. Basically, that interface play role of a converter here.
This method gets the SpecificData implementation, which the writer uses.
b. SpecificDatumReader Class
The class “SpecificDatumReader Class” belongs to org.apache.avro.specific package. Basically, in order to read the data of a schema and to determine in-memory data representation, this method implements a DatumReader interface. In addition, this class supports generated java classes.
- SpecificDatumReader(Schema schema):
Where both the writer’s and reader’s schemas are the same, this constructor constructs.
- SpecificData getSpecificData():
This method gets the contained SpecificData.
- void setSchema(Schema actual):
In order to set the writer’s schema, we use this method.
Simply put, it instantiates DataFileWrite for emp class. Moreover, along with the schema in a file, this class writes a sequence of serialized records of data conforming to a schema.
- void append(D datum):
This method appends a datum to a file
- DataFileWriter<D> appendTo(File file):
In order to open a writer appending to an existing file, we use this method.
d. Data FileReader
In order to access the files which are written with DataFileWriter randomly, we use this class. Basically, it inherits the class DataFileStream.
DataFileReader(File file, DatumReader<D> reader))
This method reads the next datum in the file.
- Boolean hasNext():
Whereas, this method gets true if more entries remain in this file.
e. Class Schema.parser
Especially, for JSON-format schemas, this class is a parser. It consists of various methods to parse the schema and this class belongs to the package: org.apache.avro.
- parse (File file):
Basically, this method parses the schema which is provided in the given file.
- parse (InputStream in):
Moreover, this method [arses the schema which is provided in the given InputStream.
- parse (String s):
And, this method parses the schema which is provided in the given String.
f. Interface GenricRecord
The “Interface GenricRecord” offers methods to access the fields by name and index as well.
- Object get(String key):
The method “Object get(String key)”, gets the value of a field given.
- void put(String key, Object v):
And this method sets the value of a field given its name.
g. Class GenericData.Record
- Object get(String key)
Basically, this method gets the value of a field of the given name.
- Schema getSchema()
Further, this method gets the schema of this instance.
- void put(int i, Object v)
Moreover, this method sets the value of a field, given its position in the schema.
- void put(String key, Object value)
And, this method sets the value of a field, given its name.
So, this was all in Apache Avro Reference API. Hope you like our explanation.
3. Conclusion: AVRO Reference API
Hence, in this Avro reference AOI tutorial, we have seen all the important Classes and Methods which we use in the serialization as well as deserialization of Avro schemas, in detail. Hope it helps!
See also –