1 本地安装(linux环境)

下载rpm包

wget https://oss-cdn.nebula-graph.com.cn/package/3.3.0/nebula-graph-3.3.0.el7.x86_64.rpm

安装rpm包

sudo rpm -ivh --prefix=<installation_path> <package_name>

--prefix为可选项,用于指定安装路径。如不设置,系统会将 NebulaGraph 安装到默认路径/usr/local/nebula/

例如,要在默认路径下安装3.3.0版本的 RPM 包,运行如下命令:

1
sudo rpm -ivh nebula-graph-3.3.0.el7.x86_64.rpm

启动NebulaGraph 服务

执行如下命令启动服务:

1
2
3
4
5
6
7
$ sudo /usr/local/nebula/scripts/nebula.service start all
[INFO] Starting nebula-metad...
[INFO] Done
[INFO] Starting nebula-graphd...
[INFO] Done
[INFO] Starting nebula-storaged...
[INFO] Done

连接NebulaGraph

首先需要下载NebulaGraph Console ,有两种方式安装nebula graph 的console:下载源码,进行编译;直接下载可执行二进制文件。由于公司服务器不支持访问外网,编译二进制文件会从github上下载一些文件,因此直接使用二进制文件进行连接

二进制下载地址: nebula-console-linux-amd64-v3.3.1

下载好后,直接执行如下命令进行连接

./nebula-console-linux-amd64-v3.3.1-addr <IP> -port <PORT> -u <USER> -p <PASSWORD>

示例:

./nebula-console-linux-amd64-v3.3.1-addr 127.0.0.1 -port 9669 -u root -p nebula

注册Storage服务

在注册Storage 主机之前,需要确保添加的主机 IP 和配置文件./etc/nebula-storaged.conflocal_ip配置的 IP 一致,否则会导致添加 Storage 主机失败。通过执行如下命令添加主机:

1
ADD HOSTS <ip>:<port> [,<ip>:<port> ...];

示例:

1
nebula> ADD HOSTS 192.168.10.100:9779, 192.168.10.101:9779, 192.168.10.102:9779;

检查主机状态,确认全部在线。

1
2
3
4
5
6
7
8
nebula> SHOW HOSTS;
+------------------+------+-----------+----------+--------------+---------------------- +------------------------+---------+
| Host | Port | HTTP port | Status | Leader count | Leader distribution | Partition distribution | Version |
+------------------+------+-----------+----------+--------------+---------------------- +------------------------+---------+
| "192.168.10.100" | 9779 | 19669 | "ONLINE" | 0 | "No valid partition" | "No valid partition" | "3.1.0" |
| "192.168.10.101" | 9779 | 19669 | "ONLINE" | 0 | "No valid partition" | "No valid partition" | "3.1.0" |
| "192.168.10.102" | 9779 | 19669 | "ONLINE" | 0 | "No valid partition" | "No valid partition" | "3.1.0" |
+------------------+------+-----------+----------+--------------+---------------------- +------------------------+---------+

这样Nebula Graph DB就安装完成啦~

2 搭建分布式集群

对于更常见的测试环境,例如三台机器构成的集群,用户可以按照如下方案部署 NebulaGraph。

机器名称 metad 进程数量 storaged 进程数量 graphd 进程数量
A 1 1 1
B - 1 1
C - 1 1

单机和分布式集群搭建方法区别在与配置文件的不同:

A机器需要修改etc文件夹下的nebula-graphd.confnebula-storaged.confnebula-metad.conf

B和C机器需要修改etc文件夹下的nebula-graphd.confnebula-storaged.conf

其中,–meta_server_addrs统一修改为: A机器IP:9559,而–local_ip修改为机器自身的IP。再在各个机器上分别输入下列命令启动:

机器A:./script/nebula.service start all

机器B:./script/nebula.service start graphd & ./script/nebula.service start storaged

机器C:./script/nebula.service start graphd & ./script/nebula.service start storaged

最后,在机器A上使用console命令行添加三台机器的hosts即可完成分布式集群的搭建。

3 NGQL指南

nGQL(NebulaGraph Query Language)是 NebulaGraph 使用的的声明式图查询语言,支持灵活高效的图模式

3.1 创建图空间

使用如下语句在分布式集群中创建图空间,partition_num:分片数量,建议选取硬盘数量的20倍,默认100;replica_factor:副本数量,由于投票原则,需要使用奇数,vid_type:指定点 ID 的数据类型,可选值为FIXED_STRING(<N>)INT64

1
nebula> create space internet_device(partition_num=60, replica_factor=3,vid_type=FIXED_STRING(30)) comment="网络设备图空间";

如果负载不均衡,可以使用如下语句进行均衡负载:

1
2
3
4
5
6
7
8
9
nebula> BALANCE LEADER;
nebula> SHOW HOSTS;
+-------------+------+-----------+----------+--------------+--------------------------------+--------------------------------+---------+
| Host | Port | HTTP port | Status | Leader count | Leader distribution | Partition distribution | Version |
+-------------+------+-----------+----------+--------------+--------------------------------+--------------------------------+---------+
| "storaged0" | 9779 | 19779 | "ONLINE" | 7 | "basketballplayer:3, test:4" | "basketballplayer:10, test:10" | "3.1.0" |
| "storaged1" | 9779 | 19779 | "ONLINE" | 7 | "basketballplayer:4, test:3" | "basketballplayer:10, test:10" | "3.1.0" |
| "storaged2" | 9779 | 19779 | "ONLINE" | 6 | "basketballplayer:3, test:3" | "basketballplayer:10, test:10" | "3.1.0" |
+-------------+------+-----------+----------+--------------+--------------------------------+--------------------------------+---------+

3.2 创建索引

nebula的索引与mysql里的索引不太一致,如果需要使用像mysql中的where语句,需要对字段添加索引。

在添加完索引之后,需要rebuild之后索引才会生效。由于添加索引之后会影响读写速度,建议先写入数据后再进行添加相应的索引。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
nebula> CREATE TAG IF NOT EXISTS person(name string, age int, gender string, email string);
nebula> CREATE TAG INDEX IF NOT EXISTS single_person_index ON person(name(10));

# 重建索引,返回任务 ID。
nebula> REBUILD TAG INDEX single_person_index;
+------------+
| New Job Id |
+------------+
| 31 |
+------------+

# 查看索引状态。
nebula> SHOW TAG INDEX STATUS;
+-----------------------+--------------+
| Name | Index Status |
+-----------------------+--------------+
| "single_person_index" | "FINISHED" |
+-----------------------+--------------+

# 也可以使用 SHOW JOB <job_id>查看重建索引的任务状态。
nebula> SHOW JOB 31;
+----------------+---------------------+------------+-------------------------+-------------------------+-------------+
| Job Id(TaskId) | Command(Dest) | Status | Start Time | Stop Time | Error Code |
+----------------+---------------------+------------+-------------------------+-------------------------+-------------+
| 31 | "REBUILD_TAG_INDEX" | "FINISHED" | 2021-07-07T09:04:24.000 | 2021-07-07T09:04:24.000 | "SUCCEEDED" |
| "Total:3" | "Succeeded:3" | "Failed:0" | "In Progress:0" | "" | "" |
+----------------+---------------------+------------+----------------------------+----------------------------+-------------+
+----------------+---------------------+------------+-------------------------+-------------------------+-------------+