数据来源
这里使用的数据数据是使用 Nebula Console 创建的测试数据
nebula> :play nba
https://docs.nebula-graph.com.cn/2.6.1/2.quick-start/3.connect-to-nebula-graph/#_3
数据结构
> show tags
+------------+
| Name |
+------------+
| "bachelor" |
| "player" |
| "team" |
+------------+
Got 3 rows (time spent 995/3049 us)
show edges;
+------------+
| Name |
+------------+
| "like" |
| "serve" |
| "teammate" |
+------------+
Got 3 rows (time spent 1105/3187 us)
show tag indexes;
+---------------------+----------+----------+
| Index Name | By Tag | Columns |
+---------------------+----------+----------+
| "player_age_index" | "player" | ["age"] |
| "player_name_index" | "player" | ["name"] |
| "team_name_index" | "team" | ["name"] |
+---------------------+----------+----------+
Got 3 rows (time spent 1043/2790 us)
show edge indexes;
+-------------------+------------+----------------+
| Index Name | By Edge | Columns |
+-------------------+------------+----------------+
| "like_index" | "like" | ["likeness"] |
| "serve_index1" | "serve" | ["start_year"] |
| "serve_index2" | "serve" | ["end_year"] |
| "teammate_index1" | "teammate" | ["start_year"] |
| "teammate_index2" | "teammate" | ["end_year"] |
+-------------------+------------+----------------+
Got 5 rows (time spent 1586/3821 us)
常用查询语句
GO 语句
GO 语句可以根据指定的条件遍历数据库。GO语句从一个或多个点开始,沿着一条或多条边遍历,返回YIELD子句中指定的信息。
查询球员 VID "Tim Duncan" 关注 (like) 的其他球员。
go from "Tim Duncan" over like;
+-----------------+
| like._dst |
+-----------------+
| "Manu Ginobili" |
| "Tony Parker" |
+-----------------+
Got 2 rows (time spent 2062/4471 us)
查询球员 VID "Tim Duncan" 关注的球员,被关注球员年龄需大于 38 岁。 返回其姓名和年龄,并取别名为 Teammate 和 Age。
go from "Tim Duncan" over like \
-> where $$.player.age >= 38 \
-> yield $$.player.name as teammate, $$.player.age as age;
+-----------------+-----+
| teammate | age |
+-----------------+-----+
| "Manu Ginobili" | 41 |
+-----------------+-----+
Got 1 rows (time spent 3056/6108 us)
说明:
- YIELD 指定希望从查询中返回的结果。
- $$ 表示边上的目的点。
- \ 表示换行符。是命令行特有的。
查询球员 "Tim Duncan" 关注的球员 所效力的球队。
使用 管道(|) 来组合两个查询语句
> go from "Tim Duncan" over like yield like._dst as id | \
-> go from $-.id over serve yield $$.team.name as Team, $^.player.name as Player;
+-----------+-----------------+
| Team | Player |
+-----------+-----------------+
| "Spurs" | "Manu Ginobili" |
| "Hornets" | "Tony Parker" |
| "Spurs" | "Tony Parker" |
+-----------+-----------------+
Got 3 rows (time spent 3635/7094 us)
说明:
- 球员 "Tim Duncan" 关注的球员 go from "Tim Duncan" over like yield like._dst as id
- $^ 表示边的起始点。
- | 表示管道。
- \(- 表示输入流。上一个查询的输出(id)作为下一个查询的输入(\)-.id)。
使用自定义的变量来组合两个查询语句
$var = go from "Tim Duncan" over like yield like._dst as id ; \
-> go from $var.id over serve yield $$.team.name as Team, $^.player.name as Player;
+-----------+-----------------+
| Team | Player |
+-----------+-----------------+
| "Spurs" | "Manu Ginobili" |
| "Hornets" | "Tony Parker" |
| "Spurs" | "Tony Parker" |
+-----------+-----------------+
Got 3 rows (time spent 3822/7353 us)
当一条组合语句被整体提交给服务器后,该语句内的自定义变量就已结束生命周期。
LOOKUP 语句
LOOKUP 语句是基于索引 的,和WHERE子句一起使用,查找符合特定条件的数据。
返回名称为 Tony Parker,标签为 player 的顶点。
lookup on player where player.name == "Tony Parker" yield player.name,player.age;
+---------------+---------------+------------+
| VertexID | player.name | player.age |
+---------------+---------------+------------+
| "Tony Parker" | "Tony Parker" | 36 |
+---------------+---------------+------------+
Got 1 rows (time spent 2078/4748 us)
FETCH 语句
FETCH 语句可以获得点或边的属性。
如果要获取点的数据,则必须指定点的标签和点 VID;
如果要获取边数据,则必须指定边的类型、起始点 VID 和目标点 VID。
FETCH PROP ON player "Tony Parker";
+-------------------------------------------------------+
| vertices_ |
+-------------------------------------------------------+
| ("Tony Parker" :player{age: 36, name: "Tony Parker"}) |
+-------------------------------------------------------+
Got 1 rows (time spent 1358/3745 us)
FETCH PROP ON serve "Tony Parker" -> "Spurs";
+-----------------------------------------------------------------------+
| edges_ |
+-----------------------------------------------------------------------+
| [:serve "Tony Parker"->"Spurs" @0 {end_year: 2018, start_year: 1999}] |
+-----------------------------------------------------------------------+
Got 1 rows (time spent 2098/5039 us)
MATCH 语句
MATCH 语句是查询图数据最常用的,可以灵活的描述各种图模式,但是它依赖索引 去匹配 Nebula Graph 中的数据模型,性能也还需要调优。
匹配点的属性的前提是 Tag 本身有对应属性的索引,否则,用户无法执行 MATCH 语句匹配该属性。
match (v:player) return v ;
match (v:player{name:"Tim Duncan"}) return v ;
+-------------------------------------------------------------------------------------------------------------+
| v |
+-------------------------------------------------------------------------------------------------------------+
| ("Tim Duncan" :bachelor{name: "Tim Duncan", speciality: "psychology"} :player{age: 42, name: "Tim Duncan"}) |
+-------------------------------------------------------------------------------------------------------------+
Got 1 rows (time spent 3920/7082 us)
更多match的例子可以看
https://docs.nebula-graph.com.cn/2.6.1/3.ngql-guide/7.general-query-statements/2.match/