Авро Схема и Массивы - PullRequest
       12

Авро Схема и Массивы

0 голосов
/ 08 ноября 2019

В 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"}

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


  ]
}

1 Ответ

1 голос
/ 09 ноября 2019

Да! После определения 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"]}
  ]
}
...