Question

I have a POJO which in need to send to another end-poind (some server). I have decided it do it using avro.

So far I have created my Avro schema and generated my DataFileWriter:

GenericRecord user1 = new GenericData.Record(schema);
user1.put("name", "Jenny");
user1.put("favorite_color", "green");


GenericRecord user2 = new GenericData.Record(schema);
user2.put("name", "Kevin");
user2.put("favorite_color", "red");

DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(schema);
DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<GenericRecord>(datumWriter);
dataFileWriter.create(schema, schemaFile);
dataFileWriter.append(user1);
dataFileWriter.append(user2);

This is what I have so far and I'm missing the following:

I would like to compress the data before sending it, I rather doing it using snappy, how should I combine it with my code?

UPDATE: Added these lines:

// use snappy compression
CodecFactory codecFactory = CodecFactory.snappyCodec();
dataFileWriter.setCodec(codecFactory);

But I'm not sure yet if it does the job.

Was it helpful?

Solution

Using this:

    public byte[] serialize(T objectToSerialize) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
DatumWriter<T> reflectDatumWriter = new ReflectDatumWriter<T>(avroSchema);
DataFileWriter<T> fileWriter = null;
try {
    fileWriter = new DataFileWriter<T>(reflectDatumWriter);
    fileWriter.setCodec(CodecFactory.snappyCodec());
    fileWriter.create(avroSchema, out);
    fileWriter.append(objectToSerialize);
    fileWriter.close();
} catch (Exception e) {
    LOG.error(e);
    return null;
}
return out.toByteArray();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top