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
The easiest way to try nebula cluster is using docker compose. For details please refer nebula docker deployment.
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 (choose and follow).
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 test
:
nebula> CREATE SPACE test(partition_num=1024, replica_factor=1);
-- Use this space
nebula> USE test;
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 follow(likeness double);
nebula> CREATE EDGE choose(grade int);
To list the tags and edge types that we just created:
-- Show tag list
nebula> SHOW TAGS;
============
| Name |
============
| building |
------------
| course |
------------
| student |
------------
-- Show edge type list
nebula> SHOW EDGES;
==========
| Name |
==========
| choose |
----------
| follow |
----------
To show the attributes of a tag or an edge type:
-- Show attributes of a tag
nebula> DESCRIBE TAG student;
===================
| Field | Type |
===================
| name | string |
-------------------
| age | int |
-------------------
| gender | string |
-------------------
-- Show attributes of an edge type
nebula> DESCRIBE EDGE follow;
=====================
| Field | Type |
=====================
| likeness | double |
---------------------
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 choose(grade) VALUES 200 -> 101:(5);
nebula> INSERT EDGE choose(grade) VALUES 200 -> 102:(3);
nebula> INSERT EDGE choose(grade) VALUES 201 -> 102:(3);
nebula> INSERT EDGE choose(grade) VALUES 202 -> 102:(3);
nebula> INSERT EDGE follow(likeness) VALUES 200 -> 201:(92.5);
nebula> INSERT EDGE follow(likeness) VALUES 201 -> 200:(85.6);
nebula> INSERT EDGE follow(likeness) VALUES 201 -> 202:(93.2);
-- Update vertex
nebula> UPDATE VERTEX 101 SET course.credits = $^.course.credits + 1, building.name = "No8" YIELD $^.course.name AS Name, $^.course.credits AS Credits, $^.building.name
-- Update edge
nebula> UPDATE EDGE 200 -> 101 OF choose SET grade = choose.grade +1
Fetch the data updated.
nebula> FETCH PROP ON course 101;
================================
| course.name | course.credits |
================================
| Math | 4 |
--------------------------------
nebula> FETCH PROP ON choose 200 -> 101
================
| choose.grade |
================
| 6 |
----------------
Delete vertex.
nebula> DELETE VERTEX $vid
Q1. Find the vertexes that 201 follows:
nebula> GO FROM 201 OVER follow;
=============
| follow._dst |
=============
| 200 |
-------------
| 202 |
-------------
Q2. Find the vertexes that 201 follows, 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 follow 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 chosen courses and corresponding grades of students followed by 201.
-- By pipe
nebula> GO FROM 201 OVER follow yield follow._dst as id \
| GO FROM $-.id OVER choose YIELD $^.student.name AS Student,\
$$.course.name AS Course, choose.grade AS Grade;
=============================
| Student | Course | Grade |
=============================
| Monica | Math | 6 |
-----------------------------
| Monica | English | 3 |
-----------------------------
| Jane | English | 3 |
-----------------------------
-- By temporary variable
nebula> $var=GO FROM 201 OVER follow yield follow._dst as id; \
GO FROM $var.id OVER choose YIELD $^.student.name AS Student,\
$$.course.name AS Course, choose.grade AS Grade;
=============================
| Student | Course | Grade |
=============================
| Monica | Math | 6 |
-----------------------------
| Monica | English | 3 |
-----------------------------
| Jane | English | 3 |
-----------------------------
|
denotes a pipe. The output of the formal query acts as input to the next one follow 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.
In practice, it often takes several retries to find a proper graph schema during testing. You can put all the DDL statements in a file as follows.
DROP SPACE test; -- Delete useless space
CREATE SPACE test(partition_num=1024, replica_factor=1);
USE test;
CREATE TAG course(name string, credits int);
CREATE TAG building(name string);
CREATE TAG student(name string, age int, gender string);
CREATE EDGE like(likeness double);
CREATE EDGE select(grade int);
Then batch write to console
cat schema.ngql | ./bin/nebula -u user -p password
Likewise, you can put hundreds or thousands DML statements in a data.ngql to insert. If you have millions of records to insert, please use the csv importer tool (or sst ingest tool).