在OrientDB中,如何将所有顶点作为JSON并排除结果中的边

时间:2015-11-19 19:59:08

标签: json orientdb graph-traversal

TRAVERSE vs fetchPlan

我有一个图表(我的例子是一个平衡的树),我想生成一个代表树的JSON结构。

以下是我如何生成用于测试的数据库:

我有两个文件,一个用于架构,一个用于数据。这里是FetchPlanTestingCreateSchema.sql文件内容:

SET ignoreErrors true;
DROP DATABASE remote:localhost/FetchPlanTesting admin admin;
SET ignoreErrors false;

CREATE DATABASE remote:localhost/FetchPlanTesting admin admin plocal graph;

CREATE CLASS Level01 extends V;
CREATE CLASS Level02 extends V;
CREATE CLASS Level03 extends V;
CREATE CLASS Level04 extends V;
CREATE CLASS Level05 extends V;
CREATE CLASS Level06 extends V;
CREATE CLASS Level07 extends V;
CREATE CLASS Level08 extends V;
CREATE CLASS Level09 extends V;

CREATE CLASS belongsTo extends E;

这是FetchPlanTestingData.sql。为简洁起见,我在显示3个级别(01,02和03),但在我的测试中,我已经填充了所有9个级别,因此我可以测试各种$depth设置:< / p>

CREATE VERTEX Level01 SET name = 'Item01_at_Level01';
CREATE VERTEX Level01 SET name = 'Item02_at_Level01';
CREATE VERTEX Level02 SET name = 'Item01_at_Level02';
CREATE EDGE belongsTo FROM (SELECT FROM Level02 WHERE name = 'Item01_at_Level02') TO (SELECT FROM Level01 WHERE name = 'Item02_at_Level01');
CREATE VERTEX Level02 SET name = 'Item02_at_Level02';
CREATE EDGE belongsTo FROM (SELECT FROM Level02 WHERE name = 'Item02_at_Level02') TO (SELECT FROM Level01 WHERE name = 'Item01_at_Level01');
CREATE VERTEX Level02 SET name = 'Item03_at_Level02';
CREATE EDGE belongsTo FROM (SELECT FROM Level02 WHERE name = 'Item03_at_Level02') TO (SELECT FROM Level01 WHERE name = 'Item02_at_Level01');
CREATE VERTEX Level02 SET name = 'Item04_at_Level02';
CREATE EDGE belongsTo FROM (SELECT FROM Level02 WHERE name = 'Item04_at_Level02') TO (SELECT FROM Level01 WHERE name = 'Item01_at_Level01');
CREATE VERTEX Level03 SET name = 'Item01_at_Level03';
CREATE EDGE belongsTo FROM (SELECT FROM Level03 WHERE name = 'Item01_at_Level03') TO (SELECT FROM Level02 WHERE name = 'Item02_at_Level02');
CREATE VERTEX Level03 SET name = 'Item02_at_Level03';
CREATE EDGE belongsTo FROM (SELECT FROM Level03 WHERE name = 'Item02_at_Level03') TO (SELECT FROM Level02 WHERE name = 'Item03_at_Level02');
CREATE VERTEX Level03 SET name = 'Item03_at_Level03';
CREATE EDGE belongsTo FROM (SELECT FROM Level03 WHERE name = 'Item03_at_Level03') TO (SELECT FROM Level02 WHERE name = 'Item04_at_Level02');
CREATE VERTEX Level03 SET name = 'Item04_at_Level03';
CREATE EDGE belongsTo FROM (SELECT FROM Level03 WHERE name = 'Item04_at_Level03') TO (SELECT FROM Level02 WHERE name = 'Item01_at_Level02');
CREATE VERTEX Level03 SET name = 'Item05_at_Level03';
CREATE EDGE belongsTo FROM (SELECT FROM Level03 WHERE name = 'Item05_at_Level03') TO (SELECT FROM Level02 WHERE name = 'Item02_at_Level02');
CREATE VERTEX Level03 SET name = 'Item06_at_Level03';
CREATE EDGE belongsTo FROM (SELECT FROM Level03 WHERE name = 'Item06_at_Level03') TO (SELECT FROM Level02 WHERE name = 'Item03_at_Level02');
CREATE VERTEX Level03 SET name = 'Item07_at_Level03';
CREATE EDGE belongsTo FROM (SELECT FROM Level03 WHERE name = 'Item07_at_Level03') TO (SELECT FROM Level02 WHERE name = 'Item04_at_Level02');
CREATE VERTEX Level03 SET name = 'Item08_at_Level03';
CREATE EDGE belongsTo FROM (SELECT FROM Level03 WHERE name = 'Item08_at_Level03') TO (SELECT FROM Level02 WHERE name = 'Item01_at_Level02');

我通过传递/opt/orientdb-community/bin/console.sh 'cat FetchPlanTestingCreateSchema.sql FetchPlanTestingData.sql'

来创建数据库/模式

使用TRAVERSE,我得到一个&#34;平坦的&#34;记录列表,它排除任何belongsTo边缘:

SELECT * FROM (TRAVERSE * FROM (SELECT FROM Level01 WHERE name = 'Item01_at_Level01') WHILE $depth<=2) WHERE @class <> 'belongsTo' LIMIT 1000

orientdb {db=FetchPlanTesting}> SELECT * FROM (TRAVERSE * FROM (SELECT FROM Level01 WHERE name = 'Item01_at_Level01') WHILE $depth<=2) WHERE @class <> 'belongsTo' LIMIT 1000

