NebulaGraph快速入门

2022-06-17 00:00:00 索引 语句 节点 查看 顶点

NebulaGraph是图数据库的一种,不同于mysql类数据库的表关系,整个space内分为顶点(tag)和边(edge),构成一种图的网状关系,每个顶点都有一个全局的顶点id(vid)。
具体帮助文档:https://docs.nebula-graph.com.cn/

现进行具体说明:

一、库级操作语句
1、查看所有库:
show spaces;
1
2、查看库信息:
describe space sqlLineage;
1
二、tag语句-》确定顶点的类型和承载信息
1、查看所有tag:
show tags;
1
2、创建tag:
变长类型为string,定长类型为fixed_string(),布尔值为bool,日期为timestamp,可以设置默认值,如日期可以设置默认函数为now()、 timestamp(),当利用该tag插入顶点时,不对该属性进行传参,则会设置默认值为当前时间。

create tag phone(name string,color fixed_string(5),size double,isSell bool default false,ct timestamp default timestamp());
1
3、删除tag:
注意,带有索引的tag不能被删除,否则会报出:confict!

drop tag phone;
1
4、查看单个tag信息:
describe tag phone;
1
三、edge type语句-》确定边的类型和承载信息
1、查看所有edge type
show edges;
1
2、创建edge type
create edge flow1(degree int);
1
3、删除edge type
注意,带有索引的edge type不能被删除,否则会报出:confict!

drop edge flow1;
1
4、查看单个edge type信息:
describe edge flow1;
1
四、顶点语句
1、插入顶点:
插入顶点时,必须指出顶点的vid,这里为hash(“iphone”)

insert vertex phone(name,color,size) values hash("iphone"):("iphone","black",6.8);
1
2、删除顶点:
delete vertex hash("iphone")
1
五、边语句
1、插入边
插入边时,必须指出边是由哪个顶点到哪个顶点的,也是通过对应顶点的vid,这里是hash(“iphone”)顶点到hash(“xiaomi”)顶点,属性为degree。

insert edge flow1(degree) values hash("iphone")->hash("xiaomi"):(1);
1
2、删除边
delete edge flow2 hash("iphone")->hash("xiaomi");
1
六、无索引查询语句
1、查看单个顶点信息
fetch prop on phone hash("iphone");
1
2、查看单个边信息
fetch prop on flow1 hash("iphone")->hash("xiaomi")
1
3、跳跃查询
下面例子表示,从vid为hash(“iphone”)的这个顶点,按照flow1这个边,查询跳跃1次到跳跃10次所有符合条件的顶点,并把该顶点的vid信息进行输出。

go 1 to 10 steps from hash("iphone") over flow1;
1
可以对该语句进行修改,下面例子,yield可以控制输出的内容,$ 表 示 的 是 对 应 边 的 下 一 个 节 点 , 还 可 以 用 表示的是对应边的下一个节点,还可以用 表示的是对应边的下一个节点,还可以用^,表示对应边的上一个节点,找到节点后,将节点上的phone的name属性取别名nm,然后进行输出。

go 1 to 10 steps from hash("iphone") over flow1 yield $$.phone.name as nm;
1
如下面例子,可以对边进行条件限定

go 1 to 10 steps from hash("iphone") over flow1 where flow1.degree >3 yield $$.phone.name as nm;
1
如下面例子,可以对顶点(这个是下位顶点)进行条件限定,并且还额外做了输出名的去重工作,并且对输出的size进行了数据类型强制转换。

go 1 to 10 steps from hash("iphone") over flow1 where $$.phone.size > 6 yield distinct($$.phone.name) as nm,toInteger($$.phone.size) as size;
1
如下面例子,引入管道符号,$-指代管道符号前的语句输出的起了别名的节点,先按照size进行排序,然后取前3个。

go 1 to 10 step from hash("iphone") over flow1 where $$.phone.size >6 yield $$.phone.name as name,toInteger($$.phone.size) as size,flow1.degree as degree| order by $-.size |limit 3;
1
七、索引语句
索引分为顶点索引和边索引:

1、查看当前space下的所有索引
(1)顶点索引
show tag indexes;
1
(2)边索引
show edge indexes;
1
2、创建索引
(1)顶点(边)全字段索引
create tag index index1 on phone();
1
(2)顶点(边)单字段索引
create tag index index2 on phone(name(10));
1
(3)顶点(边)复合字段索引
create tag index index3 on phone(name(10),size);
1
注意变长的字段在创建索引时,必须要制定长度,如上的name(10)

3、查看索引创建语句
show create tag index index3;
1
4、查看索引属性
describe tag index index2;
1
5、删除索引
drop tag index index3;
1
八、有索引查询语句
需要在插入数据前,对相应的tag(edge)建立索引,才能执行如下语句

1、按条件批量查询顶点(边)–lookup
lookup on phone where phone.size == 6.8
1
2、按条件批量查询顶点(边)–match
match (p:phone) where p.size > 6.0 return p;
————————————————
版权声明:本文为CSDN博主「樱花树下的猫老师」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u011798348/article/details/122261778

相关文章