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

com.adgear.anoa.write.AvroWriter 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.generic.IndexedRecord;
import org.apache.avro.specific.SpecificData;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

class AvroWriter extends AbstractWriter {

  final private LinkedHashMap> fieldMap;

  AvroWriter(Class recordClass) {
    this(SpecificData.get().getSchema(recordClass));
  }

  @SuppressWarnings("unchecked")
  AvroWriter(Schema schema) {
    fieldMap = new LinkedHashMap<>();
    schema.getFields().stream()
        .forEach(f -> fieldMap.put(f, (AbstractWriter) createWriter(f.schema())));
  }

  @Override
  protected void writeChecked(R record, JsonGenerator jacksonGenerator) throws IOException {
    jacksonGenerator.writeStartObject();
    for (Map.Entry> entry : fieldMap.entrySet()) {
      Schema.Field field = entry.getKey();
      Object value = record.get(field.pos());
      if (!(value == null && (field.defaultValue() == null || field.defaultValue().isNull()))) {
        jacksonGenerator.writeFieldName(field.name());
        entry.getValue().writeChecked(value, jacksonGenerator);
      }
    }
    jacksonGenerator.writeEndObject();
  }

  static protected AbstractWriter createWriter(Schema schema) {
    switch (schema.getType()) {
      case ARRAY:
        return new CollectionWriter<>(createWriter(schema.getElementType()));
      case BOOLEAN:
        return new BooleanWriter();
      case BYTES:
        return new ByteBufferWriter();
      case DOUBLE:
        return new DoubleWriter();
      case ENUM:
        return new EnumWriter();
      case FIXED:
        return new AvroFixedWriter();
      case FLOAT:
        return new FloatWriter();
      case INT:
        return new IntegerWriter();
      case LONG:
        return new LongWriter();
      case MAP:
        return new MapWriter<>(createWriter(schema.getValueType()));
      case RECORD:
        return new AvroWriter<>(schema);
      case STRING:
        return new StringWriter();
      case UNION:
        if (schema.getTypes().size() == 2) {
          return createWriter(schema.getTypes().get(
              (schema.getTypes().get(0).getType() == Schema.Type.NULL) ? 1 : 0));
        }
    }
    throw new RuntimeException("Unsupported Avro schema: " + schema);
  }
}