我已经编写了一个类来自定义将 UUID 类型的对象编码为要跨 kafka 和 avro 传输的字节。
为了使用这个类,我@AvroEncode(using=UUIDAsBytesEncoding.class)
在目标对象中的 uuid 变量上方放置了一个。(这是由 apache avro 反射库实现的)
我很难弄清楚如何让我的消费者自动使用自定义解码器。(或者我必须进去手动解码吗?)。
这是我的 UUIDAsBytesEncoder 扩展 CustomEncoding 类:
public class UUIDAsBytesEncoding extends CustomEncoding<UUID> {
public UUIDAsBytesEncoding() {
List<Schema> union = Arrays.asList(Schema.create(Schema.Type.NULL), Schema.create(Schema.Type.BYTES));
union.get(1).addProp("CustomEncoding", "UUIDAsBytesEncoding");
schema = Schema.createUnion(union);
}
@Override
protected void write(Object datum, Encoder out) throws IOException {
if(datum != null) {
// encode the position of the data in the union
out.writeLong(1);
// convert uuid to bytes
byte[] bytes = new byte[16];
Conversion.uuidToByteArray(((UUID) datum),bytes,0,16);
// encode …
Run Code Online (Sandbox Code Playgroud)