From 4313369103a90b9a957f1c2b1252c836fe620163 Mon Sep 17 00:00:00 2001 From: jude-zhu <51590253+jude-zhu@users.noreply.github.com> Date: Mon, 8 Jul 2019 14:37:51 +0800 Subject: [PATCH] Added guide to build user's owner graph (#590) * get-started.md add build user owner graph * update * update * update * update * update * update * update --- docs/get-started.md | 254 ++++++++++++++++++++++++-------------------- docs/nGQL.md | 4 +- 2 files changed, 139 insertions(+), 119 deletions(-) diff --git a/docs/get-started.md b/docs/get-started.md index eec0e595d91..d8b5ae6585c 100644 --- a/docs/get-started.md +++ b/docs/get-started.md @@ -5,7 +5,7 @@ This tutorial provides a quick introduction to use Nebula Graph. --- -### Step 1 Install Nebula Graph +### Step 1: Install Nebula Graph The easiest way to startup Nebula is using Docker. Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. @@ -54,7 +54,7 @@ Or create the new file with `vi /etc/docker/daemon.json`, then add the following --- -### Step 2 Startup Nebula Graph +### Step 2: Startup Nebula Graph When `nebula` image is ready, run @@ -107,160 +107,180 @@ Welcome to Nebula Graph (Version 0.1) nebula> ``` -Before query the dataset, you should switch to an existing graph space. - -``` -nebula> use nba -Execution succeeded (Time spent: 154/793 us) +If you have any questions or concerns about the deployment procedures, please do not hesitate to open an issue on GitHub. + +### Step 3: Build Your Own Graph + +This section describes how to build a graph and run queries. The example is built on the graph below: + +![Untitled Diagram (1)](https://user-images.githubusercontent.com/51590253/60649144-0774c980-9e74-11e9-86d6-bad1653e70ba.png) + +There are three kinds of tags (_course_, _building_ and _team_) and two edge types (_select_ and _like_). The graph schema is: +```json +{ + "tags":{ + "course":[ + "name: string", + "credits: integer" + ], + "building":[ + "name: string" + ], + "student":[ + "name: string", + "age: integer", + "gender: string" + ] + }, + "edges":{ + "select":[ + "grade: integer" + ], + "like":[ + "likeness: double" + ] + } +} ``` ---- +#### Create a Graph Space -### Step 3 Simple Query Demo - -This query comes from a vertex walk up an edge. +**SPACE** is a region that provides physically isolation of graphs in Nebula. First we need to create a space and use it before other operations. +To list all existing spaces: ``` -nebula> GO FROM 5209979940224249985 OVER like -======================== -| id | -======================== -| -7187791973189815797 | ------------------------- -| 3778194419743477824 | ------------------------- -| -6952676908621237908 | ------------------------- -........................ +nebula> SHOW SPACES; ``` -This query comes from a vertex walk up an edge and the target's age should be greater than or equal to 30, also renames the columns as `Player`, `Friend` and `Age`. - -``` -nebula> GO FROM 5209979940224249985 OVER like WHERE $$[player].age >= 30 YIELD $^[player].name AS Player, $$[player].name AS Friend, $$[player].age AS Age -============================================= -| Player | Friend | Age | -============================================= -| Dejounte Murray | Kevin Durant | 30 | ---------------------------------------------- -| Dejounte Murray | Chris Paul | 33 | ---------------------------------------------- -| Dejounte Murray | LeBron James | 34 | ---------------------------------------------- -............................................. +To create a new space named _myspace_test2_ : ``` +nebula> CREATE SPACE myspace_test2(partition_num=1, replica_factor=1); -This query comes from a vertex walk up an edge and the target's age should be greater than or equal to 30. - -Setting output result as input, query `Player Name`, `FromYear`, `ToYear` and `Team Name` with input's `id`. - -In this query, `$^` is delegated the source vertex and `$$` is the target vertex. +-- Use this space +nebula> USE myspace_test2; +``` +`replica_factor` specifies the number of replicas in the cluster. -`$-` is used to indicate the input from pipeline. +`partition_num` specifies the number of partitions in one replica. -(reference to `regular expressions`, using the special ^ (hat) and $ (dollar sign) metacharacters to describe the start and the end of the line.) +#### Define Graph Schema +The `CREATE TAG` statement defines a tag, with a type name and an attribute list. ``` -nebula> GO FROM 5209979940224249985 OVER like WHERE $$[player].age >= 30 | GO FROM $-.id OVER serve YIELD $^[player].name AS Player, serve.start_year AS FromYear, serve.end_year AS ToYear, $$[team].name AS Team -===================================================== -| Player | FromYear | ToYear | Team | -===================================================== -| Kevin Durant | 2016 | 2019 | Warriors | ------------------------------------------------------ -| Kevin Durant | 2007 | 2016 | Thunders | ------------------------------------------------------ -| Chris Paul | 2017 | 2021 | Rockets | ------------------------------------------------------ -..................................................... +nebula> CREATE TAG course(name string, credits int); +nebula> CREATE TAG building(name string); +nebula> CREATE TAG student(name string, age int, gender string); ``` - -For more detail about Query Language, please see [Traverse The Graph](/nGQL/#traverse-the-graph). - ---- - -### Step 4 Advanced Usage - -Currently you can create your own graph space, such as : - +The `CREATE EDGE` statement defines an edge type. ``` -CREATE space myspace(partition_num=1, replica_factor=1) +nebula> CREATE EDGE like(likeness double); +nebula> CREATE EDGE select(grade int); ``` -When you start `Nebula Graph`, a set of vertices and edges have already existed. -Currently the default schema is `nba`. - -The graph space describes the relationship between players and teams. -In the graph space, there are two tags (player and team) and two edges (serve and like) which is composed of `string` and `integer`. +To list the tags and edge types that we just created: +``` +-- Show tag list +nebula> SHOW TAGS; -The Schema looks like : +-- Show edge type list +nebula> SHOW EDGES; +``` +To show the attributes of a tag or an edge type: ``` -Tag team: -================== -| Field | Type | -================== -| name | string | ------------------- +-- Show attributes of a tag +nebula> DESCRIBE TAG student; -Tag player: -================== -| Field | Type | -================== -| name | string | ------------------- -| age | int | ------------------- +-- Show attributes of an edge type +nebula> DESCRIBE EDGE like; +``` -Edge serve: -===================== -| Field | Type | -===================== -| start_year | int | ---------------------- -| end_year | int | ---------------------- +#### Insert Data -Edge like: -=================== -| Field | Type | -=================== -| likeness | int | -------------------- +Insert the vertexes and edges based on the graph above. +``` +-- Insert vertexes +nebula> INSERT VERTEX student(name, age, gender) VALUES 200:("Monica", 16, "female"); +nebula> INSERT VERTEX student(name, age, gender) VALUES 201:("Mike", 18, "male"); +nebula> INSERT VERTEX student(name, age, gender) VALUES 202:("Jane", 17, "female"); +nebula> INSERT VERTEX course(name, credits),building(name) VALUES 101:("Math", 3, "No5"); +nebula> INSERT VERTEX course(name, credits),building(name) VALUES 102:("English", 6, "No11"); +-- Insert edges +nebula> INSERT EDGE select(grade) VALUES 200 -> 101:(5); +nebula> INSERT EDGE select(grade) VALUES 200 -> 102:(3); +nebula> INSERT EDGE select(grade) VALUES 201 -> 102:(3); +nebula> INSERT EDGE select(grade) VALUES 202 -> 102:(3); +nebula> INSERT EDGE like(likeness) VALUES 200 -> 201:(92.5); +nebula> INSERT EDGE like(likeness) VALUES 201 -> 200:(85.6); +nebula> INSERT EDGE like(likeness) VALUES 201 -> 202:(93.2); ``` -You can create both tag and edge's schema: +#### Sample Queries +Q1. Find the vertexes that 201 likes: ``` -// create tags schema: players and teams: -CREATE TAG player(name string, age int) +nebula> GO FROM 201 OVER like; -CREATE TAG team(name string) +======= +| id | +======= +| 200 | +------- +| 202 | +------- +``` +Q2. Find the vertexes that 201 likes, whose age are greater than 17. Return their name, age and gender, and alias the columns as Friend, Age and Gender, respectively. -// create edges schema: players and teams: -CREATE EDGE like(likeness int) +``` +nebula> GO FROM 201 OVER like WHERE $$[student].age >= 17 YIELD $$[student].name AS Friend, $$[student].age AS Age, $$[student].gender AS Gender; -CREATE EDGE serve(start_year int, end_year int) +========================= +| Friend | Age | Gender | +========================= +| Jane | 17 | female | +------------------------- ``` +`YIELD` specifies what values or results you might want to return from query. -You could describe the schema created, for example: +`$^` represents the source vertex. -``` -DESCRIBE TAG player +`$$` indicates the target vertex. + +Q3. Find the courses that the vetexes liked by 201 select and their grade. -DESCRIBE EDGE serve ``` +-- By pipe +nebula> GO FROM 201 OVER like | GO FROM $-.id OVER select YIELD $^[student].name AS Student, $$[course].name AS Course, select.grade AS Grade; -The insert sentences look like the following commands. +============================= +| Student | Course | Grade | +============================= +| Monica | Math | 5 | +----------------------------- +| Monica | English | 3 | +----------------------------- +| Jane | English | 3 | +----------------------------- +-- By temporary variable +nebula> $a=GO FROM 201 OVER like; GO FROM $a.id OVER select YIELD $^[student].name AS Student, $$[course].name AS Course, select.grade AS Grade; + +============================= +| Student | Course | Grade | +============================= +| Monica | Math | 5 | +----------------------------- +| Monica | English | 3 | +----------------------------- +| Jane | English | 3 | +----------------------------- ``` -// Insert some vertices: players and teams: -INSERT VERTEX player(name, age) VALUES -8379929135833483044:("Amar'e Stoudemire", 36) -INSERT VERTEX team(name) VALUES -9110170398241263635:("Magic") +`|` denotes a pipe. The output of the formal query acts as input to the next one like a pipeline. -// Insert some edges: likes and serves: -INSERT EDGE like(likeness) VALUES -8379929135833483044 -> 6663720087669302163:(90) +`$-` refers to the input stream. -INSERT EDGE serve(start_year, end_year) VALUES -8379929135833483044 -> 868103967282670864:(2002, 2010) -``` +The second approach adopts a user-defined variable `$a`. The scope of this variable is within the compound statement. + +For more details about Query Language, check [nGQL](https://github.com/vesoft-inc/nebula/blob/master/docs/nGQL.md). \ No newline at end of file diff --git a/docs/nGQL.md b/docs/nGQL.md index 0235d59a15e..f10d188b47f 100644 --- a/docs/nGQL.md +++ b/docs/nGQL.md @@ -245,9 +245,9 @@ It's common to refer a property in the statement, such as in - always starts with "$". There are two special variables: $_ and $$. + always starts with "$". There are two special variables: $- and $$. -$_ refers to the input stream, while $$ refers to the destination objects +$- refers to the input stream, while $$ refers to the destination objects All property names start with a letter. There are a few system property names starting with "\_". All properties names starting with "\_" are reserved.