nebula小记

为什么要用GraphDB

1、图数据库是专门用于存储和检索庞大信息网的存储引擎,能够高效将数据存储为点和边,并允许对这些点边结构进行高性能的检索。

2、关系型数据库在处理复杂数据关联运算上表现很差,数据库建模复杂,数据量大时有性能问题,多度关系查询开销大。

3、图数据库相对关系型数据库的优点:建模简单直观,复杂查询简单,支持图算法和图数据可视化。

4、Nebula Graph是一款开源的、分布式的、易扩展的图数据库,能够承载千亿个顶点和万亿条边的超大规模数据集,并提供毫秒级查询。

5、目前许多大型互联网企业都在使用Nebula,官网也提供了相关使用文档,查阅即可,下文只是简单笔记。

nGQL命令

nGQL是NebulaGraph使用的的声明式图查询语言,可以对图数据进行修改/遍历/聚合等操作。

nebula实例由一个或多个图空间组成,图空间的概念类似mysql中的database,可以在不同的图空间存储不同的数据集,create space可以创建新的图空间。

典型风控应用场景,可以用来查询不同介质(各种设备id,ip,银行卡等)关联的用户数,也可以利用图关联检测出欺诈团伙。

-- 创建space
-- 同一个空间vid唯一
CREATE SPACE `lhq_test` (partition_num = 30, replica_factor = 1, vid_type = FIXED_STRING(100))

-- 创建点,tag相当于vid类型,比如vid为身份证号,tag为滴滴司机和老板两个,则有两组属性:驾驶员信息和老板信息
-- 相同vid相同tag,两条insert,后写入的覆盖先写入的
-- 相同vid不同tag,对tag a操作不影响tag b操作
create tag uid(report_timestamp timestamp,report_time string) ttl_duration=86400, ttl_col = "report_timestamp";
create tag deviceId(report_timestamp timestamp,report_time string) ttl_duration=86400, ttl_col = "report_timestamp";
create tag 其它介质id(report_timestamp timestamp,report_time string) ttl_duration=86400, ttl_col = "report_timestamp";

-- 创建边
create edge deviceId_edge(report_timestamp timestamp,report_time string,attribute1 int,attribute2 int) ttl_duration= 86400,ttl_col = "report_timestamp";

create edge 其它介质id(report_timestamp timestamp,report_time string,attribute1 int,attribute2 int) ttl_duration= 86400,ttl_col = "report_timestamp";



-- 插入点
insert vertex uid (report_time) values "uid值": ("时间戳字符串");

-- 插入边,$表示引用变量
insert edge deviceId_edge (report_timestamp, report_time, attribute1, attribute2) values "uid值" -> "deviceId值": ($report_timestamp, "$report_time", $attribute1, $attribute2)
insert edge 其它介质id_edge (report_timestamp, report_time) values "uid值" -> "其它介质id值": ($report_timestamp, "$report_time", $attribute1, $attribute2)

-- go查询
-- 通过介质id查询关联有多少个用户。默认1跳,可省略1 step
-- _src为边的起始点,_dst为边的目的点,_type正为正向边,负为逆向边
-- 新版本支持为src(edge), dst(edge), type(edge)
go 1 step from '$deviceId' over deviceId_edge bidirect where deviceId_edge._dst <> '0'
yield deviceId_edge._dst as target_uid
union
go 1 step from '$其它介质id' over 其它介质id_edge bidirect where 其它介质id_edge._dst <> '0'
yield 其它介质id_edge._dst as target_uid
union
...
return count(*) as related_uid_cnt

-- 管道符|可以组合多个查询,将前一个查询的结果集用于后一个查询
-- $-表示管道前面查询输出的结果集
-- $^表示边的起点,$$表示边的终点
-- 可以用$blabla = 定义临时变量,在下一个语句$blabla.id中访问id或其他属性



posted @ 2024-01-06 21:06  PilgrimHui  阅读(3)  评论(0编辑  收藏  举报