avro - 使用 Avro Schema 创建 n 叉树
问题描述
我需要在.avsc
文件中定义一个模式,该模式将使用 Maven 插件进行编译,以创建类。
该类必须是一棵树,其中根是不同的类型,所有节点都是另一种类型。
例如:
(Root) -> (Node A) -> (Node C) -> (Node F)
- -> (Node D)
- -> (Node E)
- -> (Node B) -> (Node F)
要遵循这种结构,类需要遵循:
class Node {
String id;
String name;
ArrayList<Node> children;
}
class Root {
ArrayList<Node> children;
}
我知道我可以简单地扩展SpecificRecordBase
以使我的类遵循Record
使用相同插件和 avro 模式生成的其他类的结构;但是有没有办法使用 avro 模式创建这样的结构?
解决方案
This should work:
{
"namespace": "com.tree.yournamespace",
"type": "record",
"name": "TreeRoot",
"fields": [
{
"name": "nodes", "type": {
"type": "array",
"items": {
"name": "Node",
"type": "record",
"fields": [
{"name": "children", "type": {
"type": "array",
"items": "Node"
}
}
]
}
}
}
]
}