AVRO Reference API – Classes and Methods

Placement-ready Online Courses: Your Passport to Excellence - Start Now

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.

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. 

  • Constructor

SpecificDatumWriter(Schema schema)

  • Method

SpecificData getSpecificData():
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.

  • Constructor
  1. SpecificDatumReader(Schema schema): 

Where both the writer’s and reader’s schemas are the same, this constructor constructs.

  • Methods
  1. SpecificData getSpecificData(): 

This method gets the contained SpecificData.

  1. void setSchema(Schema actual):

In order to set the writer’s schema, we use this method.

c. DataFileWriter

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.

  • Constructor

DataFileWriter(DatumWriter<D> dout)

  • Methods
  1. void append(D datum): 

This method appends a datum to a file

  1. 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.

  • Constructor

DataFileReader(File file, DatumReader<D> reader))

  • Methods
  1. next(): 

This method reads the next datum in the file.

  1. 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.

  • Constructor

Schema.Parser()

  • Methods
  1. parse (File file): 

Basically, this method parses the schema which is provided in the given file.

  1. parse (InputStream in):

Moreover, this method [arses the schema which is provided in the given InputStream.

  1. 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.

  • Methods
  1. Object get(String key): 

The method “Object get(String key)”, gets the value of a field given.

  1. void put(String key, Object v): 

And this method sets the value of a field given its name.

g. Class GenericData.Record

  • Constructor

GenericData.Record(Schema schema)

  • Methods
  1. Object get(String key)

Basically, this method gets the value of a field of the given name.

  1. Schema getSchema()

Further, this method gets the schema of this instance.

  1. void put(int i, Object v)

Moreover, this method sets the value of a field, given its position in the schema.

  1. 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.

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!

Your opinion matters
Please write your valuable feedback about DataFlair on Google

courses

DataFlair Team

DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.

Leave a Reply

Your email address will not be published. Required fields are marked *