use of org.apache.avro.specific.SpecificRecord in project spf4j by zolyfarkas.
the class SpecificRecordAppender method append.
@Override
public void append(final SpecificRecord object, final Appendable appendTo) throws IOException {
StringBuilder sb = TMP.get();
sb.setLength(0);
try (AppendableOutputStream bos = new AppendableOutputStream(sb, Charsets.UTF_8)) {
final Schema schema = object.getSchema();
SpecificDatumWriter<SpecificRecord> writer = new SpecificDatumWriter<>(schema);
JsonEncoder jsonEncoder = EF.jsonEncoder(schema, bos);
writer.write(object, jsonEncoder);
jsonEncoder.flush();
} catch (IOException | RuntimeException ex) {
writeSerializationError(object, sb, ex);
}
appendTo.append(sb);
}
use of org.apache.avro.specific.SpecificRecord in project registry by hortonworks.
the class ConfluentAvroSerDesHandler method handlePayloadSerialization.
@Override
public void handlePayloadSerialization(OutputStream outputStream, Object input) {
try {
Schema schema = AvroUtils.computeSchema(input);
if (input instanceof byte[]) {
outputStream.write((byte[]) input);
} else {
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(outputStream, null);
DatumWriter<Object> writer;
if (input instanceof SpecificRecord) {
writer = new SpecificDatumWriter<>(schema);
} else {
writer = new GenericDatumWriter<>(schema);
}
writer.write(input, encoder);
encoder.flush();
}
} catch (IOException e) {
throw new AvroRetryableException("Error serializing Avro message", e);
} catch (RuntimeException e) {
// ClassCastException, etc
throw new AvroException("Error serializing Avro message", e);
}
}
use of org.apache.avro.specific.SpecificRecord in project samza by apache.
the class TestAzureBlobAvroWriter method encodeRecord.
private byte[] encodeRecord(IndexedRecord record) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Schema schema = record.getSchema();
EncoderFactory encoderfactory = new EncoderFactory();
BinaryEncoder encoder = encoderfactory.binaryEncoder(out, null);
DatumWriter<IndexedRecord> writer;
if (record instanceof SpecificRecord) {
writer = new SpecificDatumWriter<>(schema);
} else {
writer = new GenericDatumWriter<>(schema);
}
writer.write(record, encoder);
// encoder may buffer
encoder.flush();
return out.toByteArray();
}
use of org.apache.avro.specific.SpecificRecord in project databus by linkedin.
the class DbusEventAvroDecoder method getTypedValue.
@Override
public <T extends SpecificRecord> T getTypedValue(DbusEvent e, T reuse, Class<T> targetClass) {
if (null == reuse) {
try {
reuse = targetClass.newInstance();
} catch (InstantiationException e1) {
LOG.error("getTypedValue class instantiation error (" + e1.getMessage() + ") for event " + e, e1);
return null;
} catch (IllegalAccessException e1) {
LOG.error("getTypedValue access error (" + e1.getMessage() + ") for event " + e, e1);
return null;
}
}
byte[] md5 = new byte[16];
e.schemaId(md5);
SchemaId schemaId = new SchemaId(md5);
VersionedSchema writerSchema = _schemaSet.getById(schemaId);
if (null == writerSchema) {
LOG.error("Unable to find schema for id " + schemaId + "; event = " + e);
throw new DatabusRuntimeException("No schema available to decode event " + e);
}
ByteBuffer valueBuffer = e.value();
byte[] valueBytes = new byte[valueBuffer.remaining()];
valueBuffer.get(valueBytes);
try {
// JsonDecoder jsonDec = new JsonDecoder(sourceSchema.getSchema(),new ByteArrayInputStream(valueBytes));
binDecoder.set(DecoderFactory.defaultFactory().createBinaryDecoder(valueBytes, binDecoder.get()));
SpecificDatumReader<SpecificRecord> reader = new SpecificDatumReader<SpecificRecord>(writerSchema.getSchema(), reuse.getSchema());
return targetClass.cast(reader.read(reuse, binDecoder.get()));
} catch (IOException e1) {
LOG.error("getTypedValue IO error (" + e1.getMessage() + ") for event " + e, e1);
}
return reuse;
}
use of org.apache.avro.specific.SpecificRecord in project flink by apache.
the class AvroRowDeSerializationSchemaTest method testSpecificDeserializeFromClassSeveralTimes.
@Test
public void testSpecificDeserializeFromClassSeveralTimes() throws IOException {
final Tuple3<Class<? extends SpecificRecord>, SpecificRecord, Row> testData = AvroTestUtils.getSpecificTestData();
final AvroRowSerializationSchema serializationSchema = new AvroRowSerializationSchema(testData.f0);
final AvroRowDeserializationSchema deserializationSchema = new AvroRowDeserializationSchema(testData.f0);
final byte[] bytes = serializationSchema.serialize(testData.f2);
deserializationSchema.deserialize(bytes);
deserializationSchema.deserialize(bytes);
final Row actual = deserializationSchema.deserialize(bytes);
assertEquals(testData.f2, actual);
}
Aggregations