HowTo: Implement Avro Schema Inheritance

Photo by Alvin Lenin on Unsplash

Avro Schema does not support Inheritance, but we can implement avro schema inheritance with some effort.

Before getting started, clone the git repo:

Implement Avro Schema Inheritance

Let’s see the implementation class:

This avro schema inheritance implementation reads the avro schema avsc files from the classpath and loads avro schema instances with avro schema name key.

There are avro avsc files in resources/META-INF/avro. Let’s see produce-request.avsc file:

Take a looke at the type “io.shunters.coda.avro.api.RequestHeader” and “io.shunters.coda.avro.api.Records” which will be replaced with the following avro schema json string.

request-header.avsc for the type “io.shunters.coda.avro.api.RequestHeader”:

records.avsc file for the type “io.shunters.coda.avro.api.Records”:

In this records.avsc file, the record type “io.shunters.coda.avro.api.Record” can be found which will be replaced with the corresponding avro schema json string like above.

Run Avro Schema Loader

Let’s invoke AvroSchemaLoader which is implemented here.

All the classpath avro avsc files contained in the list will be loaded as avro schema instances which can be found by the key of avro schema name, for instance, “io.shunters.coda.avro.api.ProduceRequest”.

You can also use the classpath directory from which all the avro avsc files will be loaded as avro schema instances:

Conclusion

Even though avro schema inheritance is not supported, the implementation of it can be done as mentioned above with which all the duplicated avro record schema string in the avsc files can be eliminated.

References

--

--

Founder of Cloud Chef Labs Inc.(http://www.cloudchef-labs.com) | Creator of Chango(http://bitly.ws/AMQf) and DataRoaster(https://bit.ly/3BM0ccA)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store