如何编写包含数组的数组字段的Avro Schema?
Avro二维数组字段Schema修正方案
问题根源
当前写法的核心错误有两处:
- 内层数组的类型定义多套了一层多余的
type对象包装 - default配置的层级不符合Avro语法规范
修正后完整Schema
{ "name": "SelfHealingStarter", "namespace": "SCP.Kafka.AvroSchemas", "doc": "Message with all the necessary information to run Self Healing process.", "type": "record", "fields": [ { "name": "FiveMinutesAgoMeasurement", "type": "record", "doc": "Field with all five minutes ago measurement.", "fields": [ { "name": "equipments", "doc": "List with all equipments measurement, each entry corresponds to a string array of single equipment measurement data.", "type": { "type": "array", "items": { "type": "array", "items": "string", "default": [] } }, "default": [] } ] } ] }
核心调整说明
- 移除了内层数组外多余的
type嵌套:外层数组的items属性直接接收内层数组的类型定义即可,不需要额外包裹{"type": XXX}结构 - 内层数组的
default配置放在内层数组类型定义的同级,符合Avro类型属性的语法要求 - 修正后Schema对应的数据结构为二维字符串数组,可完美匹配「数组的数组」的需求,两层default配置分别对应外层数组为空、内层子数组为空的默认 fallback 值
内容的提问来源于stack exchange,提问作者Felipe Thomazi
相关产品推荐
相关产品推荐

