跳转至

FETCH

FETCH可以获取指定点或边的属性值。

openCypher 兼容性

本文操作仅适用于原生 nGQL。

获取点的属性值

语法

FETCH PROP ON {<tag_name>[, tag_name ...] | *}
<vid> [, vid ...]
[YIELD <return_list> [AS <alias>]];
参数 说明
tag_name Tag 名称。
* 表示当前图空间中的所有 Tag。
vid 点 ID。
YIELD 定义需要返回的输出。除了返回定义的属性,额外返回VertexID。详情请参见 YIELD。如果没有YIELD子句,默认返回vertices_,包含点的所有信息。
AS 设置别名。

基于 Tag 获取点的属性值

FETCH语句中指定 Tag 获取对应点的属性值。

nebula> FETCH PROP ON player "player100";
+----------------------------------------------------+
| vertices_                                          |
+----------------------------------------------------+
| ("player100" :player{age: 42, name: "Tim Duncan"}) |
+----------------------------------------------------+

获取点的指定属性值

使用YIELD子句指定返回的属性。

nebula> FETCH PROP ON player "player100" \
        YIELD properties(vertex).name AS name;
+-------------+--------------+
| VertexID    | name         |
+-------------+--------------+
| "player100" | "Tim Duncan" |
+-------------+--------------+

获取多个点的属性值

指定多个点 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 获取点的属性值

FETCH语句中指定多个 Tag 获取属性值。Tag 之间用英文逗号(,)分隔。

# 创建新 Tag t1。
nebula> CREATE TAG IF NOT EXISTS 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"})                           |
+----------------------------------------------------------------------------+

在所有标签中获取点的属性值

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"}) |
+----------------------------------------------------------------------------+

获取边的属性值

语法

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目的点 IDrank。详情请参见 YIELD。如果没有YIELD子句,默认返回edges_,包含边的所有信息。

获取边的所有属性值

# 获取连接 player100 和 team204 的边 serve 的所有属性值。
nebula> FETCH PROP ON serve "player100" -> "team204";
+-----------------------------------------------------------------------+
| edges_                                                                |
+-----------------------------------------------------------------------+
| [:serve "player100"->"team204" @0 {end_year: 2016, start_year: 1997}] |
+-----------------------------------------------------------------------+

获取边的指定属性值

使用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                        |
+-------------+------------+-------------+-----------------------------+

获取多条边的属性值

指定多个边模式 (<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 获取属性值

如果有多条边,起始点、目的点和 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}] |
+-----------------------------------------------------------------------+

复合语句中使用 FETCH

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                      |
+-------------+-------------+--------------+-------------------------+

更多复合语句的详情,请参见复合查询(子句结构)


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