【发布时间】:2021-07-30 13:49:28
【问题描述】:
我编写了一个类来自定义将 UUID 类型的对象编码为要在 kafka 和 avro 之间传输的字节。
为了使用这个类,我在目标对象的 uuid 变量上方放置了一个@AvroEncode(using=UUIDAsBytesEncoding.class)。 (这是由 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 length of data
out.writeLong(16);
// write the data
out.writeBytes(bytes);
} else {
// position of null in union
out.writeLong(0);
}
}
@Override
protected UUID read(Object reuse, Decoder in) throws IOException {
System.out.println("READING");
Long size = in.readLong();
Long leastSig = in.readLong();
Long mostSig = in.readLong();
return new UUID(mostSig, leastSig);
}
}
write 方法和编码运行良好,但 read 方法永远不会在反序列化时被调用。我将如何在消费者中实现这一点?
注册表上的架构如下所示:
{"type":"record","name":"Request","namespace":"xxxxxxx.xxx.xxx","fields":[{"name":"password","type": "string"},{"name":"email","type":"string"},{"name":"id","type":["null",{"type":"bytes", "CustomEncoding":"UUIDAsBytesEncoding"}],"default":null}]} `
如果消费者不能自动使用该信息来使用 UUIDAsBytesEncoding 读取方法,那么我将如何在我的消费者中找到带有该标签的数据?
我也在使用融合模式注册表。
任何帮助将不胜感激!
【问题讨论】:
-
可以stackoverflow.com/q/8298308/1305344在这里帮忙吗?
-
不是特别 - 查看我对架构的编辑
-
Kafka的版本是多少?
-
org.apache.kafka:kafka_2.10:0.8.2.0-cp
标签: java apache-kafka avro uuid