是否可以使用非字符串作为Avro映射中的键?
我发现了这个问题 AVRO-680,它声称添加了非字符串键支持,但我找不到任何示例,也无法从补丁中找出新支持如何与架构配合使用。
我想做这样的事情:
"name": "aThing",
"type": "record",
"fields": [
{ "name": "aMapOfThings", "type": {
"type": "map", "keys": "MyKeyType", "values": "MyValueType"
}
}
]
或avdl
:
record aThing {
map<MyKeyType, MyValueType> aMapOfThings = [:];
}
就像 protobuf 的这个问题一样:Protobuf 对象作为地图中的键
查看 PR,似乎此增强功能实际上并不是为与SpecificRecord
一起使用而设计的。看起来代码更改和测试集中在GenericRecord
上,并通过从包含非字符串键映射的 Java 类的反射来派生模式信息。
下面是一个派生自 POJO 的架构示例,其 Map 包含非字符串键(来自 TestNonStringMapKeys.java(。请注意,它是一个记录数组,而不是map
类型:
{
"type" : "record",
"name" : "Company",
"namespace" : "org.apache.avro.reflect",
"fields" : [ {
"name" : "employees",
"type" : [ "null", {
"type" : "array",
"items" : {
"type" : "record",
"name" : "Pair487429dd20c65898",
"fields" : [ {
"name" : "key",
"type" : {
"type" : "record",
"name" : "EmployeeId",
"fields" : [ {
"name" : "id",
"type" : [ "null", "int" ],
"default" : null
} ]
}
}, {
"name" : "value",
"type" : {
"type" : "record",
"name" : "EmployeeInfo",
"fields" : [ {
"name" : "name",
"type" : [ "null", "string" ],
"default" : null
} ]
}
} ]
},
"java-class" : "java.util.HashMap"
} ],
"default" : null
} ]
}