com.adgear.anoa.write.AvroEncoders Maven / Gradle / Ivy
package com.adgear.anoa.write;
import com.adgear.anoa.Anoa;
import com.adgear.anoa.AnoaHandler;
import com.fasterxml.jackson.core.JsonGenerator;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.generic.IndexedRecord;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.Encoder;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.specific.SpecificDatumWriter;
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 serializing Avro records. Unless specified otherwise,
* the functions should not be deemed thread-safe.
*/
final public class AvroEncoders {
private AvroEncoders() {
}
/**
* @param schema Avro schema of records to serialize
*/
static public Function binary(
Schema schema) {
return binary(new GenericDatumWriter<>(schema));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param schema Avro schema of records to serialize
* @param Metadata type
*/
static public Function, Anoa> binary(
AnoaHandler anoaHandler,
Schema schema) {
return binary(anoaHandler, new GenericDatumWriter<>(schema));
}
/**
* @param recordClass Class object of Avro records to be serialized
* @param Avro record type
*/
static public Function binary(
Class recordClass) {
return binary(new SpecificDatumWriter<>(recordClass));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Class object of Avro records to be serialized
* @param Avro record type
* @param Metadata type
*/
static public Function, Anoa> binary(
AnoaHandler anoaHandler,
Class recordClass) {
return binary(anoaHandler, new SpecificDatumWriter<>(recordClass));
}
static Function binary(
DatumWriter writer) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Encoder encoder = EncoderFactory.get().directBinaryEncoder(baos, null);
return (R record) -> {
baos.reset();
try {
writer.write(record, encoder);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return baos.toByteArray();
};
}
static Function, Anoa> binary(
AnoaHandler anoaHandler,
DatumWriter writer) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Encoder encoder = EncoderFactory.get().directBinaryEncoder(baos, null);
return anoaHandler.functionChecked((R record) -> {
baos.reset();
writer.write(record, encoder);
return baos.toByteArray();
});
}
/**
* @param schema Avro schema of records to serialize
*/
static public Function json(
Schema schema) {
return json(new GenericDatumWriter(schema), schema);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param schema Avro schema of records to serialize
* @param Metadata type
*/
static public Function, Anoa> json(
AnoaHandler anoaHandler,
Schema schema) {
return json(anoaHandler, new GenericDatumWriter(schema), schema);
}
/**
* @param recordClass Class object of Avro records to be serialized
* @param Avro record type
*/
static public Function json(
Class recordClass) {
final SpecificDatumWriter writer = new SpecificDatumWriter<>(recordClass);
return json(writer, writer.getSpecificData().getSchema(recordClass));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Class object of Avro records to be serialized
* @param Avro record type
* @param Metadata type
*/
static public Function, Anoa> json(
AnoaHandler anoaHandler,
Class recordClass) {
final SpecificDatumWriter writer = new SpecificDatumWriter<>(recordClass);
return json(anoaHandler, writer, writer.getSpecificData().getSchema(recordClass));
}
static Function json(
DatumWriter writer,
Schema schema) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Encoder encoder;
try {
encoder = EncoderFactory.get().jsonEncoder(schema, baos);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return (R record) -> {
baos.reset();
try {
writer.write(record, encoder);
encoder.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return baos.toString();
};
}
static Function, Anoa> json(
AnoaHandler anoaHandler,
DatumWriter writer,
Schema schema) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Encoder encoder;
try {
encoder = EncoderFactory.get().jsonEncoder(schema, baos);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return anoaHandler.functionChecked((R record) -> {
baos.reset();
writer.write(record, encoder);
encoder.flush();
return baos.toString();
});
}
/**
* @param recordClass Class object of Avro records to be serialized
* @param supplier called for each new record serialization
* @param Avro record type
* @param JsonGenerator type
* @return A function which calls the supplier for a JsonGenerator object and writes the record
* into it, in compact form.
*/
static public Function jackson(
Class recordClass,
Supplier supplier) {
return new AvroWriter<>(recordClass).encoder(supplier);
}
/**
* @param recordClass Class object of Avro records to be serialized
* @param supplier called for each new record serialization
* @param Avro record type
* @param JsonGenerator type
* @return A function which calls the supplier for a JsonGenerator object and writes the record
* into it, in strict form.
*/
static public Function jacksonStrict(
Class recordClass,
Supplier supplier) {
return new AvroWriter<>(recordClass).encoderStrict(supplier);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Class object of Avro records to be serialized
* @param supplier called for each new record serialization
* @param Avro record Type
* @param JsonGenerator type
* @param Metadata type
* @return A function which calls the supplier for a JsonGenerator object and writes the record
* into it, in compact form.
*/
static public
Function, Anoa> jackson(
AnoaHandler anoaHandler,
Class recordClass,
Supplier supplier) {
return new AvroWriter<>(recordClass).encoder(anoaHandler, supplier);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Class object of Avro records to be serialized
* @param supplier called for each new record serialization
* @param Avro record Type
* @param JsonGenerator type
* @param Metadata type
* @return A function which calls the supplier for a JsonGenerator object and writes the record
* into it, in strict form.
*/
static public
Function, Anoa> jacksonStrict(
AnoaHandler anoaHandler,
Class recordClass,
Supplier supplier) {
return new AvroWriter<>(recordClass).encoderStrict(anoaHandler, supplier);
}
/**
* @param schema Avro record schema
* @param supplier called for each new record serialization
* @param JsonGenerator type
* @return A function which calls the supplier for a JsonGenerator object and writes the record
* into it, in compact form.
*/
static public Function jackson(
Schema schema,
Supplier supplier) {
return new AvroWriter(schema).encoder(supplier);
}
/**
* @param schema Avro record schema
* @param supplier called for each new record serialization
* @param JsonGenerator type
* @return A function which calls the supplier for a JsonGenerator object and writes the record
* into it, in strict form.
*/
static public Function jacksonStrict(
Schema schema,
Supplier supplier) {
return new AvroWriter(schema).encoderStrict(supplier);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param schema Avro record schema
* @param supplier called for each new record serialization
* @param JsonGenerator type
* @param Metadata type
* @return A function which calls the supplier for a JsonGenerator object and writes the record
* into it, in compact form.
*/
static public
Function, Anoa> jackson(
AnoaHandler anoaHandler,
Schema schema,
Supplier supplier) {
return new AvroWriter(schema).encoder(anoaHandler, supplier);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param schema Avro record schema
* @param supplier called for each new record serialization
* @param JsonGenerator type
* @param Metadata type
* @return A function which calls the supplier for a JsonGenerator object and writes the record
* into it.
*/
static public
Function, Anoa> jacksonStrict(
AnoaHandler anoaHandler,
Schema schema,
Supplier supplier) {
return new AvroWriter(schema).encoderStrict(anoaHandler, supplier);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy