com.adgear.anoa.read.AvroDecoders Maven / Gradle / Ivy
package com.adgear.anoa.read;
import com.adgear.anoa.Anoa;
import com.adgear.anoa.AnoaHandler;
import com.fasterxml.jackson.core.JsonParser;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.generic.IndexedRecord;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.JsonDecoder;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificRecord;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Utility class for generating functions for deserializing Avro records. Unless specified
* otherwise, the functions should not be deemed thread-safe.
*/
final public class AvroDecoders {
private AvroDecoders() {
}
/**
* @param schema Avro record schema
* @param supplier used for record instantiation
* @return A function which deserializes an Avro record from its binary encoding
*/
static public Function binary(
Schema schema,
Supplier supplier) {
return binary(new GenericDatumReader<>(schema), supplier);
}
/**
* @param schema Avro record schema
* @return A function which deserializes an Avro record from its binary encoding
*/
static public Function binary(
Schema schema) {
return binary(new GenericDatumReader<>(schema));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param schema Avro record schema
* @param supplier used for record instantiation
* @param Metadata type
* @return A function which deserializes an Avro record from its binary encoding
*/
static public Function, Anoa> binary(
AnoaHandler anoaHandler,
Schema schema,
Supplier supplier) {
return binary(anoaHandler, new GenericDatumReader<>(schema), supplier);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param schema Avro record schema
* @param Metadata type
* @return A function which deserializes an Avro record from its binary encoding
*/
static public Function, Anoa> binary(
AnoaHandler anoaHandler,
Schema schema) {
return binary(anoaHandler, new GenericDatumReader<>(schema));
}
/**
* @param writer Avro schema with which the record was originally serialized
* @param reader Avro schema to use for deserialization
* @param supplier used for record instantiation
* @return A function which deserializes an Avro record from its binary encoding
*/
static public Function binary(
Schema writer,
Schema reader,
Supplier supplier) {
return binary(new GenericDatumReader<>(writer, reader), supplier);
}
/**
* @param writer Avro schema with which the record was originally serialized
* @param reader Avro schema to use for deserialization
* @return A function which deserializes an Avro record from its binary encoding
*/
static public Function binary(
Schema writer,
Schema reader) {
return binary(new GenericDatumReader<>(writer, reader));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param writer Avro schema with which the record was originally serialized
* @param reader Avro schema to use for deserialization
* @param supplier used for record instantiation
* @param Metadata type
* @return A function which deserializes an Avro record from its binary encoding
*/
static public Function, Anoa> binary(
AnoaHandler anoaHandler,
Schema writer,
Schema reader,
Supplier supplier) {
return binary(anoaHandler, new GenericDatumReader<>(writer, reader), supplier);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param writer Avro schema with which the record was originally serialized
* @param reader Avro schema to use for deserialization
* @param Metadata type
* @return A function which deserializes an Avro record from its binary encoding
*/
static public Function, Anoa> binary(
AnoaHandler anoaHandler,
Schema writer,
Schema reader) {
return binary(anoaHandler, new GenericDatumReader<>(writer, reader));
}
/**
* @param recordClass Avro SpecificRecord class object
* @param supplier used for record instantiation
* @param Avro SpecificRecord record type
* @return A function which deserializes an Avro record from its binary encoding
*/
static public Function binary(
Class recordClass,
Supplier supplier) {
return binary(new SpecificDatumReader<>(recordClass), supplier);
}
/**
* @param recordClass Avro SpecificRecord class object
* @param Avro SpecificRecord record type
* @return A function which deserializes an Avro record from its binary encoding
*/
static public Function binary(
Class recordClass) {
return binary(new SpecificDatumReader<>(recordClass));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Avro SpecificRecord class object
* @param supplier used for record instantiation
* @param Avro SpecificRecord record type
* @param Metadata type
* @return A function which deserializes an Avro record from its binary encoding
*/
static public Function, Anoa> binary(
AnoaHandler anoaHandler,
Class recordClass,
Supplier supplier) {
return binary(anoaHandler, new SpecificDatumReader<>(recordClass), supplier);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Avro SpecificRecord class object
* @param Avro SpecificRecord record type
* @param Metadata type
* @return A function which deserializes an Avro record from its binary encoding
*/
static public Function, Anoa> binary(
AnoaHandler anoaHandler,
Class recordClass) {
return binary(anoaHandler, new SpecificDatumReader<>(recordClass));
}
static Function binary(
GenericDatumReader reader,
Supplier supplier) {
BinaryDecoderWrapper decoderWrapper = new BinaryDecoderWrapper();
return (byte[] in) -> {
try {
return reader.read(supplier.get(), decoderWrapper.getDecoder(in));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}
static Function binary(
GenericDatumReader reader) {
return binary(reader, () -> ((R) null));
}
static Function, Anoa> binary(
AnoaHandler anoaHandler,
GenericDatumReader reader,
Supplier supplier) {
BinaryDecoderWrapper decoderWrapper = new BinaryDecoderWrapper();
return anoaHandler.functionChecked(
(byte[] in) -> reader.read(supplier.get(), decoderWrapper.getDecoder(in)));
}
static Function, Anoa> binary(
AnoaHandler anoaHandler,
GenericDatumReader reader) {
return binary(anoaHandler, reader, () -> ((R) null));
}
/**
* @param schema Avro record schema
* @param supplier used for record instantiation
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public Function json(
Schema schema,
Supplier supplier) {
return json(new GenericDatumReader<>(schema), supplier);
}
/**
* @param schema Avro record schema
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public Function json(
Schema schema) {
return json(new GenericDatumReader<>(schema));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param schema Avro record schema
* @param supplier used for record instantiation
* @param Metadata type
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public Function, Anoa> json(
AnoaHandler anoaHandler,
Schema schema,
Supplier supplier) {
return json(anoaHandler, new GenericDatumReader<>(schema), supplier);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param schema Avro record schema
* @param Metadata type
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public Function, Anoa> json(
AnoaHandler anoaHandler,
Schema schema) {
return json(anoaHandler, new GenericDatumReader<>(schema));
}
/**
* @param writer Avro schema with which the record was originally serialized
* @param reader Avro schema to use for deserialization
* @param supplier used for record instantiation
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public Function json(
Schema writer,
Schema reader,
Supplier supplier) {
return json(new GenericDatumReader<>(writer, reader), supplier);
}
/**
* @param writer Avro schema with which the record was originally serialized
* @param reader Avro schema to use for deserialization
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public Function json(
Schema writer,
Schema reader) {
return json(new GenericDatumReader<>(writer, reader));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param writer Avro schema with which the record was originally serialized
* @param reader Avro schema to use for deserialization
* @param supplier used for record instantiation
* @param Metadata type
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public Function, Anoa> json(
AnoaHandler anoaHandler,
Schema writer,
Schema reader,
Supplier supplier) {
return json(anoaHandler, new GenericDatumReader<>(writer, reader), supplier);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param writer Avro schema with which the record was originally serialized
* @param reader Avro schema to use for deserialization
* @param Metadata type
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public Function, Anoa> json(
AnoaHandler anoaHandler,
Schema writer,
Schema reader) {
return json(anoaHandler, new GenericDatumReader<>(writer, reader));
}
/**
* @param recordClass Avro SpecificRecord class object
* @param supplier used for record instantiation
* @param Avro SpecificRecord record type
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public Function json(
Class recordClass,
Supplier supplier) {
return json(new SpecificDatumReader<>(recordClass), supplier);
}
/**
* @param recordClass Avro SpecificRecord class object
* @param Avro SpecificRecord record type
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public Function json(
Class recordClass) {
return json(new SpecificDatumReader<>(recordClass));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Avro SpecificRecord class object
* @param supplier used for record instantiation
* @param Avro SpecificRecord record type
* @param Metadata type
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public Function, Anoa> json(
AnoaHandler anoaHandler,
Class recordClass,
Supplier supplier) {
return json(anoaHandler, new SpecificDatumReader<>(recordClass), supplier);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Avro SpecificRecord class object
* @param Avro SpecificRecord record type
* @param Metadata type
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public Function, Anoa> json(
AnoaHandler anoaHandler,
Class recordClass) {
return json(anoaHandler, new SpecificDatumReader<>(recordClass));
}
static protected JsonDecoder createJsonDecoder(Schema schema) {
try {
return DecoderFactory.get().jsonDecoder(schema, "");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
static Function json(
GenericDatumReader reader,
Supplier supplier) {
final JsonDecoder jsonDecoder = createJsonDecoder(reader.getSchema());
return (String in) -> {
try {
jsonDecoder.configure(in);
return reader.read(supplier.get(), jsonDecoder);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}
static Function json(
GenericDatumReader reader) {
return json(reader, () -> ((R) null));
}
static Function, Anoa> json(
AnoaHandler anoaHandler,
GenericDatumReader reader,
Supplier supplier) {
final JsonDecoder jsonDecoder = createJsonDecoder(reader.getSchema());
return anoaHandler.functionChecked((String in) -> {
jsonDecoder.configure(in);
return reader.read(supplier.get(), jsonDecoder);
});
}
static Function, Anoa> json(
AnoaHandler anoaHandler,
GenericDatumReader reader) {
return json(anoaHandler, reader, () -> ((R) null));
}
/**
* @param schema Avro record schema
* @param Jackson JsonParser type
* @return A function which reads an Avro record from a JsonParser, in its 'natural' encoding.
*/
static public
Function
jackson(
Schema schema) {
return new AvroReader.GenericReader(schema).decoder();
}
/**
* @param schema Avro record schema
* @param
Jackson JsonParser type
* @return A function which reads an Avro record from a JsonParser, in its 'natural' encoding,
* with strictest possible type checking.
*/
static public
Function
jacksonStrict(
Schema schema) {
return new AvroReader.GenericReader(schema).decoderStrict();
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param schema Avro record Schema
* @param
Jackson JsonParser type
* @param Metadata type
* @return A function which reads an Avro record from a JsonParser, in its 'natural' encoding.
*/
static public
Function, Anoa> jackson(
AnoaHandler anoaHandler,
Schema schema) {
return new AvroReader.GenericReader(schema).decoder(anoaHandler);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param schema Avro record Schema
* @param Jackson JsonParser type
* @param Metadata type
* @return A function which reads an Avro record from a JsonParser, in its 'natural' encoding,
* with strictest possible type checking.
*/
static public
Function, Anoa> jacksonStrict(
AnoaHandler anoaHandler,
Schema schema) {
return new AvroReader.GenericReader(schema).decoderStrict(anoaHandler);
}
/**
* @param recordClass Avro SpecificRecord class object
* @param Jackson JsonParser type
* @param Avro SpecificRecord record type
* @return A function which reads an Avro record from a JsonParser, in its 'natural' encoding
*/
static public Function
jackson(
Class recordClass) {
return new AvroReader.SpecificReader<>(recordClass).decoder();
}
/**
* @param recordClass Avro SpecificRecord class object
* @param Jackson JsonParser type
* @param Avro SpecificRecord record type
* @return A function which reads an Avro record from a JsonParser, in its 'natural' encoding,
* with strictest possible type checking.
*/
static public Function
jacksonStrict(
Class recordClass) {
return new AvroReader.SpecificReader<>(recordClass).decoderStrict();
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Avro SpecificRecord class object
* @param Jackson JsonParser type
* @param Avro SpecificRecord record type
* @param Metadata type
* @return A function which reads an Avro record from a JsonParser, in its 'natural' encoding
*/
static public
Function, Anoa> jackson(
AnoaHandler anoaHandler,
Class recordClass) {
return new AvroReader.SpecificReader<>(recordClass).decoder(anoaHandler);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Avro SpecificRecord class object
* @param Jackson JsonParser type
* @param Avro SpecificRecord record type
* @param Metadata type
* @return A function which reads an Avro record from a JsonParser, in its 'natural' encoding,
* with strictest possible type checking.
*/
static public
Function, Anoa> jacksonStrict(
AnoaHandler anoaHandler,
Class recordClass) {
return new AvroReader.SpecificReader<>(recordClass).decoderStrict(anoaHandler);
}
static protected class BinaryDecoderWrapper {
protected BinaryDecoder decoder = null;
protected BinaryDecoder getDecoder(byte[] bytes) {
decoder = DecoderFactory.get().binaryDecoder(bytes, decoder);
return decoder;
}
}
}