[英]How can I get Avro SchemaBuilder to build a schema of a map between two 'records'?
Trying to serialize Java Objects represented as a Map.尝试序列化 Java 表示为 Map 的对象。 This requires me to build an Avro Schema first.
这需要我先构建一个 Avro Schema。 I am using Avro SchemaBuilder for schema generation.
我正在使用 Avro SchemaBuilder生成模式。 Have already succeeded in schema generation for Map's objects.
已经成功地为 Map 的对象生成模式。 However can't deal with schema building for overall Map.
但是无法处理整体 Map 的架构构建。 Final schema should look like the following (created this one manually):
最终架构应如下所示(手动创建此架构):
{
"type": "record",
"name": "MapObject",
"namespace": "test",
"fields": [
{
"name": "CacheMap",
"type": {
"type": "map",
"values": [
{
"type": "record",
"name": "User",
"namespace": "test",
"fields": [
{
"name": "id",
"type": "long"
},
{
"name": "status",
"type": "string"
}
]
},
{
"type": "record",
"name": "UserKey",
"namespace": "test",
"fields": [
{
"name": "key",
"type": "long"
},
{
"name": "keyPrint",
"type": "string"
}
]
}
]
}
}
]
}
How can I pass to different records for key and value of a map in SchemaBuilder?如何在 SchemaBuilder 中将 map 的键和值传递给不同的记录?
SchemaBuilder.map().values()
does not allow to pass more than one Schema as a parameter for .value()
. SchemaBuilder.map().values()
不允许传递多个 Schema 作为.value()
的参数。
Apache Avro Unions can be of help here. Apache Avro Unions可以在这里提供帮助。
SchemaBuilder.builder()
.map()
.values(SchemaBuilder.unionOf().type(firstSchema).and().type(secondSchema).endUnion());
where the firstSchema
and the secondSchema
are separate schemas already defined for required User and UserKey 'records' (considering the schema from question)其中
firstSchema
和secondSchema
是已为所需的 User 和 UserKey 'records' 定义的单独模式(考虑问题中的模式)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.