Search in sources :

Example 1 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader in project storm by apache.

the class AbstractAvroSerializer method read.

@Override
public GenericContainer read(Kryo kryo, Input input, Class<GenericContainer> aClass) {
    Schema theSchema = this.getSchema(input.readString());
    GenericDatumReader<GenericContainer> reader = new GenericDatumReader<>(theSchema);
    Decoder decoder = DecoderFactory.get().directBinaryDecoder(input, null);
    GenericContainer foo;
    try {
        foo = reader.read(null, decoder);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return foo;
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) IOException(java.io.IOException) GenericContainer(org.apache.avro.generic.GenericContainer) Decoder(org.apache.avro.io.Decoder)

Example 2 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader in project hive by apache.

the class AvroGenericRecordWritable method readFields.

public void readFields(byte[] bytes, Schema writerSchema, Schema readerSchema) throws IOException {
    fileSchema = writerSchema;
    record = new GenericData.Record(writerSchema);
    GenericDatumReader<GenericRecord> gdr = new GenericDatumReader<GenericRecord>();
    gdr.setExpected(readerSchema);
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    DataFileStream<GenericRecord> dfr = new DataFileStream<GenericRecord>(is, gdr);
    record = dfr.next(record);
    dfr.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) GenericRecord(org.apache.avro.generic.GenericRecord) DataFileStream(org.apache.avro.file.DataFileStream) GenericData(org.apache.avro.generic.GenericData)

Example 3 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader in project trevni by cutting.

the class TestCases method fromJson.

private List<Object> fromJson(Schema schema, File file) throws Exception {
    InputStream in = new FileInputStream(file);
    List<Object> data = new ArrayList<Object>();
    try {
        DatumReader reader = new GenericDatumReader(schema);
        Decoder decoder = DecoderFactory.get().jsonDecoder(schema, in);
        while (true) data.add(reader.read(null, decoder));
    } catch (EOFException e) {
    } finally {
        in.close();
    }
    return data;
}
Also used : DatumReader(org.apache.avro.io.DatumReader) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) ArrayList(java.util.ArrayList) EOFException(java.io.EOFException) Decoder(org.apache.avro.io.Decoder) FileInputStream(java.io.FileInputStream)

Example 4 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader in project druid by druid-io.

the class InlineSchemaAvroBytesDecoder method parse.

@Override
public GenericRecord parse(ByteBuffer bytes) {
    DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(schemaObj);
    ByteBufferInputStream inputStream = new ByteBufferInputStream(Collections.singletonList(bytes));
    try {
        return reader.read(null, DecoderFactory.get().binaryDecoder(inputStream, null));
    } catch (Exception e) {
        throw new ParseException(e, "Fail to decode avro message!");
    }
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) ByteBufferInputStream(org.apache.avro.util.ByteBufferInputStream) ParseException(io.druid.java.util.common.parsers.ParseException) GenericRecord(org.apache.avro.generic.GenericRecord) ParseException(io.druid.java.util.common.parsers.ParseException)

Example 5 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader in project druid by druid-io.

the class InlineSchemasAvroBytesDecoder method parse.

// It is assumed that record has following format.
// byte 1 : version, static 0x1
// byte 2-5 : int schemaId
// remaining bytes would have avro data
@Override
public GenericRecord parse(ByteBuffer bytes) {
    if (bytes.remaining() < 5) {
        throw new ParseException("record must have at least 5 bytes carrying version and schemaId");
    }
    byte version = bytes.get();
    if (version != V1) {
        throw new ParseException("found record of arbitrary version [%s]", version);
    }
    int schemaId = bytes.getInt();
    Schema schemaObj = schemaObjs.get(schemaId);
    if (schemaObj == null) {
        throw new ParseException("Failed to find schema for id [%s]", schemaId);
    }
    try {
        DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(schemaObj);
        ByteBufferInputStream inputStream = new ByteBufferInputStream(Collections.singletonList(bytes));
        return reader.read(null, DecoderFactory.get().binaryDecoder(inputStream, null));
    } catch (Exception e) {
        throw new ParseException(e, "Fail to decode avro message with schemaId [%s].", schemaId);
    }
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) ByteBufferInputStream(org.apache.avro.util.ByteBufferInputStream) ParseException(io.druid.java.util.common.parsers.ParseException) GenericRecord(org.apache.avro.generic.GenericRecord) ParseException(io.druid.java.util.common.parsers.ParseException)

Aggregations

GenericDatumReader (org.apache.avro.generic.GenericDatumReader)46 GenericRecord (org.apache.avro.generic.GenericRecord)31 Schema (org.apache.avro.Schema)20 IOException (java.io.IOException)15 File (java.io.File)10 DataFileStream (org.apache.avro.file.DataFileStream)10 Decoder (org.apache.avro.io.Decoder)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 GenericData (org.apache.avro.generic.GenericData)7 DataFileReader (org.apache.avro.file.DataFileReader)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 JsonDecoder (org.apache.avro.io.JsonDecoder)5 ParseException (io.druid.java.util.common.parsers.ParseException)4 FileInputStream (java.io.FileInputStream)4 DataFileWriter (org.apache.avro.file.DataFileWriter)4 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Map (java.util.Map)3 ChannelBufferInputStream (org.jboss.netty.buffer.ChannelBufferInputStream)3