FETCHGraph
FETCH
可以获取指定点或边的属性值。
openCypher兼容性Graph
本文操作仅适用于原生nGQL。
获取点的属性值Graph
语法Graph
FETCH PROP ON {<tag_name>[, tag_name ...] | *}
<vid> [, vid ...]
[YIELD <return_list> [AS <alias>]];
参数 | 说明 |
---|---|
tag_name |
Tag名称。 |
* |
表示当前图空间中的所有Tag。 |
vid |
点ID。 |
YIELD |
定义需要返回的输出。除了返回定义的属性,额外返回VertexID 。详情请参见Graph。如果没有YIELD 子句,默认返回vertices_ ,包含点的所有信息。 |
AS |
设置别名。 |
基于Tag获取点的属性值Graph
在FETCH
语句中指定Tag获取对应点的属性值。
nebula> FETCH PROP ON player "player100";
+----------------------------------------------------+
| vertices_ |
+----------------------------------------------------+
| ("player100" :player{age: 42, name: "Tim Duncan"}) |
+----------------------------------------------------+
获取点的指定属性值Graph
使用YIELD
子句指定返回的属性。
nebula> FETCH PROP ON player "player100" \
YIELD properties(vertex).name AS name;
+-------------+--------------+
| VertexID | name |
+-------------+--------------+
| "player100" | "Tim Duncan" |
+-------------+--------------+
获取多个点的属性值Graph
指定多个点ID获取多个点的属性值,点之间用英文逗号(,)分隔。
nebula> FETCH PROP ON player "player101", "player102", "player103";
+-----------------------------------------------------------+
| vertices_ |
+-----------------------------------------------------------+
| ("player101" :player{age: 36, name: "Tony Parker"}) |
| ("player102" :player{age: 33, name: "LaMarcus Aldridge"}) |
| ("player103" :player{age: 32, name: "Rudy Gay"}) |
+-----------------------------------------------------------+
基于多个Tag获取点的属性值Graph
在FETCH
语句中指定多个Tag获取属性值。Tag之间用英文逗号(,)分隔。
# 创建新Tag t1。
nebula> CREATE TAG t1(a string, b int);
# 为点player100添加Tag t1。
nebula> INSERT VERTEX t1(a, b) VALUE "player100":("Hello", 100);
# 基于Tag player和t1获取点player100上的属性值。
nebula> FETCH PROP ON player, t1 "player100";
+----------------------------------------------------------------------------+
| vertices_ |
+----------------------------------------------------------------------------+
| ("player100" :t1{a: "Hello", b: 100} :player{age: 42, name: "Tim Duncan"}) |
+----------------------------------------------------------------------------+
用户可以在FETCH
语句中组合多个Tag和多个点。
nebula> FETCH PROP ON player, t1 "player100", "player103";
+----------------------------------------------------------------------------+
| vertices_ |
+----------------------------------------------------------------------------+
| ("player100" :t1{a: "Hello", b: 100} :player{age: 42, name: "Tim Duncan"}) |
| ("player103" :player{age: 32, name: "Rudy Gay"}) |
+----------------------------------------------------------------------------+
在所有标签中获取点的属性值Graph
在FETCH
语句中使用*
获取当前图空间所有标签里,点的属性值。
nebula> FETCH PROP ON * "player100", "player106", "team200";
+----------------------------------------------------------------------------+
| vertices_ |
+----------------------------------------------------------------------------+
| ("player106" :player{age: 25, name: "Kyle Anderson"}) |
| ("team200" :team{name: "Warriors"}) |
| ("player100" :t1{a: "Hello", b: 100} :player{age: 42, name: "Tim Duncan"}) |
+----------------------------------------------------------------------------+
获取边的属性值Graph
语法Graph
FETCH PROP ON <edge_type> <src_vid> -> <dst_vid>[@<rank>] [, <src_vid> -> <dst_vid> ...]
[YIELD <output>]
参数 | 说明 |
---|---|
edge_type |
Edge type名称。 |
src_vid |
起始点ID,表示边的起点。 |
dst_vid |
目的点ID,表示边的终点。 |
rank |
边的rank。可选参数,默认值为0 。起始点、目的点、Edge type和rank可以唯一确定一条边。 |
YIELD |
定义需要返回的输出。除了返回定义的属性,额外返回起始点ID 、目的点ID 和rank 。详情请参见Graph。如果没有YIELD 子句,默认返回edges_ ,包含边的所有信息。 |
获取边的所有属性值Graph
# 获取连接player100和team204的边serve的所有属性值。
nebula> FETCH PROP ON serve "player100" -> "team204";
+-----------------------------------------------------------------------+
| edges_ |
+-----------------------------------------------------------------------+
| [:serve "player100"->"team204" @0 {end_year: 2016, start_year: 1997}] |
+-----------------------------------------------------------------------+
获取边的指定属性值Graph
使用YIELD
子句指定返回的属性。
nebula> FETCH PROP ON serve "player100" -> "team204" \
YIELD properties(edge).start_year;
+-------------+------------+-------------+-----------------------------+
| serve._src | serve._dst | serve._rank | properties(EDGE).start_year |
+-------------+------------+-------------+-----------------------------+
| "player100" | "team204" | 0 | 1997 |
+-------------+------------+-------------+-----------------------------+
获取多条边的属性值Graph
指定多个边模式(<src_vid> -> <dst_vid>[@<rank>]
)获取多个边的属性值。模式之间用英文逗号(,)分隔。
nebula> FETCH PROP ON serve "player100" -> "team204", "player133" -> "team202";
+-----------------------------------------------------------------------+
| edges_ |
+-----------------------------------------------------------------------+
| [:serve "player100"->"team204" @0 {end_year: 2016, start_year: 1997}] |
| [:serve "player133"->"team202" @0 {end_year: 2011, start_year: 2002}] |
+-----------------------------------------------------------------------+
基于rank获取属性值Graph
如果有多条边,起始点、目的点和Edge type都相同,可以通过指定rank获取正确的边属性值。
# 插入不同属性值、不同rank的边。
nebula> insert edge serve(start_year,end_year) \
values "player100"->"team204"@1:(1998, 2017);
nebula> insert edge serve(start_year,end_year) \
values "player100"->"team204"@2:(1990, 2018);
# 默认返回rank为0的边。
nebula> FETCH PROP ON serve "player100" -> "team204";
+-----------------------------------------------------------------------+
| edges_ |
+-----------------------------------------------------------------------+
| [:serve "player100"->"team204" @0 {end_year: 2016, start_year: 1997}] |
+-----------------------------------------------------------------------+
# 要获取rank不为0的边,请在FETCH语句中设置rank。
nebula> FETCH PROP ON serve "player100" -> "team204"@1;
+-----------------------------------------------------------------------+
| edges_ |
+-----------------------------------------------------------------------+
| [:serve "player100"->"team204" @1 {end_year: 2017, start_year: 1998}] |
+-----------------------------------------------------------------------+
复合语句中使用FETCHGraph
将FETCH
与原生nGQL结合使用是一种常见的方式,例如和GO
一起。
# 返回从点player101开始的follow边的degree值。
nebula> GO FROM "player101" OVER follow \
YIELD src(edge) AS s, dst(edge) AS d \
| FETCH PROP ON follow $-.s -> $-.d \
YIELD properties(edge).degree;
+-------------+-------------+--------------+-------------------------+
| follow._src | follow._dst | follow._rank | properties(EDGE).degree |
+-------------+-------------+--------------+-------------------------+
| "player101" | "player100" | 0 | 95 |
| "player101" | "player102" | 0 | 90 |
| "player101" | "player125" | 0 | 95 |
+-------------+-------------+--------------+-------------------------+
用户也可以通过自定义变量构建类似的查询。
nebula> $var = GO FROM "player101" OVER follow \
YIELD src(edge) AS s, dst(edge) AS d; \
FETCH PROP ON follow $var.s -> $var.d \
YIELD properties(edge).degree;
+-------------+-------------+--------------+-------------------------+
| follow._src | follow._dst | follow._rank | properties(EDGE).degree |
+-------------+-------------+--------------+-------------------------+
| "player101" | "player100" | 0 | 95 |
| "player101" | "player102" | 0 | 90 |
| "player101" | "player125" | 0 | 95 |
+-------------+-------------+--------------+-------------------------+
更多复合语句的详情,请参见Graph。
最后更新: October 27, 2021