GET SUBGRAPH¶
GET SUBGRAPH
语句查询并返回一个通过从指定点出发对图进行游走而生成的子图。在GET SUBGRAPH
语句中,用户可以指定游走的步数以及游走所经过的边的类型或方向。
语法¶
GET SUBGRAPH [WITH PROP] [<step_count> {STEP|STEPS}] FROM {<vid>, <vid>...}
[{IN | OUT | BOTH} <edge_type>, <edge_type>...]
[WHERE <expression> [AND <expression> ...]]
YIELD [VERTICES AS <vertex_alias>] [, EDGES AS <edge_alias>];
WITH PROP
:展示属性。不添加本参数则隐藏属性。
step_count
:指定从起始点开始的跳数,返回从 0 到step_count
跳的子图。必须是非负整数。默认值为 1。
vid
:指定起始点 ID。
edge_type
:指定 Edge type。可以用IN
、OUT
和BOTH
来指定起始点上该 Edge type 的方向。默认为BOTH
。
WHERE
:指定遍历的过滤条件,可以结合布尔运算符 AND 使用。
YIELD
:定义需要返回的输出。可以仅返回点或边。必须设置别名。
Note
GET SUBGRAPH
语句检索的路径类型为trail
,即检索的路径只有点可以重复,边不可以重复。详情请参见路径。
WHERE 语句限制¶
在GET SUBGRAPH
语句中使用WHERE
子句,注意以下限制:
- 仅支持
AND
运算符。 - 仅支持过滤目的点,点的格式为
$$.tagName.propName
。 - 支持过滤边,边的格式为
edge_type.propName
。 - 支持数学函数、聚合函数、字符串函数、日期时间函数、列表函数中的通用函数和类型转化函数。
- 不支持聚合函数、Schema 相关函数、条件表达式函数、谓词函数、geo 函数和自定义函数,列表函数中除通用函数以外的函数。
示例¶
以下面的示例图进行演示。
插入测试数据:
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 {}]] | +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+
返回的子图如下。
- 查询从点
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"})] | [] | +-------------------------------------------------------+-------------------------------------------------------------------------+
返回的子图如下。
- 查询从点
player101
开始、0~2 跳、follow
类型边degree
大于 90,年龄大于 30 的子图,同时展示边的属性。nebula> GET SUBGRAPH WITH PROP 2 STEPS FROM "player101" \ WHERE follow.degree > 90 AND $$.player.age > 30 \ YIELD VERTICES AS nodes, EDGES AS relationships; +-------------------------------------------------------+------------------------------------------------------+ | nodes | relationships | +-------------------------------------------------------+------------------------------------------------------+ | [("player101" :player{age: 36, name: "Tony Parker"})] | [[:follow "player101"->"player100" @0 {degree: 95}]] | | [("player100" :player{age: 42, name: "Tim Duncan"})] | [] | +-------------------------------------------------------+------------------------------------------------------+
FAQ¶
为什么返回结果中会出现超出step_count
跳数之外的关系?¶
为了展示子图的完整性,会在满足条件的所有点上额外查询一跳。例如下图。
- 用
GET SUBGRAPH 1 STEPS FROM "A";
查询的满足结果的路径是A->B
、B->A
和A->C
,为了子图的完整性,会在满足结果的点上额外查询一跳,即B->C
。
- 用
GET SUBGRAPH 1 STEPS FROM "A" IN follow;
查询的满足结果的路径是B->A
,在满足结果的点上额外查询一跳,即A->B
。
如果只是查询满足条件的路径或点,建议使用 MATCH 或 GO 语句。例如:
nebula> MATCH p= (v:player) -- (v2) WHERE id(v)=="A" RETURN p;
nebula> GO 1 STEPS FROM "A" OVER follow YIELD src(edge),dst(edge);
为什么返回结果中会出现低于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 {}]] |
+----------------------------------------------------+--------------------------------------------------------------------------------------+
最后更新:
September 4, 2023