Search in sources :

Example 1 with Decoder

use of org.apache.avro.io.Decoder 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 Decoder

use of org.apache.avro.io.Decoder 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 3 with Decoder

use of org.apache.avro.io.Decoder in project databus by linkedin.

the class AvroConverter method convert.

public List<GenericRecord> convert(InputStream in) throws IOException {
    Decoder inputDecoder = (_inputFormat == AvroFormat.BINARY) ? DecoderFactory.defaultFactory().createBinaryDecoder(in, null) : (AvroFormat.JSON == _inputFormat) ? new JsonDecoder(_inputSchema, in) : null;
    ArrayList<GenericRecord> result = new ArrayList<GenericRecord>();
    GenericDatumReader<GenericRecord> genericReader = _inputSchema != _outputSchema ? new GenericDatumReader<GenericRecord>(_inputSchema, _outputSchema) : new GenericDatumReader<GenericRecord>(_inputSchema);
    switch(_inputFormat) {
        case BINARY:
        case JSON:
            {
                GenericRecord r = genericReader.read(null, inputDecoder);
                result.add(r);
                break;
            }
        case JSON_LINES:
            {
                InputStreamReader inReader = new InputStreamReader(in);
                try {
                    BufferedReader lineIn = new BufferedReader(inReader);
                    try {
                        String line;
                        while (null != (line = lineIn.readLine())) {
                            inputDecoder = new JsonDecoder(_inputSchema, line);
                            GenericRecord r = genericReader.read(null, inputDecoder);
                            result.add(r);
                            break;
                        }
                    } finally {
                        lineIn.close();
                    }
                } finally {
                    inReader.close();
                }
            }
        default:
            {
                throw new RuntimeException("Unimplemented input format: " + _inputFormat);
            }
    }
    return result;
}
Also used : JsonDecoder(org.apache.avro.io.JsonDecoder) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) JsonDecoder(org.apache.avro.io.JsonDecoder) Decoder(org.apache.avro.io.Decoder) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 4 with Decoder

use of org.apache.avro.io.Decoder in project gora by apache.

the class AccumuloStore method fromBytes.

public Object fromBytes(Schema schema, byte[] data) throws IOException {
    Schema fromSchema = null;
    if (schema.getType() == Type.UNION) {
        try {
            Decoder decoder = DecoderFactory.get().binaryDecoder(data, null);
            int unionIndex = decoder.readIndex();
            List<Schema> possibleTypes = schema.getTypes();
            fromSchema = possibleTypes.get(unionIndex);
            Schema effectiveSchema = possibleTypes.get(unionIndex);
            if (effectiveSchema.getType() == Type.NULL) {
                decoder.readNull();
                return null;
            } else {
                data = decoder.readBytes(null).array();
            }
        } catch (IOException e) {
            LOG.error(e.getMessage());
            throw new GoraException("Error decoding union type: ", e);
        }
    } else {
        fromSchema = schema;
    }
    return fromBytes(encoder, fromSchema, data);
}
Also used : GoraException(org.apache.gora.util.GoraException) Schema(org.apache.avro.Schema) IOException(java.io.IOException) Decoder(org.apache.avro.io.Decoder) BinaryDecoder(org.apache.avro.io.BinaryDecoder)

Example 5 with Decoder

use of org.apache.avro.io.Decoder in project voldemort by voldemort.

the class AvroVersionedGenericSerializer method toObject.

public Object toObject(byte[] bytes) {
    Integer version = Integer.valueOf(bytes[0]);
    if (version > newestVersion)
        throw new SerializationException("Client needs to rebootstrap! \n Writer's schema version greater than Reader");
    Schema typeDefWriter = Schema.parse(typeDefVersions.get(version));
    Decoder decoder = DecoderFactory.defaultFactory().createBinaryDecoder(bytes, 1, bytes.length - 1, null);
    GenericDatumReader<Object> reader = null;
    try {
        reader = new GenericDatumReader<Object>(typeDefWriter, typeDef);
        // writer's schema
        reader.setSchema(typeDefWriter);
        // Reader's schema
        reader.setExpected(typeDef);
        return reader.read(null, decoder);
    } catch (IOException e) {
        throw new SerializationException(e);
    }
}
Also used : SerializationException(voldemort.serialization.SerializationException) Schema(org.apache.avro.Schema) IOException(java.io.IOException) Decoder(org.apache.avro.io.Decoder)

Aggregations

Decoder (org.apache.avro.io.Decoder)16 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)8 GenericRecord (org.apache.avro.generic.GenericRecord)8 IOException (java.io.IOException)6 SerializationException (voldemort.serialization.SerializationException)4 Schema (org.apache.avro.Schema)3 ChannelBufferInputStream (org.jboss.netty.buffer.ChannelBufferInputStream)3 BadRequestException (co.cask.cdap.common.BadRequestException)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2 TopicId (co.cask.cdap.proto.id.TopicId)2 AvroAdapter (com.linkedin.data.avro.AvroAdapter)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 RawMessage (co.cask.cdap.messaging.data.RawMessage)1 InputSupplier (com.google.common.io.InputSupplier)1 BufferedReader (java.io.BufferedReader)1 EOFException (java.io.EOFException)1 FileInputStream (java.io.FileInputStream)1