avro学习笔记——实现序列化和反序列化
1.使用java语言来实现avro序列化和反序列化
使用DatumWriter和DatumReader对avro进行序列化和反序列化
public static <T> byte[] binarySerializable(T t) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
BinaryEncoder binaryEncoder = EncoderFactory.get().binaryEncoder(out, null);
DatumWriter<T> writer = new SpecificDatumWriter<T>((Class<T>) t.getClass());
try {
writer.write(t, binaryEncoder);
binaryEncoder.flush();
out.flush();
} catch (IOException e) {
LOGGER.error("binary serializable error");
e.printStackTrace();
}
LOGGER.debug("ByteArrayOutputStream = {}", new String(out.toByteArray()));
return out.toByteArray();
}
public static <T> T binaryDeserialize(byte[] bytes, Schema schema) {
try {
BinaryDecoder binaryDecoder = DecoderFactory.get().binaryDecoder(bytes, null);
DatumReader<T> datumReader = new SpecificDatumReader<T>(schema);
T read = datumReader.read(null, binaryDecoder);
return read;
} catch (IOException e) {
LOGGER.error("binary deserialize error");
e.printStackTrace();
}
return null;
}
使用Twitter 的 Bijection 类库来实现avro对象的序列化和反序列化
参考:Kafka 中使用 Avro 序列化框架(二):使用 Twitter 的 Bijection 类库实现 avro 的序列化与反序列化
2.使用python语言来实现avro序列化和反序列化
3.使用go语言来实现avro序列化和反序列化
本文只发表于博客园和tonglin0325的博客,作者:tonglin0325,转载请注明原文链接:https://www.cnblogs.com/tonglin0325/p/4637600.html

浙公网安备 33010602011771号