具有默认对象数组的AVRO模式

时间:2020-10-19 15:42:19

标签: avro

我有一个带有数组字段的AVRO模式。该数组字段的项目是另一个AVRO模式的对象。下面是我现在拥有的代码。

{
  "name": "bars",
  "type": ["null", {
    "type": "array",
    "items": "com.example.Bar"
  }],
  "default": null
}

此代码是“ bars”的字段定义,“ bars”是一个包含“ com.example.Bar”对象的数组。

现在我已将默认值设置为“ null”,但是我想将默认值显式设置为3个Bar对象的数组。

想象我的酒吧有以下字段定义

{
  "name": "name",
  "type": [
    "null",
    "string"
  ],
  "default": null
},
{
  "name": "number",
  "type": [
    "null",
    "int"
  ],
  "default": null
}

我想将“酒吧”默认设置为

{
  "name": "bars",
  "type": ["null", {
    "type": "array",
    "items": "com.example.Bar"
  }],
  "default": [{"name": "bar1", "number":1}, {"name": "bar2", "number":2}, {"name": "bar3", "number":3}]
}

但是此代码不起作用。我应该如何设置对象的默认值?

2 个答案:

答案 0 :(得分:1)

联合字段的默认值对应于first schema in the union。如果默认值为数组,则数组架构应为联合中的第一个架构。

"type": [
  {
    "type": "array",
    "items": "com.example.Bar"
  },
  "null"
],

答案 1 :(得分:0)

问题是我允许将“ null”作为数组字段的类型。我想如果我想用不能不允许为null的值预填充默认字段。

因此以下代码解决了该问题。

{
  "name": "bars",
  "type": {
    "type": "array",
    "items": "com.example.Bar"
  },
  "default": [{"name": "bar1", "number":1}, {"name": "bar2", "number":2}, {"name": "bar3", "number":3}]
}