All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adgear.anoa.write.AvroEncoders Maven / Gradle / Ivy

Go to download

Core classes for Anoa library, which aims to be a safe, convenient and fast record de/serialization wrapper for the Avro, Thrift and Jackson libraries, using the functional idioms of Java 8. The anoa-core module tries to keep upstream dependencies to a minimum.

There is a newer version: 3.1.2
Show newest version
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.ByteArrayOutputStream;
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.
 */
public class AvroEncoders {

  protected AvroEncoders() {
  }

  /**
   * @param schema Avro schema of records to serialize
   */
  static public /*@NonNull*/ Function binary(
      /*@NonNull*/ 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  /*@NonNull*/ Function, Anoa> binary(
      /*@NonNull*/ AnoaHandler anoaHandler,
      /*@NonNull*/ Schema schema) {
    return binary(anoaHandler, new GenericDatumWriter<>(schema));
  }

  /**
   * @param recordClass Class object of Avro records to be serialized
   * @param  Avro record type
   */
  static public  /*@NonNull*/ Function binary(
      /*@NonNull*/ 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  /*@NonNull*/ Function, Anoa> binary(
      /*@NonNull*/ AnoaHandler anoaHandler,
      /*@NonNull*/ Class recordClass) {
    return binary(anoaHandler, new SpecificDatumWriter<>(recordClass));
  }

  static  /*@NonNull*/ Function binary(
      /*@NonNull*/ 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  /*@NonNull*/ Function, Anoa> binary(
      /*@NonNull*/ AnoaHandler anoaHandler,
      /*@NonNull*/ 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 /*@NonNull*/ Function json(
      /*@NonNull*/ 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  /*@NonNull*/ Function, Anoa> json(
      /*@NonNull*/ AnoaHandler anoaHandler,
      /*@NonNull*/ 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  /*@NonNull*/ Function json(
      /*@NonNull*/ 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  /*@NonNull*/ Function, Anoa> json(
      /*@NonNull*/ AnoaHandler anoaHandler,
      /*@NonNull*/ Class recordClass) {
    final SpecificDatumWriter writer = new SpecificDatumWriter<>(recordClass);
    return json(anoaHandler, writer, writer.getSpecificData().getSchema(recordClass));
  }

  static  /*@NonNull*/ Function json(
      /*@NonNull*/ DatumWriter writer,
      /*@NonNull*/ 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  /*@NonNull*/ Function, Anoa> json(
      /*@NonNull*/ AnoaHandler anoaHandler,
      /*@NonNull*/ DatumWriter writer,
      /*@NonNull*/ 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.
   */
  static public  /*@NonNull*/ Function jackson(
      /*@NonNull*/ Class recordClass,
      /*@NonNull*/ Supplier supplier) {
    return jackson(new AvroWriter<>(recordClass), 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.
   */
  static public 
  /*@NonNull*/ Function, Anoa> jackson(
      /*@NonNull*/ AnoaHandler anoaHandler,
      /*@NonNull*/ Class recordClass,
      /*@NonNull*/ Supplier supplier) {
    return jackson(anoaHandler, new AvroWriter<>(recordClass), 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.
   */
  static public  /*@NonNull*/ Function jackson(
      /*@NonNull*/ Schema schema,
      /*@NonNull*/ Supplier supplier) {
    return jackson(new AvroWriter<>(schema), 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 
  /*@NonNull*/ Function, Anoa> jackson(
      /*@NonNull*/ AnoaHandler anoaHandler,
      /*@NonNull*/ Schema schema,
      /*@NonNull*/ Supplier supplier) {
    return jackson(anoaHandler, new AvroWriter<>(schema), supplier);
  }

  static  /*@NonNull*/ Function jackson(
      /*@NonNull*/ AvroWriter writer,
      /*@NonNull*/ Supplier supplier) {
    return (R record) -> {
      G jg = supplier.get();
      writer.write(record, jg);
      return jg;
    };
  }

  static 
  /*@NonNull*/ Function, Anoa> jackson(
      /*@NonNull*/ AnoaHandler anoaHandler,
      /*@NonNull*/ AvroWriter writer,
      /*@NonNull*/ Supplier supplier) {
    return anoaHandler.functionChecked((R record) -> {
      G jg = supplier.get();
      writer.writeChecked(record, jg);
      return jg;
    });
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy