FETCH

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

openCypher兼容性

本文操作仅适用于nGQL扩展。

获取点的属性值

语法

  1. FETCH PROP ON {<tag_name>[, tag_name ...] | *}
  2. <vid> [, vid ...]
  3. [YIELD <output>]
参数说明
tag_name标签名称。
*表示当前图空间中的所有标签。
vid点ID。
output指定要返回的信息。详情请参见YIELD。如果没有YIELD子句,将返回所有匹配的信息。

基于标签获取点的属性值

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

  1. nebula> FETCH PROP ON player "player100";
  2. +----------------------------------------------------+
  3. | vertices_ |
  4. +----------------------------------------------------+
  5. | ("player100" :player{age: 42, name: "Tim Duncan"}) |
  6. +----------------------------------------------------+

获取点的指定属性值

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

  1. nebula> FETCH PROP ON player "player100" \
  2. YIELD player.name;
  3. +-------------+--------------+
  4. | VertexID | player.name |
  5. +-------------+--------------+
  6. | "player100" | "Tim Duncan" |
  7. +-------------+--------------+

获取多个点的属性值

指定多个点ID获取多个点的属性值,点之间用英文逗号(,)分隔。

  1. nebula> FETCH PROP ON player "player101", "player102", "player103";
  2. +-----------------------------------------------------------+
  3. | vertices_ |
  4. +-----------------------------------------------------------+
  5. | ("player101" :player{age: 36, name: "Tony Parker"}) |
  6. +-----------------------------------------------------------+
  7. | ("player102" :player{age: 33, name: "LaMarcus Aldridge"}) |
  8. +-----------------------------------------------------------+
  9. | ("player103" :player{age: 32, name: "Rudy Gay"}) |
  10. +-----------------------------------------------------------+

基于多个标签获取点的属性值

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

  1. # 创建新标签t1。
  2. nebula> CREATE TAG t1(a string, b int);
  3. # 为点player100添加标签t1。
  4. nebula> INSERT VERTEX t1(a, b) VALUE "player100":("Hello", 100);
  5. # 基于标签player和t1获取点player100上的属性值。
  6. nebula> FETCH PROP ON player, t1 "player100";
  7. +----------------------------------------------------------------------------+
  8. | vertices_ |
  9. +----------------------------------------------------------------------------+
  10. | ("player100" :t1{a: "Hello", b: 100} :player{age: 42, name: "Tim Duncan"}) |
  11. +----------------------------------------------------------------------------+

您可以在FETCH语句中组合多个标签和多个点。

  1. nebula> FETCH PROP ON player, t1 "player100", "player103";
  2. +----------------------------------------------------------------------------+
  3. | vertices_ |
  4. +----------------------------------------------------------------------------+
  5. | ("player100" :t1{a: "Hello", b: 100} :player{age: 42, name: "Tim Duncan"}) |
  6. +----------------------------------------------------------------------------+
  7. | ("player103" :player{age: 32, name: "Rudy Gay"}) |
  8. +----------------------------------------------------------------------------+

获取点上所有属性值

FETCH语句中使用*获取点上所有属性值。

  1. nebula> FETCH PROP ON * "player100", "player106", "team200";
  2. +----------------------------------------------------------------------------+
  3. | vertices_ |
  4. +----------------------------------------------------------------------------+
  5. | ("player106" :player{age: 25, name: "Kyle Anderson"}) |
  6. +----------------------------------------------------------------------------+
  7. | ("team200" :team{name: "Warriors"}) |
  8. +----------------------------------------------------------------------------+
  9. | ("player100" :t1{a: "Hello", b: 100} :player{age: 42, name: "Tim Duncan"}) |
  10. +----------------------------------------------------------------------------+

获取边的属性值

语法

  1. FETCH PROP ON <edge_type> <src_vid> -> <dst_vid>[@<rank>] [, <src_vid> -> <dst_vid> ...]
  2. [YIELD <output>]
参数说明
edge_type边类型名称。
src_vid起始点ID,表示边的起点。
dst_vid目的点ID,表示边的终点。
rank边的rank。可选参数,默认值为0。起始点、目的点、边类型和rank可以唯一确定一条边。
output指定要返回的信息。详情请参见YIELD。如果没有YIELD子句,将返回所有匹配的信息。

获取边的所有属性值

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

获取边的指定属性值

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

  1. nebula> FETCH PROP ON serve "player100" -> "team204" \
  2. YIELD serve.start_year;
  3. +-------------+------------+-------------+------------------+
  4. | serve._src | serve._dst | serve._rank | serve.start_year |
  5. +-------------+------------+-------------+------------------+
  6. | "player100" | "team204" | 0 | 1997 |
  7. +-------------+------------+-------------+------------------+

获取多条边的属性值

指定多个边模式(<src_vid> -> <dst_vid>[@<rank>])获取多个边的属性值。模式之间用英文逗号(,)分隔。

  1. nebula> FETCH PROP ON serve "player100" -> "team204", "player133" -> "team202";
  2. +-----------------------------------------------------------------------+
  3. | edges_ |
  4. +-----------------------------------------------------------------------+
  5. | [:serve "player100"->"team204" @0 {end_year: 2016, start_year: 1997}] |
  6. +-----------------------------------------------------------------------+
  7. | [:serve "player133"->"team202" @0 {end_year: 2011, start_year: 2002}] |
  8. +-----------------------------------------------------------------------+

基于rank获取属性值

如果有多条边,起始点、目的点和边类型都相同,可以通过指定rank获取正确的边属性值。

  1. # 插入不同属性值、不同rank的边。
  2. nebula> insert edge serve(start_year,end_year) \
  3. values "player100"->"team204"@1:(1998, 2017);
  4. nebula> insert edge serve(start_year,end_year) \
  5. values "player100"->"team204"@2:(1990, 2018);
  6. # 默认返回rank为0的边。
  7. nebula> FETCH PROP ON serve "player100" -> "team204";
  8. +-----------------------------------------------------------------------+
  9. | edges_ |
  10. +-----------------------------------------------------------------------+
  11. | [:serve "player100"->"team204" @0 {end_year: 2016, start_year: 1997}] |
  12. +-----------------------------------------------------------------------+
  13. # 要获取rank不为0的边,请在FETCH语句中设置rank。
  14. nebula> FETCH PROP ON serve "player100" -> "team204"@1;
  15. +-----------------------------------------------------------------------+
  16. | edges_ |
  17. +-----------------------------------------------------------------------+
  18. | [:serve "player100"->"team204" @1 {end_year: 2017, start_year: 1998}] |
  19. +-----------------------------------------------------------------------+

复合语句中使用FETCH

FETCH与nGQL扩展结合使用是一种常见的方式,例如和GO一起。

  1. # 返回从点player101开始的follow边的degree值。
  2. nebula> GO FROM "player101" OVER follow \
  3. YIELD follow._src AS s, follow._dst AS d \
  4. | FETCH PROP ON follow $-.s -> $-.d \
  5. YIELD follow.degree;
  6. +-------------+-------------+--------------+---------------+
  7. | follow._src | follow._dst | follow._rank | follow.degree |
  8. +-------------+-------------+--------------+---------------+
  9. | "player101" | "player100" | 0 | 95 |
  10. +-------------+-------------+--------------+---------------+
  11. | "player101" | "player102" | 0 | 90 |
  12. +-------------+-------------+--------------+---------------+
  13. | "player101" | "player125" | 0 | 95 |
  14. +-------------+-------------+--------------+---------------+

您也可以通过自定义变量构建类似的查询。

  1. nebula> $var = GO FROM "player101" OVER follow \
  2. YIELD follow._src AS s, follow._dst AS d; \
  3. FETCH PROP ON follow $var.s -> $var.d \
  4. YIELD follow.degree;
  5. +-------------+-------------+--------------+---------------+
  6. | follow._src | follow._dst | follow._rank | follow.degree |
  7. +-------------+-------------+--------------+---------------+
  8. | "player101" | "player100" | 0 | 95 |
  9. +-------------+-------------+--------------+---------------+
  10. | "player101" | "player102" | 0 | 90 |
  11. +-------------+-------------+--------------+---------------+
  12. | "player101" | "player125" | 0 | 95 |
  13. +-------------+-------------+--------------+---------------+

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