如何使用 Avro 创建包含对象列表的模式?

新手上路,请多包涵

有谁知道如何创建包含某个类的对象列表的 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 类?

有人可以帮忙吗?

原文由 Shekhar 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 850
2 个回答

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

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

原文由 Kapil Balagi 发布,翻译遵循 CC BY-SA 3.0 许可协议

我有以下类,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"
                        }
                    ]
                }
            }
        }
    ]
}

原文由 Smart Coder 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题