【发布时间】:2015-08-06 19:43:05
【问题描述】:
我有以下 JSON 数据集:
{
"hashtags":null
}
{
"hashtags":[
"value1",
"value2"
]
}
以及从 Kite SDK 生成的以下 Avro 架构(看起来是正确的 - 空值或字符串数组的联合):
{
"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 大小写更改为空数组:
{ "hashtags":null } to { "hashtags":[] }
将架构更改为允许在项目字段中使用字符串或 null
"type" : {
"type" : "array",
"items" : [ "null", "string" ]
}
一旦输入 JSON 中的字符串被限定为“字符串”,就可以正常工作。
因此,是否可以有一个可为空的数组字段,或者,从 Avro 的角度来看,是否可以使用空数组来处理可空性?
【问题讨论】: