跳转至

GET SUBGRAPH

GET SUBGRAPH语句检索指定 Edge type 的起始点可以到达的点和边的信息,返回子图信息。

语法

GET SUBGRAPH [WITH PROP] [<step_count> STEPS] FROM {<vid>, <vid>...}
[{IN | OUT | BOTH} <edge_type>, <edge_type>...]
[YIELD [VERTICES AS <vertex_alias>] [,EDGES AS <edge_alias>]];
  • WITH PROP:展示属性。不添加本参数则隐藏属性。
  • step_count:指定从起始点开始的跳数,返回从 0 到step_count跳的子图。必须是非负整数。默认值为 1。
  • vid:指定起始点 ID。
  • edge_type:指定 Edge type。可以用INOUTBOTH来指定起始点上该 Edge type 的方向。默认为BOTH
  • YIELD:定义需要返回的输出。可以仅返回点或边。必须设置别名。不使用YIELD定义输出结果时,默认返回_vertices_edges

Note

GET SUBGRAPH语句检索的路径类型为trail,即检索的路径只有点可以重复,边不可以重复。详情请参见路径

示例

以下面的示例图进行演示。

A sample graph for GET SUBGRAPH

插入测试数据:

nebula> CREATE SPACE IF NOT EXISTS subgraph(partition_num=15, replica_factor=1, vid_type=fixed_string(30));
nebula> USE subgraph;
nebula> CREATE TAG IF NOT EXISTS player(name string, age int);
nebula> CREATE TAG IF NOT EXISTS team(name string);
nebula> CREATE EDGE IF NOT EXISTS follow(degree int);
nebula> CREATE EDGE IF NOT EXISTS serve(start_year int, end_year int);
nebula> INSERT VERTEX player(name, age) VALUES "player100":("Tim Duncan", 42);
nebula> INSERT VERTEX player(name, age) VALUES "player101":("Tony Parker", 36);
nebula> INSERT VERTEX player(name, age) VALUES "player102":("LaMarcus Aldridge", 33);
nebula> INSERT VERTEX team(name) VALUES "team203":("Trail Blazers"), "team204":("Spurs");
nebula> INSERT EDGE follow(degree) VALUES "player101" -> "player100":(95);
nebula> INSERT EDGE follow(degree) VALUES "player101" -> "player102":(90);
nebula> INSERT EDGE follow(degree) VALUES "player102" -> "player100":(75);
nebula> INSERT EDGE serve(start_year, end_year) VALUES "player101" -> "team204":(1999, 2018),"player102" -> "team203":(2006,  2015);
  • 查询从点player101开始、0~1 跳、所有 Edge type 的子图。
    nebula> GET SUBGRAPH 1 STEPS FROM "player101" YIELD VERTICES AS nodes, EDGES AS relationships;
    +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+
    | nodes                                                                   | relationships                                                                                                               |
    +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+
    | [("player101" :player{})]                                               | [[:serve "player101"->"team204" @0 {}], [:follow "player101"->"player100" @0 {}], [:follow "player101"->"player102" @0 {}]] |
    | [("team204" :team{}), ("player100" :player{}), ("player102" :player{})] | [[:follow "player102"->"player100" @0 {}]]                                                                                  |
    +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+
    

    返回的子图如下。

    GET SUBGRAPH FROM "player101"

  • 查询从点player101开始、0~1 跳、follow类型的入边的子图。
    nebula> GET SUBGRAPH 1 STEPS FROM "player101" IN follow YIELD VERTICES AS nodes, EDGES AS relationships;
    +---------------------------+---------------+
    | nodes                     | relationships |
    +---------------------------+---------------+
    | [("player101" :player{})] | []            |
    | []                        | []            |
    +---------------------------+---------------+
    

    因为player101没有follow类型的入边。所以仅返回点player101

  • 查询从点player101开始、0~1 跳、serve类型的出边的子图,同时展示边的属性。
    nebula> GET SUBGRAPH WITH PROP 1 STEPS FROM "player101" OUT serve YIELD VERTICES AS nodes, EDGES AS relationships;
    +-------------------------------------------------------+-------------------------------------------------------------------------+
    | nodes                                                 | relationships                                                           |
    +-------------------------------------------------------+-------------------------------------------------------------------------+
    | [("player101" :player{age: 36, name: "Tony Parker"})] | [[:serve "player101"->"team204" @0 {end_year: 2018, start_year: 1999}]] |
    | [("team204" :team{name: "Spurs"})]                    | []                                                                      |
    +-------------------------------------------------------+-------------------------------------------------------------------------+
    

    返回的子图如下。

    GET SUBGRAPH FROM "101" OUT serve

FAQ

为什么返回结果中会出现超出step_count跳数之外的关系?

为了展示子图的完整性,会在满足条件的所有点上额外查询一跳。例如下图。

FAQ

  • GET SUBGRAPH 1 STEPS FROM "A";查询的满足结果的路径是A->BB->AA->C,为了子图的完整性,会在满足结果的点上额外查询一跳,即B->C
  • GET SUBGRAPH 1 STEPS FROM "A" IN follow;查询的满足结果的路径是B->A,在满足结果的点上额外查询一跳,即A->B

如果只是查询满足条件的路径或点,建议使用 MATCHGO 语句。例如:

nebula> match p= (v:player) -- (v2) where id(v)=="A" return p;

nebula> go 1 steps from "A" over follow;

为什么返回结果中会出现低于step_count跳数的关系?

查询到没有多余子图数据时会停止查询,且不会返回空值。

nebula> GET SUBGRAPH 100 STEPS FROM "player101" OUT follow YIELD VERTICES AS nodes, EDGES AS relationships;
+----------------------------------------------------+--------------------------------------------------------------------------------------+
| nodes                                              | relationships                                                                        |
+----------------------------------------------------+--------------------------------------------------------------------------------------+
| [("player101" :player{})]                          | [[:follow "player101"->"player100" @0 {}], [:follow "player101"->"player102" @0 {}]] |
| [("player100" :player{}), ("player102" :player{})] | [[:follow "player102"->"player100" @0 {}]]                                           |
+----------------------------------------------------+--------------------------------------------------------------------------------------+

最后更新: November 24, 2021
Back to top