是否可以在Avro架构中有一个可选字段(即该字段在.JSON文件中根本不显示)?
在我的Avro架构中,我有两个字段:
{"name": "author", "type": ["null", "string"], "default": null},
{"name": "importance", "type": ["null", "string"], "default": null},
Run Code Online (Sandbox Code Playgroud)
在我的JSON文件中,这两个字段可以存在与否.
但是,当它们不存在时,我收到一个错误(例如,当我使用avro-tools命令行客户端测试这样的JSON文件时):
Expected field name not found: author
Run Code Online (Sandbox Code Playgroud)
我理解只要字段名称存在于JSON中,它可以是null或者是string值,但我想表达的是"如果这些字段名称不存在则此JSON有效,或者如果它们是存在,它们是null或字符串".
这是否可以在Avro架构中表达?如果是这样,怎么样?
arv*_*v_s 25
您可以将默认属性定义为未定义的示例。所以可以跳过该字段。
{
"name": "first_name",
"type": "string",
"default": "undefined"
},
此外,所有字段在 avro 中都是强制性的。如果您希望它是可选的,则将其类型与 null 联合。例子:
Run Code Online (Sandbox Code Playgroud){ "name": "username", "type": [ "null", "string" ], "default": null },
根据 avro 规范,这是可能的,使用默认属性。
请参阅https://avro.apache.org/docs/1.8.2/spec.html
default:此字段的默认值,在读取缺少此字段的实例时使用(可选)。根据下表,允许的值取决于字段的架构类型。联合字段的默认值对应于联合中的第一个架构。
在您提供的示例中,您确实添加了值为“null”的默认属性,因此这应该可以工作。但是,支持这一点还取决于您用于读取 avro 消息的库(在 c、c++、python、java、c#、ruby 等中有库)。也许(可能)您使用的库缺少此功能。
| 归档时间: |
|
| 查看次数: |
5821 次 |
| 最近记录: |