Class AvroUtil


  • public class AvroUtil
    extends Object
    Helper class using utility methods for converting Avro format from and to JSON.
    Author:
    laurent
    • Constructor Summary

      Constructors 
      Constructor Description
      AvroUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.apache.avro.generic.GenericRecord avroToAvroRecord​(byte[] avroBinary, String avroSchema)
      Convert an Avro binary representation into an Avro GenericRecord object using specified schema.
      static org.apache.avro.generic.GenericRecord avroToAvroRecord​(byte[] avroBinary, org.apache.avro.Schema avroSchema)
      Convert an Avro binary representation into an Avro GenericRecord object using specified schema.
      static String avroToJson​(byte[] avroBinary, String avroSchema)
      Convert an Avro binary representation into a JSON string using specified schema.
      static String avroToJson​(byte[] avroBinary, org.apache.avro.Schema avroSchema)
      Convert an Avro binary representation into a JSON string using specified schema.
      static List<String> getValidationErrors​(org.apache.avro.Schema schema, Object datum, String... fieldName)
      Get validation errors of a datum object regarding Avro schema.
      static byte[] jsonToAvro​(String json, String avroSchema)
      Convert a JSON string into an Avro binary representation using specified schema.
      static byte[] jsonToAvro​(String json, org.apache.avro.Schema avroSchema)
      Convert a JSON string into an Avro binary representation using specified schema.
      static org.apache.avro.generic.GenericRecord jsonToAvroRecord​(String json, String avroSchema)
      Convert a JSON string into an Avro GenericRecord object using specified schema.
      static org.apache.avro.generic.GenericRecord jsonToAvroRecord​(String json, org.apache.avro.Schema avroSchema)
      Convert a JSON string into an Avro GenericRecord object using specified schema.
      static boolean validate​(org.apache.avro.Schema schema, Object datum)
      Validate that a datum object (typically a GenericRecord read somewhere but the method signature is loosely coupled to make it recursive friendly) is compliant with an Avro schema.
    • Constructor Detail

      • AvroUtil

        public AvroUtil()
    • Method Detail

      • jsonToAvro

        public static byte[] jsonToAvro​(String json,
                                        String avroSchema)
                                 throws org.apache.avro.AvroTypeException,
                                        IOException
        Convert a JSON string into an Avro binary representation using specified schema.
        Parameters:
        json - A JSON string to convert to Avro
        avroSchema - String representation of an Avro Schema to use for conversion
        Returns:
        The Avro binary representation of JSON
        Throws:
        org.apache.avro.AvroTypeException - if there's a mismatch between JSON string and Avro Schema
        IOException - if something goes wrong during conversion
      • jsonToAvro

        public static byte[] jsonToAvro​(String json,
                                        org.apache.avro.Schema avroSchema)
                                 throws org.apache.avro.AvroTypeException,
                                        IOException
        Convert a JSON string into an Avro binary representation using specified schema.
        Parameters:
        json - A JSON string to convert to Avro
        avroSchema - The Avro Schema to use for conversion
        Returns:
        The Avro binary representation of JSON
        Throws:
        org.apache.avro.AvroTypeException - if there's a mismatch between JSON string and Avro Schema
        IOException - if something goes wrong during conversion
      • jsonToAvroRecord

        public static org.apache.avro.generic.GenericRecord jsonToAvroRecord​(String json,
                                                                             String avroSchema)
                                                                      throws org.apache.avro.AvroTypeException,
                                                                             IOException
        Convert a JSON string into an Avro GenericRecord object using specified schema.
        Parameters:
        json - A JSON string to convert to Avro
        avroSchema - String representation of an Avro Schema to use for conversion
        Returns:
        The GenericRecord representation of JSON
        Throws:
        org.apache.avro.AvroTypeException - if there's a mismatch between JSON string and Avro Schema
        IOException - if something goes wrong during conversion
      • jsonToAvroRecord

        public static org.apache.avro.generic.GenericRecord jsonToAvroRecord​(String json,
                                                                             org.apache.avro.Schema avroSchema)
                                                                      throws org.apache.avro.AvroTypeException,
                                                                             IOException
        Convert a JSON string into an Avro GenericRecord object using specified schema.
        Parameters:
        json - A JSON string to convert to Avro
        avroSchema - The Avro Schema to use for conversion
        Returns:
        The GenericRecord representation of JSON
        Throws:
        org.apache.avro.AvroTypeException - if there's a mismatch between JSON string and Avro Schema
        IOException - if something goes wrong during conversion
      • avroToJson

        public static String avroToJson​(byte[] avroBinary,
                                        String avroSchema)
                                 throws org.apache.avro.AvroTypeException,
                                        IOException
        Convert an Avro binary representation into a JSON string using specified schema.
        Parameters:
        avroBinary - An Avro binary representation to convert in JSON string
        avroSchema - The Avro Schema to use for conversion
        Returns:
        The JSON string representing Avro binary
        Throws:
        org.apache.avro.AvroTypeException - if there's a mismatch between Avro binary and Schema
        IOException - if something goes wrong during conversion
      • avroToJson

        public static String avroToJson​(byte[] avroBinary,
                                        org.apache.avro.Schema avroSchema)
                                 throws org.apache.avro.AvroTypeException,
                                        IOException
        Convert an Avro binary representation into a JSON string using specified schema.
        Parameters:
        avroBinary - An Avro binary representation to convert in JSON string
        avroSchema - The Avro Schema to use for conversion
        Returns:
        The JSON string representing Avro binary
        Throws:
        org.apache.avro.AvroTypeException - if there's a mismatch between Avro binary and Schema
        IOException - if something goes wrong during conversion
      • avroToAvroRecord

        public static org.apache.avro.generic.GenericRecord avroToAvroRecord​(byte[] avroBinary,
                                                                             String avroSchema)
                                                                      throws org.apache.avro.AvroTypeException,
                                                                             IOException
        Convert an Avro binary representation into an Avro GenericRecord object using specified schema.
        Parameters:
        avroBinary - An Avro binary representation to convert in record
        avroSchema - The Avro Schema to use for conversion
        Returns:
        The JSON string representing Avro binary
        Throws:
        org.apache.avro.AvroTypeException - if there's a mismatch between Avro binary and Schema
        IOException - if something goes wrong during conversion
      • avroToAvroRecord

        public static org.apache.avro.generic.GenericRecord avroToAvroRecord​(byte[] avroBinary,
                                                                             org.apache.avro.Schema avroSchema)
                                                                      throws org.apache.avro.AvroTypeException,
                                                                             IOException
        Convert an Avro binary representation into an Avro GenericRecord object using specified schema.
        Parameters:
        avroBinary - An Avro binary representation to convert in record
        avroSchema - The Avro Schema to use for conversion
        Returns:
        The JSON string representing Avro binary
        Throws:
        org.apache.avro.AvroTypeException - if there's a mismatch between Avro binary and Schema
        IOException - if something goes wrong during conversion
      • validate

        public static boolean validate​(org.apache.avro.Schema schema,
                                       Object datum)
        Validate that a datum object (typically a GenericRecord read somewhere but the method signature is loosely coupled to make it recursive friendly) is compliant with an Avro schema.
        Parameters:
        schema - The Avro Schema to validate datum against
        datum - The Object datum to validate
        Returns:
        True if the object is compliant with supplied schema, false otherwise.
      • getValidationErrors

        public static List<String> getValidationErrors​(org.apache.avro.Schema schema,
                                                       Object datum,
                                                       String... fieldName)
        Get validation errors of a datum object regarding Avro schema.
        Parameters:
        schema - The Schema to check datum object against
        datum - The datum object to validate
        fieldName - The name of the field we're currently validating
        Returns:
        A list of String representing validation errors. List may be empty if no error found.