Package io.github.microcks.util
Class AvroUtil
- java.lang.Object
-
- io.github.microcks.util.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.GenericRecordavroToAvroRecord(byte[] avroBinary, String avroSchema)Convert an Avro binary representation into an Avro GenericRecord object using specified schema.static org.apache.avro.generic.GenericRecordavroToAvroRecord(byte[] avroBinary, org.apache.avro.Schema avroSchema)Convert an Avro binary representation into an Avro GenericRecord object using specified schema.static StringavroToJson(byte[] avroBinary, String avroSchema)Convert an Avro binary representation into a JSON string using specified schema.static StringavroToJson(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.GenericRecordjsonToAvroRecord(String json, String avroSchema)Convert a JSON string into an Avro GenericRecord object using specified schema.static org.apache.avro.generic.GenericRecordjsonToAvroRecord(String json, org.apache.avro.Schema avroSchema)Convert a JSON string into an Avro GenericRecord object using specified schema.static booleanvalidate(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.
-
-
-
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 AvroavroSchema- 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 SchemaIOException- 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 AvroavroSchema- 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 SchemaIOException- 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 AvroavroSchema- 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 SchemaIOException- 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 AvroavroSchema- 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 SchemaIOException- 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 stringavroSchema- 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 SchemaIOException- 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 stringavroSchema- 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 SchemaIOException- if something goes wrong during conversion
-
avroToAvroRecord
public static org.apache.avro.generic.GenericRecord avroToAvroRecord(byte[] avroBinary, String avroSchema) throws org.apache.avro.AvroTypeException, IOExceptionConvert an Avro binary representation into an Avro GenericRecord object using specified schema.- Parameters:
avroBinary- An Avro binary representation to convert in recordavroSchema- 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 SchemaIOException- 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, IOExceptionConvert an Avro binary representation into an Avro GenericRecord object using specified schema.- Parameters:
avroBinary- An Avro binary representation to convert in recordavroSchema- 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 SchemaIOException- 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 againstdatum- 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 againstdatum- The datum object to validatefieldName- 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.
-
-