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;
}
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;
}
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;
}
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);
}
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);
}
}
Aggregations