Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change example #1115

Merged
merged 2 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 52 additions & 97 deletions docs-2.0/2.quick-start/4.nebula-graph-crud.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

本文将使用下图的数据集演示基础操作的语法。

![The demo dataset](https://docs-cdn.nebula-graph.com.cn/docs-2.0/2.quick-start/dataset-for-crud.png)
![The demo dataset](dataset-for-crud.png)

## 检查Nebula Graph集群的机器状态

Expand All @@ -41,7 +41,6 @@ nebula> SHOW HOSTS;
| "storaged2" | 9779 | "ONLINE" | 0 | "No valid partition" | "No valid partition" |
| "Total" | __EMPTY__ | __EMPTY__ | 0 | __EMPTY__ | __EMPTY__ |
+-------------+-----------+-----------+--------------+----------------------+------------------------+
Got 4 rows (time spent 1061/2251 us)
```

在返回结果中,查看**Status**列,可以看到所有Storage服务都在线。
Expand Down Expand Up @@ -110,7 +109,6 @@ Got 4 rows (time spent 1061/2251 us)

```ngql
nebula> CREATE SPACE basketballplayer(partition_num=15, replica_factor=1, vid_type=fixed_string(30));
Execution succeeded (time spent 2817/3280 us)
```

2. 执行命令`SHOW HOSTS`检查分片的分布情况,确保平衡分布。
Expand All @@ -125,7 +123,6 @@ Got 4 rows (time spent 1061/2251 us)
| "storaged2" | 9779 | "ONLINE" | 5 | "basketballplayer:5" | "basketballplayer:5" |
| "Total" | | | 15 | "basketballplayer:15" | "basketballplayer:15" |
+-------------+-----------+-----------+--------------+----------------------------------+------------------------+
Got 4 rows (time spent 1633/2867 us)
```

如果**Leader distribution**分布不均匀,请执行命令`BALANCE LEADER`重新分配。更多信息,请参见[Storage负载均衡](../8.service-tuning/load-balance.md)。
Expand All @@ -134,7 +131,6 @@ Got 4 rows (time spent 1061/2251 us)

```ngql
nebula[(none)]> USE basketballplayer;
Execution succeeded (time spent 1229/2318 us)
```

用户可以执行命令`SHOW SPACES`查看创建的图空间。
Expand All @@ -146,7 +142,6 @@ Got 4 rows (time spent 1061/2251 us)
+--------------------+
| "basketballplayer" |
+--------------------+
Got 1 rows (time spent 977/2000 us)
```

## 创建Tag和Edge type
Expand Down Expand Up @@ -174,16 +169,12 @@ CREATE {TAG | EDGE} {<tag_name> | <edge_type>}(<property_name> <data_type>

```ngql
nebula> CREATE TAG player(name string, age int);
Execution succeeded (time spent 20708/22071 us)

nebula> CREATE TAG team(name string);
Execution succeeded (time spent 5643/6810 us)

nebula> CREATE EDGE follow(degree int);
Execution succeeded (time spent 12665/13934 us)

nebula> CREATE EDGE serve(start_year int, end_year int);
Execution succeeded (time spent 5858/6870 us)
```

## 插入点和边
Expand Down Expand Up @@ -219,32 +210,24 @@ Execution succeeded (time spent 5858/6870 us)

```ngql
nebula> INSERT VERTEX player(name, age) VALUES "player100":("Tim Duncan", 42);
Execution succeeded (time spent 28196/30896 us)

nebula> INSERT VERTEX player(name, age) VALUES "player101":("Tony Parker", 36);
Execution succeeded (time spent 2708/3834 us)

nebula> INSERT VERTEX player(name, age) VALUES "player102":("LaMarcus Aldridge", 33);
Execution succeeded (time spent 1945/3294 us)

nebula> INSERT VERTEX team(name) VALUES "team200":("Warriors"), "team201":("Nuggets");
Execution succeeded (time spent 2269/3310 us)
nebula> INSERT VERTEX team(name) VALUES "team203":("Trail Blazers"), "team204":("Spurs");
```

- 插入代表球员和球队之间关系的边。

```ngql
nebula> INSERT EDGE follow(degree) VALUES "player100" -> "player101":(95);
Execution succeeded (time spent 3362/4542 us)
nebula> INSERT EDGE follow(degree) VALUES "player101" -> "player100":(95);

nebula> INSERT EDGE follow(degree) VALUES "player100" -> "player102":(90);
Execution succeeded (time spent 2974/4274 us)
nebula> INSERT EDGE follow(degree) VALUES "player101" -> "player102":(90);

nebula> INSERT EDGE follow(degree) VALUES "player102" -> "player101":(75);
Execution succeeded (time spent 1891/3096 us)
nebula> INSERT EDGE follow(degree) VALUES "player102" -> "player100":(75);

nebula> INSERT EDGE serve(start_year, end_year) VALUES "player100" -> "team200":(1997, 2016), "player101" -> "team201":(1999, 2018);
Execution succeeded (time spent 6064/7104 us)
nebula> INSERT EDGE serve(start_year, end_year) VALUES "player101" -> "team204":(1999, 2018),"player102" -> "team203":(2006, 2015);
```

## 查询数据
Expand Down Expand Up @@ -305,30 +288,28 @@ Execution succeeded (time spent 5858/6870 us)

### `GO`语句示例

- 从VID为`player100`的球员开始,沿着边`follow`找到连接的球员。
- 从VID为`player101`的球员开始,沿着边`follow`找到连接的球员。

```ngql
nebula> GO FROM "player100" OVER follow;
nebula> GO FROM "player101" OVER follow;
+-------------+
| follow._dst |
+-------------+
| "player101" |
| "player125" |
| "player100" |
| "player102" |
+-------------+
Got 2 rows (time spent 12097/14220 us)
```

- 从VID为`player100`的球员开始,沿着边`follow`查找年龄大于或等于35岁的球员,并返回他们的姓名和年龄,同时重命名对应的列。
- 从VID为`player101`的球员开始,沿着边`follow`查找年龄大于或等于35岁的球员,并返回他们的姓名和年龄,同时重命名对应的列。

```ngql
nebula> GO FROM "player100" OVER follow WHERE properties($$).age >= 35 \
nebula> GO FROM "player101" OVER follow WHERE properties($$).age >= 35 \
YIELD properties($$).name AS Teammate, properties($$).age AS Age;
+-----------------+-----+
| Teammate | Age |
+-----------------+-----+
| "Tony Parker" | 36 |
| "Manu Ginobili" | 41 |
+-----------------+-----+
+--------------+-----+
| Teammate | Age |
+--------------+-----+
| "Tim Duncan" | 42 |
+--------------+-----+
```

|子句/符号|说明|
Expand All @@ -337,21 +318,19 @@ Execution succeeded (time spent 5858/6870 us)
|`$$`|表示边的终点。|
|`\`|表示换行继续输入。|

- 从VID为`player100`的球员开始,沿着边`follow`查找连接的球员,然后检索这些球员的球队。为了合并这两个查询请求,可以使用管道符或临时变量。
- 从VID为`player101`的球员开始,沿着边`follow`查找连接的球员,然后检索这些球员的球队。为了合并这两个查询请求,可以使用管道符或临时变量。

- 使用管道符

```ngql
nebula> GO FROM "player100" OVER follow YIELD dst(edge) AS id | \
nebula> GO FROM "player101" OVER follow YIELD dst(edge) AS id | \
GO FROM $-.id OVER serve YIELD properties($$).name AS Team, \
properties($^).name AS Player;
+-----------+-----------------+
| Team | Player |
+-----------+-----------------+
| "Spurs" | "Tony Parker" |
| "Hornets" | "Tony Parker" |
| "Spurs" | "Manu Ginobili" |
+-----------+-----------------+
+-----------------+---------------------+
| Team | Player |
+-----------------+---------------------+
| "Trail Blazers" | "LaMarcus Aldridge" |
+-----------------+---------------------+
```

|子句/符号|说明|
Expand All @@ -367,16 +346,14 @@ Execution succeeded (time spent 5858/6870 us)
当复合语句作为一个整体提交给服务器时,其中的临时变量会在语句结束时被释放。

```ngql
nebula> $var = GO FROM "player100" OVER follow YIELD dst(edge) AS id; \
nebula> $var = GO FROM "player101" OVER follow YIELD dst(edge) AS id; \
GO FROM $var.id OVER serve YIELD properties($$).name AS Team, \
properties($^).name AS Player;
+-----------+-----------------+
| Team | Player |
+-----------+-----------------+
| "Spurs" | "Tony Parker" |
| "Hornets" | "Tony Parker" |
| "Spurs" | "Manu Ginobili" |
+-----------+-----------------+
+-----------------+---------------------+
| Team | Player |
+-----------------+---------------------+
| "Trail Blazers" | "LaMarcus Aldridge" |
+-----------------+---------------------+
```

### `FETCH`语句示例
Expand Down Expand Up @@ -435,49 +412,41 @@ nebula> FETCH PROP ON player "player100";

```ngql
nebula> UPDATE VERTEX "player100" SET player.name = "Tim";
Execution succeeded (time spent 3483/3914 us)

nebula> FETCH PROP ON player "player100";
+---------------------------------------------+
| vertices_ |
+---------------------------------------------+
| ("player100" :player{age: 42, name: "Tim"}) |
+---------------------------------------------+
Got 1 rows (time spent 2463/3042 us)
```

- 用`UPDATE`修改某条边的`degree`属性,然后用`FETCH`检查结果。

```ngql
nebula> UPDATE EDGE "player100" -> "player101" OF follow SET degree = 96;
Execution succeeded (time spent 3932/4432 us)
nebula> UPDATE EDGE "player101" -> "player100" OF follow SET degree = 96;

nebula> FETCH PROP ON follow "player100" -> "player101";
nebula> FETCH PROP ON follow "player101" -> "player100";
+----------------------------------------------------+
| edges_ |
+----------------------------------------------------+
| [:follow "player100"->"player101" @0 {degree: 96}] |
| [:follow "player101"->"player100" @0 {degree: 96}] |
+----------------------------------------------------+
Got 1 rows (time spent 2205/2800 us)
```

- 用`INSERT`插入一个VID为`player111`的点,然后用`UPSERT`更新它。

```ngql
nebula> INSERT VERTEX player(name, age) VALUES "player111":("Ben Simmons", 22);
Execution succeeded (time spent 2115/2900 us)
nebula> INSERT VERTEX player(name,age) values "player111":("David West", 38);

Wed, 21 Oct 2020 11:11:50 UTC

nebula> UPSERT VERTEX "player111" SET player.name = "Dwight Howard", player.age = $^.player.age + 11 \
WHEN $^.player.name == "Ben Simmons" AND $^.player.age > 20 \
nebula> UPSERT VERTEX "player111" SET player.name = "David", player.age = $^.player.age + 11 \
WHEN $^.player.name == "David West" AND $^.player.age > 20 \
YIELD $^.player.name AS Name, $^.player.age AS Age;
+---------------+-----+
| Name | Age |
+---------------+-----+
| Dwight Howard | 33 |
+---------------+-----+
Got 1 rows (time spent 1815/2329 us)
+---------+-----+
| Name | Age |
+---------+-----+
| "David" | 49 |
+---------+-----+
```

## 删除点和边
Expand All @@ -502,15 +471,13 @@ nebula> FETCH PROP ON player "player100";
- 删除点

```ngql
nebula> DELETE VERTEX "team1", "team2";
Execution succeeded (time spent 4337/4782 us)
nebula> DELETE VERTEX "player111", "team203";
```

- 删除边

```ngql
nebula> DELETE EDGE follow "team1" -> "team2";
Execution succeeded (time spent 3700/4101 us)
nebula> DELETE EDGE follow "player101" -> "team204";
```

## 索引
Expand Down Expand Up @@ -538,15 +505,6 @@ nebula> FETCH PROP ON player "player100";
REBUILD {TAG | EDGE} INDEX <index_name>;
```

### 示例

为Tag`player`的属性`name`创建索引,并且重建索引。

```ngql
nebula> CREATE TAG INDEX player_index_0 on player(name(20));
nebula> REBUILD TAG INDEX player_index_0;
```

!!! Note

为没有指定长度的变量属性创建索引时,需要指定索引长度。在utf-8编码中,一个中文字符占3字节,请根据变量属性长度设置合适的索引长度。例如10个中文字符,索引长度需要为30。详情请参见[创建索引](../3.ngql-guide/14.native-index-statements/1.create-native-index.md#_6)。
Expand All @@ -558,27 +516,25 @@ nebula> REBUILD TAG INDEX player_index_0;
找到Tag为`player`的点的信息,它的`name`属性值为`Tony Parker`。

```nGQL
// 为name属性创建索引player_name_0。
nebula> CREATE TAG INDEX player_name_0 on player(name(10));
Execution succeeded (time spent 3465/4150 us)
// 为name属性创建索引player_index_1。
nebula> CREATE TAG INDEX player_index_1 ON player(name(20));

// 重建索引确保能对已存在数据生效。
nebula> REBUILD TAG INDEX player_name_0
nebula> REBUILD TAG INDEX player_index_1
+------------+
| New Job Id |
+------------+
| 31 |
+------------+
Got 1 rows (time spent 2379/3033 us)

// 使用LOOKUP语句检索点的属性。
nebula> LOOKUP ON player WHERE player.name == "Tony Parker" \
YIELD player.name, player.age;
+-------------+---------------+------------+
| VertexID | player.name | player.age |
+-------------+---------------+------------+
| "player101" | "Tony Parker" | 36 |
+-------------+---------------+------------+
YIELD properties(vertex).name AS name, properties(vertex).age AS age;
+-------------+---------------+-----+
| VertexID | name | age |
+-------------+---------------+-----+
| "player101" | "Tony Parker" | 36 |
+-------------+---------------+-----+

// 使用MATCH语句检索点的属性。
nebula> MATCH (v:player{name:"Tony Parker"}) RETURN v;
Expand All @@ -587,5 +543,4 @@ nebula> MATCH (v:player{name:"Tony Parker"}) RETURN v;
+-----------------------------------------------------+
| ("player101" :player{age: 36, name: "Tony Parker"}) |
+-----------------------------------------------------+
Got 1 rows (time spent 5132/6246 us)
```
Binary file added docs-2.0/2.quick-start/dataset-for-crud.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading