How to create LogicalType in Apache Avro 
Tags: apacheavro, gradle, java, logicaltype
Published: 2020-12-21 20:24:37


---
title: How to create LogicalType in Apache Avro 
published: true
description: I am writing this post to understand how to create a custom LogicalType in Apache Avro. It helps in enforcing strict schema validations for the data in transit as well as rest within their systems. Dividing this into broader sections: Define a schema using Avro IDL Generate Java classes using gradle avro plugin Define Custom… Read More &raquo;
tags: ApacheAvro, gradle, java, LogicalType
canonical_url: https://anilkulkarni.com/2020/12/how-to-create-logicaltype-in-apache-avro/
cover_image: https://i1.wp.com/anilkulkarni.com/wp-content/uploads/2020/12/Avro-IDL.jpg?fit=320%2C204&ssl=1
---

I am writing this post to understand how to create a custom LogicalType in Apache Avro. It helps in enforcing strict schema validations for the data in transit as well as rest within their systems. Dividing this into broader sections:

1. Define a schema using Avro IDL
2. Generate Java classes using gradle avro plugin
3. Define Custom Logical Type
4. Test the Custom LogicalType
5. Next Steps (There is always a better way)

![LogicalType in Avro](https://i1.wp.com/anilkulkarni.com/wp-content/uploads/2020/12/Avro-IDL.jpg?resize=320%2C204&ssl=1)Define schema using Avro IDLLets go over in detail how to create a Custom LogicalType in Apache Avro.

1. **Define a schema using Avro IDL**Any simple to complex schema can be defined easily using Avro IDL. Maven and Gradle plugins which aid in generation .avsc files and java classes for the defined schema. Below is a sample [schema ](https://github.com/anilkulkarni87/AvroLogicalType/blob/master/src/main/java/com/lina/avdl/query.avdl)defined by me. Read more about [Avro IDL](https://avro.apache.org/docs/current/idl.html) here. As a standard practice we usually define default value for our fields to accommodate for backward compatibility whenever schema changes.
2. **Generate Java classes and schema file** Gradle avro plugin to our rescue. The plugin has been updated many times. You might notice that what I have is an older version. The first task creates Avro protocol files, next task created schema files (.avsc files), The final task created the required java classes.
  The complete build file can be found [here](https://github.com/anilkulkarni87/AvroLogicalType/blob/master/build.gradle). ![Gradle Avro Plugin](https://i2.wp.com/anilkulkarni.com/wp-content/uploads/2020/12/gradle-avro-plugin.jpg?w=665&ssl=1)
3. **Define Custom LogicalType**A logical type is an Avro primitive or complex type with extra attributes to represent a derived type. This basically is a custom defined type and can be leveraged for many use cases. Notice the “reversed” keyword in the .avdl file in Step 1. 
  We need to define the LogicalType and the custom conversion for it. For example, What I am doing here is appending the data ingested as part of ‘*queryAuthor*‘ field with the word ‘*reversed*‘.
  Use Cases:
  -Create a type for encryption which encrypts the data ingested as well as decrypts the field later. 
  -Create definition for Address (Line1, Line2, Line3, State etc ) and use that as Custom Logical type. 
  -Choose to do a one way hash of the data ingested.
  You can find the LogicalType and Conversion class here:
  Logical Type: [Click here](https://github.com/anilkulkarni87/AvroLogicalType/blob/master/src/main/java/com/lina/customAvro/ReversedLogicalType.java)
  Conversion: [Click Here](https://github.com/anilkulkarni87/AvroLogicalType/blob/master/src/main/java/com/lina/customAvro/ReversedConversion.java)
  
  Another important steps is to register this Logical Type which i am doing in the main method. 
  `public class AvroExample {<br></br>public static void main(String[] args) {<br></br>LogicalTypes.register(ReversedLogicalType.REVERSED_LOGICAL_TYPE_NAME, new LogicalTypes.LogicalTypeFactory() {<br></br>private final LogicalType reversedLogicalType = new ReversedLogicalType();<br></br>@Override<br></br>public LogicalType fromSchema(Schema schema) {<br></br>return reversedLogicalType;<br></br>}<br></br>});`
  What I am doing in the Custom conversion is self explanatory if you look at the code below.
  ![Image of Custom conversion class ](https://i0.wp.com/anilkulkarni.com/wp-content/uploads/2020/12/CustomConversion.jpg?w=665&ssl=1)
4. **Test the Custom LogicalType**My approach to test the Logical Type is:
  *a) Write data to .avro file.
  b) Read the data in the file.
  c) Read the files using avro tools*
  Better explained with some screenshots:
  In the main method I am creating an avro file and we shall see the value before the data is saved as .avro file. Then we read the data from the file and print it before conversion happens. 
  
  ![Testing Logical Type](https://i1.wp.com/anilkulkarni.com/wp-content/uploads/2020/12/Testing-LogicalType.jpg?w=665&ssl=1)
  We also see the data in the avro file using avro-tools.
  ![Avro-tools](https://i1.wp.com/anilkulkarni.com/wp-content/uploads/2020/12/Avrotools-cmdPretty.jpg?resize=282%2C130&ssl=1)
5. **Next Steps or TODO:** There is always a better way to do it. I am going to work on these and probably come up with another post
  *– Github link is [here](https://github.com/anilkulkarni87/AvroLogicalType).
  – Leverage latest version of plugin to register the LogicalType as part of the build. 
  – Notice the TODO in [QueryRecord.java](https://github.com/anilkulkarni87/AvroLogicalType/blob/master/src/main/java/com/lina/query/QueryRecord.java). 
  – Explore modifying velocity templates to include the LogicalTypes in the auto-generated code.*
  *– Some of my other [learnings](https://anilkulkarni.com/2019/06/how-to-view-spark-history-logs-locally/).*

The post [How to create LogicalType in Apache Avro](https://anilkulkarni.com/2020/12/how-to-create-logicaltype-in-apache-avro/) appeared first on [Anil Kulkarni | Blog](https://anilkulkarni.com).