Class AvroObjectConverter


  • public class AvroObjectConverter
    extends Object

    Utility class that converts Avro objects into similar objects and vice versa by using reflection. The converter methods expect that both classes have the same fields with the same name. The respective field types can be different. When converting an object into its Avro counterpart the fields of type Date and GregorianCalendar are converted to Instant. The converter also supports converting complex structures (e.g. an Avro object that contains a field of type SpecificRecordBase or a list with SpecificRecordBase objects). If different enum fields must be converted the literals of both enums must be equal!

    Copyright 2021 (C) by Martin Ganserer

    Version:
    1.0.0
    Author:
    Martin Ganserer
    • Method Detail

      • toAvro

        public static <T extends org.apache.avro.specific.SpecificRecordBase> T toAvro​(Object object,
                                                                                       Class<T> avroType)
        Convert the given object into an Avro object
        Type Parameters:
        T - the desired Avro type
        Parameters:
        object - the source object
        avroType - the Avro class
        Returns:
        the converted Avro object
        Throws:
        AvroObjectConversionException - if the conversion has failed
      • toAvroList

        public static <T extends org.apache.avro.specific.SpecificRecordBase,​S> List<T> toAvroList​(List<S> objectList,
                                                                                                         Class<T> avroType)
        Convert an object list into a list of Avro objects
        Type Parameters:
        T - the desired Avro type
        S - the type of the objects that are contained in the list
        Parameters:
        objectList - the list that contains all objects that should be converted
        avroType - the Avro class
        Returns:
        a list with the converted Avro objects
        Throws:
        AvroObjectConversionException - if the conversion has failed
      • toObject

        public static <T> T toObject​(org.apache.avro.specific.SpecificRecordBase record,
                                     Class<T> type)
        Convert the given Avro object into an object of the desired type
        Type Parameters:
        T - the desired object type
        Parameters:
        record - the Avro object to be converted
        type - the object class
        Returns:
        the target object
        Throws:
        AvroObjectConversionException - if the conversion has failed
      • toObjectList

        public static <T,​S extends org.apache.avro.specific.SpecificRecordBase> List<T> toObjectList​(List<S> avroList,
                                                                                                           Class<T> objectType)
        Convert an Avro object list into a list of objects
        Type Parameters:
        T - the desired object type
        S - the type of the objects that are contained in the list
        Parameters:
        avroList - the list that contains all Avro objects that should be converted
        objectType - the object class
        Returns:
        a list with the converted objects
        Throws:
        AvroObjectConversionException - if the conversion has failed