Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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"
                        }
                    ]
                }
            }
        }
    ]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top