The easiest way to get Nebula Graph up and running is using Docker. Before you start, make sure that you have:
-
Installed the latest version of Docker
-
Pulled the latest images of Nebula from Nebula Docker Hub. If not, pull the images using the following command:
> docker pull vesoft/nebula-graph:nightly
When nebula
image is ready, run a container:
> docker run -it vesoft/nebula-graph:nightly /bin/bash
After login, you're in the root
directory and you should switch to the nebula directory
> cd /usr/local/nebula/
Start meta service, storage service and graph service:
> scripts/nebula.service start all
Check services' status:
> scripts/nebula.service status all
> bin/nebula -u=user -p=password
- -u is to set the user name,
user
is the default Nebula user account - -p is to set password,
password
is the default password for accountuser
Further more, if you run the console (bin/nebula
) in a remote host from graphd
, please specify the server's ip and port:
> bin/nebula -u=user -p=password -addr={graphd ip} --port={graphd port}
If you have any questions or concerns about the deployment procedures, please do not hesitate to open an issue on GitHub.
This section describes how to build a graph and run queries. The example is built on the graph below:
There are three kinds of tags (course, building and student) and two edge types (select and like).
First, we need to create a space and use it before other operations.
To list all existing spaces:
nebula> SHOW SPACES;
To create a new space named myspace_test2
:
nebula> CREATE SPACE myspace_test2 (partition_num=1024, replica_factor=1);
-- Use this space
nebula> USE myspace_test2;
replica_factor
specifies the number of replicas in the cluster.partition_num
specifies the number of partitions in one replica.
The CREATE TAG
statement defines a tag, with a type name and an attribute list.
nebula> CREATE TAG course (name string, credits int);
nebula> CREATE TAG building (name string);
nebula> CREATE TAG student (name string, age int, gender string);
The CREATE EDGE
statement defines an edge type.
nebula> CREATE EDGE like(likeness double);
nebula> CREATE EDGE select(grade int);
To list the tags and edge types that we just created:
-- Show tag list
nebula> SHOW TAGS;
-- Show edge type list
nebula> SHOW EDGES;
To show the attributes of a tag or an edge type:
-- Show attributes of a tag
nebula> DESCRIBE TAG student;
-- Show attributes of an edge type
nebula> DESCRIBE EDGE like;
Insert the vertices and edges based on the graph above.
-- Insert vertices
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);
Q1. Find the vertexes that 201 likes:
nebula> GO FROM 201 OVER like;
=============
| like._dst |
=============
| 200 |
-------------
| 202 |
-------------
Q2. Find the vertexes that 201 likes, whose ages are greater than 17. Return their name, age and gender, and alias the columns as Friend, Age and Gender, respectively.
nebula> GO FROM 201 OVER like WHERE $$.student.age >= 17 YIELD $$.student.name AS Friend, \
$$.student.age AS Age, $$.student.gender AS Gender;
=========================
| Friend | Age | Gender |
=========================
| Jane | 17 | female |
-------------------------
You can use backslash (\
) as line break;
YIELD
specifies what values or results you might want to return from query.$^
represents the source vertex.$$
indicates the target vertex.
Q3. Find the selected courses and corresponding grades of students liked by 201.
-- By pipe
nebula> GO FROM 201 OVER like yield like._dst as id \
| GO FROM $-.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 |
-----------------------------
-- By temporary variable
nebula> $var=GO FROM 201 OVER like yield like._dst as id; \
GO FROM $var.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 |
-----------------------------
|
denotes a pipe. The output of the formal query acts as input to the next one like a pipeline.$-
refers to the input stream.
The second approach adopts a user-defined variable $var
. The scope of this variable is within the compound statement.