Search in sources :

Example 11 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project crunch by cloudera.

the class AvroTypeSortTest method writeAvroFile.

private void writeAvroFile(List<Person> people, File avroFile) throws IOException {
    FileOutputStream outputStream = new FileOutputStream(avroFile);
    SpecificDatumWriter<Person> writer = new SpecificDatumWriter<Person>(Person.class);
    DataFileWriter<Person> dataFileWriter = new DataFileWriter<Person>(writer);
    dataFileWriter.create(Person.SCHEMA$, outputStream);
    for (Person person : people) {
        dataFileWriter.append(person);
    }
    dataFileWriter.close();
    outputStream.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) DataFileWriter(org.apache.avro.file.DataFileWriter) Person(org.apache.crunch.test.Person) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 12 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project camel by apache.

the class AvroDataFormat method marshal.

public void marshal(Exchange exchange, Object graph, OutputStream outputStream) throws Exception {
    // the schema should be from the graph class name
    Schema useSchema = actualSchema != null ? actualSchema : loadSchema(graph.getClass().getName());
    DatumWriter<Object> datum = new SpecificDatumWriter<Object>(useSchema);
    Encoder encoder = EncoderFactory.get().binaryEncoder(outputStream, null);
    datum.write(graph, encoder);
    encoder.flush();
}
Also used : Encoder(org.apache.avro.io.Encoder) Schema(org.apache.avro.Schema) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 13 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project eiger by wlloyd.

the class SerDeUtils method serialize.

/**
     * Serializes a single object.
     * @param o Object to serialize
     */
public static <T extends SpecificRecord> ByteBuffer serialize(T o) throws IOException {
    OutputBuffer buff = new OutputBuffer();
    BinaryEncoder enc = new BinaryEncoder(buff);
    SpecificDatumWriter<T> writer = new SpecificDatumWriter<T>(o.getSchema());
    writer.write(o, enc);
    enc.flush();
    return ByteBuffer.wrap(buff.asByteArray());
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) OutputBuffer(org.apache.cassandra.io.util.OutputBuffer) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 14 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project gora by apache.

the class SolrStore method getDatumWriter.

@SuppressWarnings("rawtypes")
private SpecificDatumWriter getDatumWriter(Schema fieldSchema) {
    SpecificDatumWriter writer = writerMap.get(fieldSchema);
    if (writer == null) {
        // ignore dirty bits
        writer = new SpecificDatumWriter(fieldSchema);
        writerMap.put(fieldSchema, writer);
    }
    return writer;
}
Also used : SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 15 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project gora by apache.

the class AccumuloStore method put.

@Override
public void put(K key, T val) {
    try {
        Mutation m = new Mutation(new Text(toBytes(key)));
        Schema schema = val.getSchema();
        List<Field> fields = schema.getFields();
        int count = 0;
        for (int i = 0; i < fields.size(); i++) {
            if (!val.isDirty(i)) {
                continue;
            }
            Field field = fields.get(i);
            Object o = val.get(field.pos());
            Pair<Text, Text> col = mapping.fieldMap.get(field.name());
            if (col == null) {
                throw new GoraException("Please define the gora to accumulo mapping for field " + field.name());
            }
            switch(field.schema().getType()) {
                case MAP:
                    count = putMap(m, count, field.schema().getValueType(), o, col, field.name());
                    break;
                case ARRAY:
                    count = putArray(m, count, o, col, field.name());
                    break;
                case // default value of null acts like union with null
                UNION:
                    Schema effectiveSchema = field.schema().getTypes().get(firstNotNullSchemaTypeIndex(field.schema()));
                    // map and array need to compute qualifier
                    if (effectiveSchema.getType() == Type.ARRAY) {
                        count = putArray(m, count, o, col, field.name());
                        break;
                    } else if (effectiveSchema.getType() == Type.MAP) {
                        count = putMap(m, count, effectiveSchema.getValueType(), o, col, field.name());
                        break;
                    }
                // continue like a regular top-level union
                case RECORD:
                    final SpecificDatumWriter<Object> writer = new SpecificDatumWriter<>(field.schema());
                    final byte[] byteData = IOUtils.serialize(writer, o);
                    m.put(col.getFirst(), col.getSecond(), new Value(byteData));
                    count++;
                    break;
                default:
                    m.put(col.getFirst(), col.getSecond(), new Value(toBytes(o)));
                    count++;
            }
        }
        if (count > 0)
            try {
                getBatchWriter().addMutation(m);
            } catch (MutationsRejectedException e) {
                LOG.error(e.getMessage(), e);
            }
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : Schema(org.apache.avro.Schema) Text(org.apache.hadoop.io.Text) IOException(java.io.IOException) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) Field(org.apache.avro.Schema.Field) GoraException(org.apache.gora.util.GoraException) Value(org.apache.accumulo.core.data.Value) Mutation(org.apache.accumulo.core.data.Mutation) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Aggregations

SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)17 DataFileWriter (org.apache.avro.file.DataFileWriter)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Schema (org.apache.avro.Schema)4 BinaryEncoder (org.apache.avro.io.BinaryEncoder)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Encoder (org.apache.avro.io.Encoder)3 Person (org.apache.crunch.test.Person)3 FileOutputStream (java.io.FileOutputStream)2 OutputBuffer (org.apache.cassandra.io.util.OutputBuffer)2 Address (org.apache.flink.api.io.avro.generated.Address)2 User (org.apache.flink.api.io.avro.generated.User)2 Address (org.apache.hadoop.hive.hbase.avro.Address)2 ContactInfo (org.apache.hadoop.hive.hbase.avro.ContactInfo)2 Employee (org.apache.hadoop.hive.hbase.avro.Employee)2 HomePhone (org.apache.hadoop.hive.hbase.avro.HomePhone)2 OfficePhone (org.apache.hadoop.hive.hbase.avro.OfficePhone)2 Before (org.junit.Before)2