【问题标题】:Avro Schema and ArraysAvro 模式和数组
【发布时间】:2025-11-29 07:40:01
【问题描述】:

在 C# 中我可以定义这两个 POCO 来定义世代

public class Family
{
public List<Person> FamilyMembers {get; set;}
}

public class Person
{
public string FirstName {get; set;}
public string LastName {get; set;}
public List<Person> Children {get; set;}
}

我正在尝试定义一个 AVRO 模式来将 FamilyMembers 序列化为。是否可以在 Avro 中定义一个递归数组(不确定这是否是正确的术语),而不必像下面那样在模式中指定每一代。

{
  "type": "record",
  "name": "family",
  "namespace": "com.family.my",
  "fields": [

    {
     "name":"familymember",
            "type":{
                "type": "array",  
                "items":{
                    "name":"person",
                    "type":"record",
                    "fields":[
                                {"name":"firstname", "type":"string"},
                                {"name":"lastname",  "type":"string"},
                                {"name":"children",
                                            "type":{
                                            "type": "array",  
                                            "items":{
                                                    "name":"children",
                                                    "type":"record",
                                                    "fields":[
                                                                {"name":"firstname", "type":"string"},
                                                                {"name":"lastname",  "type":"string"},
                                                                 {"name":"grandchildren",
                                                                                "type":{
                                                                                "type": "array",  
                                                                                "items":{
                                                                                        "name":"greatgrandchildren",
                                                                                        "type":"record",
                                                                                        "fields":[
                                                                                                    {"name":"firstname", "type":"string"},
                                                                                                    {"name":"lastname",  "type":"string"}

                                                                                                ]   
                                                                                            }
                                                                                    }}
                                                            ]   
                                                        }
                                                }}
                    ]   
                }
            }
    }


  ]
}

【问题讨论】:

    标签: avro confluent-schema-registry


    【解决方案1】:

    是的!定义person 后,您可以将其用作children 数组中的项类型。例如:

    {
      "type": "record",
      "name": "family",
      "namespace": "com.family.my",
      "fields": [{
        "name":"familymember",
          "type":{
            "type": "array",  
            "items":{
              "name":"person",
              "type":"record",
              "fields":[
                {"name":"firstname", "type":"string"},
                {"name":"lastname",  "type":"string"},
                {"name":"children",  "type": { "type": "array", "items": "person" }}
              ]
            }
          }
      }]
    }
    

    这种递归模式由LongList 中的Avro spec 演示:

    {
      "type": "record",
      "name": "LongList",
      "aliases": ["LinkedLongs"],
      "fields" : [
        {"name": "value", "type": "long"},
        {"name": "next", "type": ["null", "LongList"]}
      ]
    }
    

    【讨论】: