在avro模式java中定义对象数组



我有下面的avro架构内容:

"fields":[
{
"name" : "data",
"type" :[
"null" ,
{
"type":"array",
"items":[
"string",
"null"
]
}
],
"default":null
}

所以,这个数据目前是一个字符串列表。因此,我们有了一个新的要求:

我们已经设置了Type的对象,com.test.Pos和com.test.Trans(基本上,仓位/交易模型类型的数据都必须作为数据字段的一部分发送给消费者。因此,json解析在它们的末尾很容易(

**If the model was only one type, I could see we can set it as below:
"type": {"type" :"array" ,"items":"com.test.Position"}}**

但是,由于我必须将两个模型com.test.Pos/com.test.Trans设置为同一数据字段的一部分,[对于这两个数据,我们将为消费者使用相同的avsc文件]

我们如何才能做到这一点?我们应该如何定义avro,以便数据字段也可以接受这两种类型的对象(com.test.Pos和com.test.Trans(

您可以尝试以下操作:

[
{   
"type": "record",   
"name": "com.test.Pos",   
"aliases": ["com.test.Position"],   
"fields" : [     
{"name": "value", "type": "long"}
] 
},
{   
"type": "record",   
"name": "com.test.Trans",   
"aliases": ["com.test.Transition"],   
"fields" : [     
{"name": "value", "type": "int"}
] 
},
{   
"type": "record",   
"name": "data",   
"aliases": ["com.test.Union"],   
"fields" : [     
{"name": "foo", "type": ["null", "string", "com.test.Trans", "com.test.Pos"]}
] 
}]

转换为:

{
"oneOf" : [ {
"$ref" : "#/definitions/record:com.test.Pos"
}, {
"$ref" : "#/definitions/record:com.test.Trans"
}, {
"$ref" : "#/definitions/record:data"
} ],
"definitions" : {
"record:com.test.Pos" : {
"type" : "object",
"required" : [ "value" ],
"additionalProperties" : false,
"properties" : {
"value" : {
"type" : "integer",
"minimum" : -9223372036854775808,
"maximum" : 9223372036854775807
}
}
},
"record:com.test.Trans" : {
"type" : "object",
"required" : [ "value" ],
"additionalProperties" : false,
"properties" : {
"value" : {
"type" : "integer",
"minimum" : -2147483648,
"maximum" : 2147483647
}
}
},
"record:data" : {
"type" : "object",
"required" : [ "foo" ],
"additionalProperties" : false,
"properties" : {
"foo" : {
"oneOf" : [ {
"type" : "null"
}, {
"type" : "string"
}, {
"$ref" : "#/definitions/record:com.test.Trans"
}, {
"$ref" : "#/definitions/record:com.test.Pos"
} ]
}
}
}
}
}

最新更新