首页 > 解决方案 > 使用 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 模式创建这样的结构?

标签: 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"
                                }
                           }
                      ]
                  }
              }
       }
 ]
}

推荐阅读