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

com.adgear.anoa.write.AvroConsumers 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.fasterxml.jackson.core.JsonGenerator;

import org.apache.avro.Schema;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.specific.SpecificDatumWriter;
import org.apache.avro.specific.SpecificRecord;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;

/**
 * Utility class for generating {@code WriteConsumer} instances to write Avro records.
 */
public class AvroConsumers {

  protected AvroConsumers() {
  }

  /**
   * Write as compressed Avro batch file, readable with {@link org.apache.avro.file.DataFileStream}
   *
   * @param schema Avro record schema to accept
   * @param file file to write into
   */
  static public /*@NonNull*/ WriteConsumer batch(
      /*@NonNull*/ Schema schema,
      /*@NonNull*/ File file) {
    try {
      return new AvroBatchWriteConsumer<>(
          new DataFileWriter(new GenericDatumWriter<>(schema))
              .create(schema, file));
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  /**
   * Write as compressed Avro batch file, readable with {@link org.apache.avro.file.DataFileStream}
   *
   * @param recordClass Avro SpecificRecord class to accept
   * @param file file to write into
   * @param  Avro record type
   */
  static public  /*@NonNull*/ WriteConsumer batch(
      /*@NonNull*/ Class recordClass,
      /*@NonNull*/ File file) {
    Schema schema = SpecificData.get().getSchema(recordClass);
    if (schema == null) {
      throw new IllegalArgumentException("No schema found for class " + recordClass);
    }
    try {
      return new AvroBatchWriteConsumer<>(
          new DataFileWriter(new SpecificDatumWriter<>(schema)).create(schema, file));
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  /**
   * Write as compressed Avro batch file, readable with {@link org.apache.avro.file.DataFileStream}
   *
   * @param schema Avro record schema to accept
   * @param outputStream stream to write into
   */
  static public /*@NonNull*/ WriteConsumer batch(
      /*@NonNull*/ Schema schema,
      /*@NonNull*/ OutputStream outputStream) {
    try {
      return new AvroBatchWriteConsumer<>(
          new DataFileWriter(new GenericDatumWriter<>(schema))
              .create(schema, outputStream));
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  /**
   * Write as compressed Avro batch file, readable with {@link org.apache.avro.file.DataFileStream}
   *
   * @param recordClass Avro SpecificRecord class to accept
   * @param outputStream stream to write into
   * @param  Avro record type
   */
  static public  /*@NonNull*/ WriteConsumer batch(
      /*@NonNull*/ Class recordClass,
      /*@NonNull*/ OutputStream outputStream) {
    Schema schema = SpecificData.get().getSchema(recordClass);
    if (schema == null) {
      throw new IllegalArgumentException("No schema found for class " + recordClass);
    }
    try {
      return new AvroBatchWriteConsumer<>(
          new DataFileWriter(new SpecificDatumWriter<>(schema)).create(schema, outputStream));
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  /**
   * Write as Avro binary encoding
   *
   * @param schema Avro schema to accept
   * @param outputStream stream to write into
   */
  static public /*@NonNull*/ WriteConsumer binary(
      /*@NonNull*/ Schema schema,
      /*@NonNull*/ OutputStream outputStream) {
    return new AvroWriteConsumer<>(
        new GenericDatumWriter<>(schema),
        EncoderFactory.get().binaryEncoder(new BufferedOutputStream(outputStream), null));
  }

  /**
   * Write as Avro binary encoding
   *
   * @param recordClass Avro SpecificRecord class to accept
   * @param outputStream stream to write into
   * @param  Avro record type
   */
  static public  /*@NonNull*/ WriteConsumer binary(
      /*@NonNull*/ Class recordClass,
      /*@NonNull*/ OutputStream outputStream) {
    Schema schema = SpecificData.get().getSchema(recordClass);
    if (schema == null) {
      throw new IllegalArgumentException("No schema found for class " + recordClass);
    }
    return new AvroWriteConsumer<>(
        new SpecificDatumWriter<>(schema),
        EncoderFactory.get().binaryEncoder(new BufferedOutputStream(outputStream), null));
  }

  /**
   * Write as Avro JSON encoding
   *
   * @param schema Avro schema to accept
   * @param outputStream stream to write into
   */
  static public /*@NonNull*/ WriteConsumer json(
      /*@NonNull*/ Schema schema,
      /*@NonNull*/ OutputStream outputStream) {
    try {
      return new AvroWriteConsumer<>(
          new GenericDatumWriter<>(schema),
          EncoderFactory.get().jsonEncoder(schema, new BufferedOutputStream(outputStream)));
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  /**
   * Write as Avro JSON encoding
   *
   * @param recordClass Avro SpecificRecord class to accept
   * @param outputStream stream to write into
   * @param  Avro record type
   */
  static public  /*@NonNull*/ WriteConsumer json(
      /*@NonNull*/ Class recordClass,
      /*@NonNull*/ OutputStream outputStream) {
    Schema schema = SpecificData.get().getSchema(recordClass);
    if (schema == null) {
      throw new IllegalArgumentException("No schema found for class " + recordClass);
    }
    try {
      return new AvroWriteConsumer<>(
          new SpecificDatumWriter<>(schema),
          EncoderFactory.get().jsonEncoder(schema, new BufferedOutputStream(outputStream)));
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  /**
   * Write as 'natural' JSON serialization using provided generator
   *
   * @param schema Avro schema to accept
   * @param jacksonGenerator JsonGenerator instance to write into
   */
  static public /*@NonNull*/ WriteConsumer jackson(
      /*@NonNull*/ Schema schema,
      /*@NonNull*/ JsonGenerator jacksonGenerator) {
    return new JacksonWriteConsumer<>(jacksonGenerator, new AvroWriter(schema));
  }

  /**
   * Write as 'natural' JSON serialization using provided generator
   *
   * @param recordClass Avro SpecificRecord class to accept
   * @param jacksonGenerator JsonGenerator instance to write into
   * @param  Avro record type
   */
  static public 
  /*@NonNull*/ WriteConsumer jackson(
      /*@NonNull*/ Class recordClass,
      /*@NonNull*/ JsonGenerator jacksonGenerator) {
    return new JacksonWriteConsumer<>(jacksonGenerator, new AvroWriter<>(recordClass));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy