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

How to create schema containing list of objects using Avro?

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

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

1
2
3
4
5
6
7
class Child {
    String name;
}

class Parent {
    list<Child> children;
}

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

我的架构文件如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
   "name":"Parent",
   "type":"record",
   "fields":[
        {
           "name":"children",
           "type":{
               "name":"Child",
               "type":"record",
               "fields":[
                    {"name":"name","type":"string"}
                ]
            }
        }
    ]
}

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

任何人都可以帮忙吗?


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
   "name":"Parent",
   "type":"record",
   "fields":[
        {
           "name":"children",
           "type":{
               "type":"array",  
               "items":{
                   "name":"Child",
                   "type":"record",
                   "fields":[
                        {"name":"name","type":"string"}
                    ]
                }
            }
        }
    ]
}


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

1
2
3
4
5
6
7
8
9
10
public class Employees{
    String accountNumber;
    String address;
    List<Account> accountList;    
}

public class Account {
    String accountNumber;
    String id;
}

Avro文件格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
   "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"
                        }
                    ]
                }
            }
        }
    ]
}