我有以下JSON数据集:
{
"hashtags":null
}
{
"hashtags":[
"value1",
"value2"
]
}
以及从Kite SDK生成的以下Avro架构(看起来正确 - null或字符串数组的联合):
{
"type" : "record",
"name" : "tweet",
"fields" : [ {
"name" : "hashtags",
"type" : [ "null", {
"type" : "array",
"items" : "string"
} ],
"doc" : "Type inferred from 'null'"
} ]
}
当我尝试使用
转换数据时avro-tools fromjson --schema-file tweet.avsc twitter.json > twitter.avro
我收到以下错误(为简洁而修剪):
Exception in thread "main" org.apache.avro.AvroTypeException: Expected start-union. Got START_ARRAY
将null case更改为空数组:
{ "hashtags":null } to { "hashtags":[] }
将架构更改为允许字符串或在项目字段中为null
"type" : {
"type" : "array",
"items" : [ "null", "string" ]
}
一旦输入JSON中的字符串被限定为'字符串' ,
就可以正常工作。
因此,是否可以拥有一个可以为空的数组字段,或者从Avro的角度来看,是否使用空数组处理可空性?
答案 0 :(得分:0)
每当架构中有联合时,您必须明确告诉Avro将数据解释为什么类型。或者像你一样预处理你的数据,这样你就不需要工会了。你现在拥有它的方式,你会发现使用
"type" : {
"type" : "array",
"items" : "string"
}
也可以,因为您已将所有数据强制转换为数组类型。 null是不必要的。