我很想了解在 Avro 中编码一种非常特定类型的数据的最佳实践:UUID。
我是这样做的:
{
"name": "user_id",
"type": "string",
"logicalType": "UUID"
}
在撰写本文时,UUID 的
logicalType
尚未记录,但仍然受支持,您可以在此处检查代码并自行验证:https://github.com/apache/avro/blob/branch-1.8 /lang/java/avro/src/main/java/org/apache/avro/LogicalTypes.java#L71
到目前为止,我发现的唯一方法是定义自定义 UUID:
{
"namespace" : "your.namespace",
"type" : "fixed",
"name" : "UUID",
"size" : 16
}
我使用的是Scala,所以我还定义了
java.util.UUID
和我的自定义UUID之间的隐式转换,所以使用它并没有那么麻烦。
我必须创建一个自定义转换器,因为我无法在 1.8 版本的项目中使用 UUID 逻辑类型。 只需2步——
1)创建了一个新类,如下 -
public class UUIDCustomConverter extends CustomEncoding<UUID> {
@Override
protected void write(Object datum, Encoder out) throws IOException {
out.writeString(((UUID)datum).toString());
}
@Override
protected UUID read(Object reuse, Decoder in) throws IOException {
return UUID.fromString(in.readString());
}
}
2)在UUID属性中添加以下注释
//annotation for handling custom converter
@AvroEncode(using=UUIDCustomConverter.class)
protected UUID id;
从 avro 1.12.0 开始,已添加此功能。
{
"namespace": "com.test.avro",
"type": "record",
"name": "TestRecord
"fields": [
{
"name": "test",
"type": {
"name": "name",
"type": "fixed",
"size": 16,
"logicalType": "uuid"
}
}
]
}
有点不幸的是,你必须在内部字段中添加名称,但它是固定类型所必需的。