如何使用Avro创建包含对象列表的架构?

2022-09-01 01:50:49

有没有人知道如何创建包含某个类的对象列表的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"}
                ]
            }
        }
    ] 
}

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

任何人都可以帮忙吗?


答案 1

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

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

答案 2

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

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

public class Account {
    String accountNumber;
    String id;
}

阿夫罗文件格式 :

{
    "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"
                        }
                    ]
                }
            }
        }
    ]
}

推荐