【问题标题】:How to create schema containing list of objects using Avro?如何使用 Avro 创建包含对象列表的模式?
【发布时间】:2014-09-24 10:53:36
【问题描述】:

有谁知道如何创建包含某个类的对象列表的 Avro 模式?

我希望我生成的类如下所示:

class Child {
    String name;
}

class Parent {
    list<Child> children;
}

为此,我编写了部分模式文件,但不知道如何告诉 Avro 创建 Children 类型的对象列表?

我的架构文件如下所示:

{
    "name": "Parent",
    "type":"record",
    "fields":[
        {
            "name":"children",
            "type":{
                "name":"Child",
                "type":"record",
                "fields":[
                    {"name":"name", "type":"string"}
                ]
            }
        }
    ] 
}

现在的问题是我可以将字段 children 标记为 Child 类型或数组,但不知道如何将其标记为 array of objects of type Child 类?

有人可以帮忙吗?

【问题讨论】:

    标签: java schema avro


    【解决方案1】:

    数组作为类型

    {
        "type": "record",
        "name": "jamesMedice",
        "fields": [{
            "name": "columns",
            "type": {
                "type": "array",
                "items": {
                    "type": "record",
                    "name": "columnValues",
                    "fields": [{
                            "name": "personId",
                            "type": "string",
                            "default": "null"
                        },
                        {
                            "name": "email",
                            "type": "string",
                            "default": "null"
                        }
                    ]
                }
            }
        }]
    }
    

    【讨论】:

      【解决方案2】:

      我有以下类,avro maven 插件相应地生成了两个类:

      public class Employees{
          String accountNumber;
          String address;
          List<Account> accountList;    
      }
      
      public class Account {
          String accountNumber;
          String id;
      }
      

      Avro 文件格式:

      {
          "type": "record",
          "namespace": "com.mypackage",
          "name": "AccountEvent",
          "fields": [
              {
                  "name": "accountNumber",
                  "type": "string"
              },
              {
                  "name": "address",
                  "type": "string"
              },
              {
                  "name": "accountList",
                  "type": {
                      "type": "array",
                      "items":{
                          "name": "Account",
                          "type": "record",
                          "fields":[
                              {   "name": "accountNumber",
                                  "type": "string"
                              },
                              {   "name": "id",
                                  "type": "string"
                              }
                          ]
                      }
                  }
              }
          ]
      }
      

      【讨论】:

        【解决方案3】:

        您需要使用array 类型来创建列表。 以下是处理您的用例的更新架构。

        {
            "name": "Parent",
            "type":"record",
            "fields":[
                {
                    "name":"children",
                    "type":{
                        "type": "array",  
                        "items":{
                            "name":"Child",
                            "type":"record",
                            "fields":[
                                {"name":"name", "type":"string"}
                            ]
                        }
                    }
                }
            ] 
        }
        

        【讨论】:

        • 你可能要考虑使用 List 代替,因为我在反序列化字符串数组时遇到了 NPE。
        • @qwetty 数据需要这样嵌套吗?如果我们必须有多个相同类型的字段怎么办?子类型的定义是否需要重复?似乎更清晰地区分类型
        • @Kapil 这个默认值是多少?
        • Avro-to-Hive 文档将 avro 类型列为“list”而不是“array”。 cwiki.apache.org/confluence/display/Hive/… Hive 文档是否可能不正确?
        猜你喜欢
        • 2019-06-03
        • 2022-09-25
        • 1970-01-01
        • 2020-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多