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

She*_*har 26 java schema avro

有谁知道如何创建包含某些类对象列表的Avro架构?

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

class Child {
    String name;
}

class Parent {
    list<Child> children;
}
Run Code Online (Sandbox Code Playgroud)

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

我的架构文件如下所示:

{
    "name": "Parent",
    "type":"record",
    "fields":[
        {
            "name":"children",
            "type":{
                "name":"Child",
                "type":"record",
                "fields":[
                    {"name":"name", "type":"string"}
                ]
            }
        }
    ] 
}
Run Code Online (Sandbox Code Playgroud)

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

有人可以帮忙吗?

小智 54

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

{
    "name": "Parent",
    "type":"record",
    "fields":[
        {
            "name":"children",
            "type":{
                "type": "array",  
                "items":{
                    "name":"Child",
                    "type":"record",
                    "fields":[
                        {"name":"name", "type":"string"}
                    ]
                }
            }
        }
    ] 
}
Run Code Online (Sandbox Code Playgroud)


Sma*_*der 7

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

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

public class Account {
    String accountNumber;
    String id;
}
Run Code Online (Sandbox Code Playgroud)

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"
                        }
                    ]
                }
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)