use of org.apache.avro.io.BinaryDecoder in project eiger by wlloyd.
the class SerDeUtils method deserialize.
/**
* Deserializes a single object based on the given Schema.
* @param writer writer's schema
* @param bytes Array to deserialize from
* @param ob An empty object to deserialize into (must not be null).
* @throws IOException
*/
public static <T extends SpecificRecord> T deserialize(Schema writer, ByteBuffer bytes, T ob) throws IOException {
BinaryDecoder dec = DIRECT_DECODERS.createBinaryDecoder(ByteBufferUtil.getArray(bytes), null);
SpecificDatumReader<T> reader = new SpecificDatumReader<T>(writer);
reader.setExpected(ob.getSchema());
return reader.read(ob, dec);
}
use of org.apache.avro.io.BinaryDecoder in project storm by apache.
the class AvroScheme method deserialize.
@Override
public List<Object> deserialize(ByteBuffer ser) {
try {
Schema schema = schemas.getSchema(schemaString);
DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(Utils.toByteArray(ser), null);
GenericRecord record = reader.read(null, decoder);
ArrayList<Object> list = new ArrayList<>(fieldNames.size());
for (String field : fieldNames) {
Object value = record.get(field);
// Avro strings are stored using a special Avro Utf8 type instead of using Java primitives
list.add(SerdeUtils.convertAvroUtf8(value));
}
return list;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.apache.avro.io.BinaryDecoder in project beam by apache.
the class AvroCoder method decode.
@Override
public T decode(InputStream inStream) throws IOException {
// Get a BinaryDecoder instance from the ThreadLocal cache and attempt to reuse it.
BinaryDecoder decoderInstance = DECODER_FACTORY.directBinaryDecoder(inStream, decoder.get());
// Save the potentially-new instance for later.
decoder.set(decoderInstance);
return reader.get().read(null, decoderInstance);
}
use of org.apache.avro.io.BinaryDecoder in project gora by apache.
the class HBaseByteInterface method fromBytes.
/**
* Deserializes an array of bytes matching the given schema to the proper basic
* (enum, Utf8,...) or complex type (Persistent/Record).
*
* Does not handle <code>arrays/maps</code> if not inside a <code>record</code> type.
*
* @param schema Avro schema describing the expected data
* @param val array of bytes with the data serialized
* @return Enum|Utf8|ByteBuffer|Integer|Long|Float|Double|Boolean|Persistent|Null
* @throws IOException
*/
@SuppressWarnings({ "rawtypes" })
public static Object fromBytes(Schema schema, byte[] val) throws IOException {
Type type = schema.getType();
switch(type) {
case ENUM:
return AvroUtils.getEnumValue(schema, val[0]);
case STRING:
return new Utf8(Bytes.toString(val));
case BYTES:
return ByteBuffer.wrap(val);
case INT:
return Bytes.toInt(val);
case LONG:
return Bytes.toLong(val);
case FLOAT:
return Bytes.toFloat(val);
case DOUBLE:
return Bytes.toDouble(val);
case BOOLEAN:
return val[0] != 0;
case UNION:
// if 'val' is empty we ignore the special case (will match Null in "case RECORD")
if (schema.getTypes().size() == 2) {
// schema [type0, type1]
Type type0 = schema.getTypes().get(0).getType();
Type type1 = schema.getTypes().get(1).getType();
// Check if types are different and there's a "null", like ["null","type"] or ["type","null"]
if (!type0.equals(type1) && (type0.equals(Schema.Type.NULL) || type1.equals(Schema.Type.NULL))) {
if (type0.equals(Schema.Type.NULL))
schema = schema.getTypes().get(1);
else
schema = schema.getTypes().get(0);
// Deserialize as if schema was ["type"]
return fromBytes(schema, val);
}
}
case RECORD:
// For UNION schemas, must use a specific SpecificDatumReader
// from the readerMap since unions don't have own name
// (key name in map will be "UNION-type-type-...")
String schemaId = schema.getType().equals(Schema.Type.UNION) ? String.valueOf(schema.hashCode()) : schema.getFullName();
SpecificDatumReader<?> reader = readerMap.get(schemaId);
if (reader == null) {
// ignore dirty bits
reader = new SpecificDatumReader(schema);
SpecificDatumReader localReader = null;
if ((localReader = readerMap.putIfAbsent(schemaId, reader)) != null) {
reader = localReader;
}
}
// initialize a decoder, possibly reusing previous one
BinaryDecoder decoderFromCache = decoders.get();
BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(val, null);
// put in threadlocal cache if the initial get was empty
if (decoderFromCache == null) {
decoders.set(decoder);
}
return reader.read(null, decoder);
default:
throw new RuntimeException("Unknown type: " + type);
}
}
Aggregations