avro union的json编码 [英] json encoding of avro union
问题描述
我的avro架构中有一个UNITED,其UNITED_NUMBER可以是null或int.当我对对象进行json编码时,会得到:
I have a union in my avro schema for favorite_number which could be a null or an int. When I json encode the object I get:
{"name":"Alyssa","favorite_number":{"int":7},"favorite_color":null}
{"name": "Alyssa", "favorite_number": {"int": 7}, "favorite_color": null}
我试图摆脱联合的类型指示符,在这种情况下为int,从而使其变为:
I am trying to get rid of the type indicator for the union, in this case the int, so that it becomes:
{名称":"Alyssa",收藏夹编号":7,收藏夹颜色":蓝色"}
{"name": "Alyssa", "favorite_number": 7, "favorite_color": "blue"}
avro模式:
{"name": "person", "type": "record",
"fields": [
{"name": "name", "type": "string"},
{"name": "favorite_number", "type": ["null", "int"], "default": null},
{"name": "favorite_color", "type": "string"}
]
}
有没有办法做到这一点?谢谢!
Is there a way to do this? Thanks!
推荐答案
编辑整个答案:)
似乎无法直接使用Avro API进行操作.有一段时间的拉取请求正在等待以支持可空类型
It looks like it is not possible do it directly with the Avro API. There is a pull request pending for a while to support this for nullable types
https://issues.apache.org/jira/browse/AVRO-1582
根据您的评论,我发现了尝试在没有数据类型的情况下进行序列化的相同问题,我最终使用了
Base on your comments, I found the same issue trying to serialize without data types, I ended up using toString function defined in https://avro.apache.org/docs/1.8.1/api/java/org/apache/avro/generic/GenericData.html#toString(java.lang.Object), The only cases where it does not work properly, is for schemas with circular references.
这篇关于avro union的json编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!