You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Avro模式中嵌套记录的默认值设置方法问询

Setting Default Values for Nested Record Types in Avro

Got it, let's fix that missing field error for your address nested record and get Avro to handle defaults automatically. The key thing to remember here is that Avro requires default values to strictly match the field's declared type—so we have two main approaches depending on whether you want an empty record (dictionary) or an empty string as the fallback.

1. Default to an Empty Nested Record (Empty Dictionary)

If you want the missing address field to default to an empty instance of your nested record type, you need two things:

  • First, make sure every field inside the nested record has its own default value (otherwise Avro will throw an error when trying to create the empty instance).
  • Then, set the outer address field's default to a valid empty instance of that nested record.

Here's an example schema:

{
  "type": "record",
  "name": "User",
  "fields": [
    {"name": "name", "type": "string"},
    {"name": "address", "type": {
      "type": "record",
      "name": "Address",
      "fields": [
        {"name": "street", "type": "string", "default": ""},
        {"name": "city", "type": "string", "default": ""}
      ]
    }, "default": {"street": "", "city": ""}}
  ]
}

Now, when the address field is missing, Avro will automatically populate it with the empty Address record (a dictionary with empty strings for street and city).

2. Default to an Empty String (Union Type Hack)

If you specifically want an empty string as the default (instead of an empty record), you'll need to change the address field's type to a union that includes both your original nested record type and the string type. This lets Avro accept either a valid Address record or a string as the value, with the empty string as the fallback.

Example schema:

{
  "type": "record",
  "name": "User",
  "fields": [
    {"name": "name", "type": "string"},
    {"name": "address", "type": [
      {
        "type": "record",
        "name": "Address",
        "fields": [
          {"name": "street", "type": "string"},
          {"name": "city", "type": "string"}
        ]
      },
      "string"
    ], "default": ""}
  ]
}

Note: When using unions, the default value just needs to match one of the types in the union—here, the empty string matches the string type, so it's valid.

Bonus: Default to Null (Simpler Fallback)

If you don't mind using null instead of an empty dictionary or string, this is often the easiest approach. Just make address a union of null and your nested record type, then set the default to null:

{
  "type": "record",
  "name": "User",
  "fields": [
    {"name": "name", "type": "string"},
    {"name": "address", "type": ["null", {
      "type": "record",
      "name": "Address",
      "fields": [
        {"name": "street", "type": "string"},
        {"name": "city", "type": "string"}
      ]
    }], "default": null}
  ]
}

This avoids having to set defaults for every field in the nested record, and Avro will automatically use null when address is missing.


内容的提问来源于stack exchange,提问作者Sokratis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:53:46