----+-----+-------+-----------------+------------+-------------
#   |@RID |@CLASS |name             |in_belongsTo|out_belongsTo
----+-----+-------+-----------------+------------+-------------
0   |#11:0|Level01|Item01_at_Level01|[size=2]    |null
1   |#12:1|Level02|Item02_at_Level02|[size=2]    |[size=1]
2   |#12:3|Level02|Item04_at_Level02|[size=2]    |[size=1]
----+-----+-------+-----------------+------------+-------------

3 item(s) found. Query executed in 0.006 sec(s).

耶!我可以遍历记录。但是,有了这个结果,我必须手动构建我的嵌套JSON。似乎无法使用图形数据库引擎。

使用fetchPlan,我越来越近了,但仍然不是我想要的。

orientdb {db=FetchPlanTesting}> SELECT @this.toJSON('fetchPlan:*:-1') FROM (SELECT FROM Level01 WHERE name = 'Item01_at_Level01')

----+------+----------------------------------------------------------------------------------------------------------------------------------
#   |@CLASS|this
----+------+----------------------------------------------------------------------------------------------------------------------------------
0   |null  |{"name":"Item01_at_Level01","in_belongsTo":[{"out":{"name":"Item02_at_Level02","in_belongsTo":[{"out":{"name":"Item01_at_Level0...
----+------+----------------------------------------------------------------------------------------------------------------------------------

1 item(s) found. Query executed in 0.112 sec(s).

这给了我树上的一切。这是我剩下的问题。 1.我如何限制$depth<=2

之类的内容
  1. 如何从结果中消除输入/输出/ in_belongsTo / out_belongsTo?

  2. 我可以使用其他方法,只包含某些类吗?

  3. 以下是使用fetchplan:*:5而不是`fetchplan:*: - 1&#39;:

    的更可读格式的JSON
    SELECT @this.toJSON('fetchPlan:*:5') FROM (SELECT FROM Level01 WHERE name = 'Item01_at_Level01')
    {
      "in_belongsTo": [
        {
          "in": "#11:0", 
          "out": {
            "in_belongsTo": [
              {
                "in": "#12:1", 
                "out": {
                  "in_belongsTo": [
                    {
                      "in": "#13:0", 
                      "out": "#14:7"
                    }, 
                    {
                      "in": "#13:0", 
                      "out": "#14:15"
                    }
                  ], 
                  "name": "Item01_at_Level03", 
                  "out_belongsTo": [
                    "#20:4"
                  ]
                }
              }, 
              {
                "in": "#12:1", 
                "out": {
                  "in_belongsTo": [
                    {
                      "in": "#13:4", 
                      "out": "#14:3"
                    }, 
                    {
                      "in": "#13:4", 
                      "out": "#14:11"
                    }
                  ], 
                  "name": "Item05_at_Level03", 
                  "out_belongsTo": [
                    "#20:8"
                  ]
                }
              }
            ], 
            "name": "Item02_at_Level02", 
            "out_belongsTo": [
              "#20:1"
            ]
          }
        }, 
        {
          "in": "#11:0", 
          "out": {
            "in_belongsTo": [
              {
                "in": "#12:3", 
                "out": {
                  "in_belongsTo": [
                    {
                      "in": "#13:2", 
                      "out": "#14:1"
                    }, 
                    {
                      "in": "#13:2", 
                      "out": "#14:9"
                    }
                  ], 
                  "name": "Item03_at_Level03", 
                  "out_belongsTo": [
                    "#20:6"
                  ]
                }
              }, 
              {
                "in": "#12:3", 
                "out": {
                  "in_belongsTo": [
                    {
                      "in": "#13:6", 
                      "out": "#14:5"
                    }, 
                    {
                      "in": "#13:6", 
                      "out": "#14:13"
                    }
                  ], 
                  "name": "Item07_at_Level03", 
                  "out_belongsTo": [
                    "#20:10"
                  ]
                }
              }
            ], 
            "name": "Item04_at_Level02", 
            "out_belongsTo": [
              "#20:3"
            ]
          }
        }
      ], 
      "name": "Item01_at_Level01"
    }
    

    我真的很想得到嵌套结构(比如第1级和第2级)并且生成的JSON看起来像这样:

    {
      "Level02": [
        {
          "name": "Item02_at_Level02", 
        }, 
        {
          "name": "Item04_at_Level02", }
        }
      ], 
      "name": "Item01_at_Level01"
    }
    

    更好的是这种结构:

    {
      "Level01": [
        {
          "name": "Item01_at_Level01",
          "Level02": [
            {
              "name": "Item02_at_Level02",
              "Level03": [
                {
                  "name": "Item01_at_Level03", 
                },
                {
                  "name": "Item05_at_Level03", 
                }
              ]
            },
            {
              "name": "Item04_at_Level02",
              "Level03": [
                {
                },
                {
                }
              ]
            }
          ]
        }
      ]
    }
    

    基本上,JSON,其中类是关键,然后是每个连接的类的嵌套结构,并且不包含任何输入/输出边缘信息。

1 个答案:

答案 0 :(得分:0)

对于问题的第一部分, 您可以从遍历查询中获取 $ depth 值并编写逻辑以从深度构建树

第二部分,

  1. 如果要按深度加载树,则可以通过给出[0-2]之类的输入来指定深度值,-1将递归加载整个树
  2. 2.你可以通过添加跳过边缘 [*]在_ *: - 2到fecth计划但是将在那里

    示例:

    选择@ this.toJSON(&#34; fetchPlan:[*] in_ : - 2 out _ : - 1&#34;)FROM rid