在Avro模式中使用可选字段时,需要显式指定字段类型为Union,其中包括要使用的数据类型及其null类型。如下所示:
{ "type": "record", "name": "example", "fields": [ {"name": "requiredField", "type": "string"}, {"name": "optionalField", "type": ["null", "string"]} ] }
在这个例子中,“optionalField”是可选的,因为它的类型是一个Union,其中包括null和string类型。如果没有指定field的类型为Union,则默认情况下为空,并且在读取模式时会引发异常。
上一篇:AVRO模式中的可选记录