Search in sources :

Example 1 with AvroAdapter

use of com.linkedin.data.avro.AvroAdapter in project rest.li by linkedin.

the class AvroUtil method jsonFromGenericRecord.

public static String jsonFromGenericRecord(GenericRecord record) throws IOException {
    GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    AvroAdapter avroAdapter = AvroAdapterFinder.getAvroAdapter();
    Encoder jsonEncoder = avroAdapter.createJsonEncoder(record.getSchema(), outputStream);
    writer.setSchema(record.getSchema());
    writer.write(record, jsonEncoder);
    jsonEncoder.flush();
    return outputStream.toString();
}
Also used : AvroAdapter(com.linkedin.data.avro.AvroAdapter) Encoder(org.apache.avro.io.Encoder) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 2 with AvroAdapter

use of com.linkedin.data.avro.AvroAdapter in project rest.li by linkedin.

the class AvroUtil method bytesFromGenericRecord.

public static byte[] bytesFromGenericRecord(GenericRecord record) throws IOException {
    GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    AvroAdapter avroAdapter = AvroAdapterFinder.getAvroAdapter();
    Encoder binaryEncoder = avroAdapter.createBinaryEncoder(outputStream);
    writer.setSchema(record.getSchema());
    writer.write(record, binaryEncoder);
    binaryEncoder.flush();
    return outputStream.toByteArray();
}
Also used : AvroAdapter(com.linkedin.data.avro.AvroAdapter) Encoder(org.apache.avro.io.Encoder) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 3 with AvroAdapter

use of com.linkedin.data.avro.AvroAdapter in project rest.li by linkedin.

the class AvroUtil method genericRecordFromJson.

public static GenericRecord genericRecordFromJson(String json, Schema schema) throws IOException {
    GenericDatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>();
    AvroAdapter avroAdapter = AvroAdapterFinder.getAvroAdapter();
    Decoder jsonDecoder = avroAdapter.createJsonDecoder(schema, json);
    reader.setSchema(schema);
    GenericRecord record = reader.read(null, jsonDecoder);
    return record;
}
Also used : AvroAdapter(com.linkedin.data.avro.AvroAdapter) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) GenericRecord(org.apache.avro.generic.GenericRecord) Decoder(org.apache.avro.io.Decoder)

Example 4 with AvroAdapter

use of com.linkedin.data.avro.AvroAdapter in project rest.li by linkedin.

the class AvroUtil method genericRecordFromBytes.

public static GenericRecord genericRecordFromBytes(byte[] bytes, Schema schema) throws IOException {
    GenericDatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>();
    AvroAdapter avroAdapter = AvroAdapterFinder.getAvroAdapter();
    Decoder binaryDecoder = avroAdapter.createBinaryDecoder(bytes);
    reader.setSchema(schema);
    GenericRecord record = reader.read(null, binaryDecoder);
    return record;
}
Also used : AvroAdapter(com.linkedin.data.avro.AvroAdapter) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) GenericRecord(org.apache.avro.generic.GenericRecord) Decoder(org.apache.avro.io.Decoder)

Aggregations

AvroAdapter (com.linkedin.data.avro.AvroAdapter)4 GenericRecord (org.apache.avro.generic.GenericRecord)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)2 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)2 Decoder (org.apache.avro.io.Decoder)2 Encoder (org.apache.avro.io.Encoder)2