How to create schema containing list of objects using Avro?

寵の児 提交于 2020-11-30 02:59:45

问题


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

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?

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 children as either Child type or array but do not know how to mark it as a array of objects of type Child class?

Can anyone please help?


回答1:


You need to use array type 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"}
                    ]
                }
            }
        }
    ] 
}



回答2:


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

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

public class Account {
    String accountNumber;
    String id;
}

Avro file format :

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


来源:https://stackoverflow.com/questions/25076786/how-to-create-schema-containing-list-of-objects-using-avro

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!