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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25076786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:34:40  来源:igfitidea点击:

How to create schema containing list of objects using Avro?

javaschemaavro

提问by Shekhar

Does anyone knows how to create Avro schema which contains list of objects of some class?

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

I want my generated classes to look like below :

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

class Child {
    String name;
}

class Parent {
    list<Child> children;
}

For this, I have written part of schema file but do not know how to tell Avro to create list of objects of type Children?

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

My schema file looks like below :

我的架构文件如下所示:

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

Now problem is that I can mark field childrenas either Childtype or array but do not know how to mark it as a array of objects of type Childclass?

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

Can anyone please help?

有人可以帮忙吗?

采纳答案by Kapil Balagi

You need to use arraytype for creating the list. Following is the updated schema that handles your usecase.

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

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

回答by Smart Coder

I had following class and avro maven plugin generated two classes accordingly :

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

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

public class Account {
    String accountNumber;
    String id;
}

Avro file format :

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