4000-520-616
欢迎来到免疫在线!(蚂蚁淘生物旗下平台)  请登录 |  免费注册 |  询价篮
主营:原厂直采,平行进口,授权代理(蚂蚁淘为您服务)
咨询热线电话
4000-520-616
当前位置: 首页 > 新闻动态 >
新闻详情
cayley基本使用_qq_41875308的博客-CSDN博客_cayley
来自 : CSDN技术社区 发布时间:2021-03-24
1. 概述2. 基本概念3. 环境搭建 1. 安装mongodb2. 安装go环境3. 安装cayley4. 基于Gremlin的增、删、查

1. 概述

Cayley是Google开源的图数据库 受到Freebase和Google知识图谱 Google s Knowledge Graph 的启发 其目标是成为开发者在Linked Data和图数据 语义网络、社交网络等 的工具之一。

编程语言 Go运行简单 只需 3 到 4 个命令RESTful API REPL(Read Eval Print Loop)内建查询编辑器和可视化界面支持多种查询语言 Gizmo - a JavaScript, with a Gremlin-inspired* graph object.GraphQL-inspired* query language.(simplified) MQL, for Freebase fans支持多种后端存储 KVs: Bolt, LevelDBNoSQL: MongoDBSQL: PostgreSQL, CockroachDB, MySQLIn-memory, ephemeral模块化设计 易于扩展对新语言和新后端的支持良好的测试覆盖率性能好

初步性能测试表明 在普通PC硬件和硬盘上 LevelDB跑1.34亿三元组毫无问题 多跳交叉查询 比如由X和Y两位影星同时出演的电影 只需150ms。

图数据库是一种NoSQL数据库 是基于节点、边和属性的图论 适合处理大量复杂、互连接、低结构化的数据。它更擅长描述数据之间的关系 因此被大量用于社交网络、推荐系统等专注于构建关系图谱的系统。

2. 基本概念n-quad

quads/triples是cayley中的一行 描述了起点、关系和终点 可以想象成是两个节点用一条边连接了起来。

例如 cayley内置的测试数据data/testdata.nq内容如下

 alice follows bob . bob follows fred . bob status cool_person . charlie follows bob . charlie follows dani . dani follows bob . dani follows greg . dani status cool_person . emily follows fred . fred follows greg . greg status cool_person . predicates are follows . predicates are status . emily status smart_person smart_graph . greg status smart_person smart_graph .

每一行是一个Triple 每个Triple由四部分组成 依次对应数据用空格分隔的每项 分别叫Subject Predicate Object Provenance 对应中文里的主语、谓语、宾语、来源。Subject转换成有向图中的顶点 Object是出度的顶点 Predicate是路径 Provenance是指可以在一个数据库里存多个图 用其来区分不同的图。

上述关系可以用下图来表示

\"image\"

其中箭头指明了follows的关系 而#括起来的人名表示 这些人有status为cool_person or smart_person。

triple 基于内存的TripleStore数据结构
type Triple struct { Subject string Predicate string Object string  Provenance string}
type TripleStore struct { idCounter int64 //idMap的长度  tripleIdCounter int64 //tripleId的序列 idMap map[string]int64 //存储三元组的内容- tripleId的对应关系 revIdMap map[int64]string //存储三元组的tripleId- 内容的对应关系idMap的反向映射关系 triples []graph.Triple //存储每条记录的关系三元组 size int64 //triples的数量 index TripleDirectionIndex //triples的索引 每个idMap中的一个key有一个平衡二叉树 里面放了tripleId}
type TripleDirectionIndex struct { subject map[int64]*llrb.LLRB  predicate map[int64]*llrb.LLRB object map[int64]*llrb.LLRB provenance map[int64]*llrb.LLRB}
neo4j vs cayley

neo4j根据节点和边存储对象 取某个属性值时类似于取表中的记录。cayley中所有属性都被当成了节点 只是简单存了表示各属性之间关系的triple 本质是字符串 。

\"image\"

比如要找user123的height时 neo4j是找到id为123的person 拉取包含这个person属性的节点 然后获得其height值。cayley则是找到id为123的person节点 通过height关系直接找到其height值。

3. 环境搭建1. 安装mongodb从mongodb官网下载mongodb-linux-x86_64-rhel70-3.4.10.tgz并解压
tar -zxvf mongodb-linux-x86_64-rhel70-3.4.10.tgz -C /opt/mongodb
创建data和log目录
mkdir -p /opt/mongodb/data/dbmkdir -p /opt/mongodb/log
修改配置文件
# 日志文件位置logpath /opt/mongodb/log/mongodb.log# 以追加方式写入日志logappend true# 是否以守护进程方式运行fork true# 默认27017port 27017# 数据库文件位置dbpath /opt/mongodb/data/db# 启用定期记录CPU利用率和 I/O 等待# cpu true# 是否以安全认证方式运行 默认是不认证的非安全方式# noauth true# auth true# 详细记录输出# verbose true# Inspect all client data for validity on receipt (useful for# developing drivers)用于开发驱动程序时验证客户端请求# objcheck true# Enable db quota management# 启用数据库配额管理# quota true# 设置oplog记录等级# Set oplogging level where n is# 0 off (default)# 1 W# 2 R# 3 both# 7 W some reads# diaglog 0# Diagnostic/debugging option 动态调试项# nocursors true# Ignore query hints 忽略查询提示# nohints true# 禁用http界面 默认为localhost 28017# nohttpinterface true# 关闭服务器端脚本 这将极大的限制功能# Turns off server-side scripting. This will result in greatly limited# functionality# noscripting true# 关闭扫描表 任何查询将会是扫描失败# Turns off table scans. Any query that would do a table scan fails.# notablescan true# 关闭数据文件预分配# Disable data file preallocation.# noprealloc true# 为新数据库指定.ns文件的大小 单位:MB# Specify .ns file size for new databases.# nssize # Replication Options 复制选项# in replicated mongo databases, specify the replica set name here# replSet setname# maximum size in megabytes for replication operation log# oplogSize 1024# path to a key file storing authentication info for connections# between replica set members# 指定存储身份验证信息的密钥文件的路径# keyFile /path/to/keyfile
运行mongod启动/关闭服务
cd /opt/mongodb./bin/mongod -f /opt/mongodb/mongodb.conf./bin/mongod --shutdown -f /opt/mongodb/mongodb.conf
查看服务进程
lsof -i :27017
添加环境变量并测试
export MONGODB_PATH /opt/mongodbsource /etc/profilemongo show dbs exit
2. 安装go环境从官网下载稳定版本解压 将其配置到环境变
tar -zxvf go1.8.1.linux-amd64.tar.gz -C /usr/local/export GOPATH /usr/local/goexport PATH $JAVA_HOME/bin:$MONGODB_PATH/bin:$GOPATH/bin:$PATHsource /etc/profile
3. 安装cayley从cayley官网下载v.0.6.1的AMD64位linux版本 并解压
tar -zxvf cayley_v0.6.1_linux_amd64.tar.gz -C /opt/cayley
拷贝cayley.cfg.example并修改配置
cp cayley.cfg.example /etc/cayley.cfgvim /etc/cayley.cfg database : mongo , db_path : 192.168.234.134:27017 , read_only : false, listen_host : 192.168.234.134 , listen_port : 64100 }
导入测试数据
cd /opt/cayley/cayley_v0.6.1_linux_amd64./cayley load --quads data/testdata.nq --alsologtostderr true./cayley load --quads data/30kmoviedata.nq.gz --alsologtostderr true
在mongodb中查看
 show dbsadmin 0.000GBcayley 0.046GBlocal 0.000GB use cayleyswitched to db cayley show collectionsnodesquads exitbye
启动webUI
./cayley http --config /etc/cayley.cfg# curl http://192.168.234.134:64100/ through browser
启动REPL命令行并测试
./cayley repl --config /etc/cayley.cfg# See who dani follows.cayley graph.Vertex( dani ).Out( follows ).All()
4. 基于Gremlin的增、删、查Add(:a subject predicate object label .)
cayley :a leon follows alice .cayley :a leon status cool_person .
Delete(:d subject predicate object .)
cayley :d leon follows alice .

Select

graph.Vertex([nodeId],[nodeId]…) or g.V
# 查看目前图中所有的顶点cayley g.V().All() # 获得bob这个节点cayley g.V( bob ).GetLimit(1)
path.Out([predicatePath],[tags])
# 查看charlie follows了谁 结果是 bob and danicayley g.V( charlie ).Out( follows ).All()# 查看alice follows的人 他们又follows了谁 结果是 fred cayley g.V( alice ).Out( follows ).Out( follows ).All()# 从dani出去的路径都指向了哪里 结果是 bob, greg 和 cool_personcayley g.V( dani ).Out().All()# 找到所有dani通过follows和status指向的节点 结果是 bob, greg 和 cool_personcayley g.V( dani ).Out([ follows , status ]).All()# 找到所有dani通过status指向的节点 并加上tagcayley g.V( dani ).Out(g.V( status ), pred ).All()
path.Is(node,[node..])
# 从图中所有节点出发 找到follows指向bob的路径 结果显示三个路径指向bob (来自 alice, charlie and dani)cayley g.V().Is( bob ).In( follows ).All()
path.Has(predicate,object)
# 从所有节点开始 找到谁follows了 结果是 alice, charlie and danicayley g.V().Has( follows , bob ).All()# follows charlie的人之中 哪些人follows了fred 结果是 bobcayley g.V( charlie ).Out( follows ).Has( follows , fred ).All()
path.Tag(tag) or path.As
# 从所有节点开始 把他们保存到“start”中 找到所有有status的predicate 并返回结果cayley g.V().Tag( start ).Out( status ).All()
path.Back(tag)
# 从所有节点出发 把它们保存到“start”中 找到有status的predicate的连接 然后跳回到“start”中 看看有谁follows了他们 返回结果cayley g.V().Tag( start ).Out( status ).Back( start ).In( follows ).All()
path.Save(predicate,tag)
# 从 dani 和 bob 开始 查看他们follows了谁 并把结果保存在“target”中cayley g.V( dani , bob ).Save( follows , target ).All()
path.Intersect(query) or path.Andpath.Union(query) or path.Orpath.Except(query) or path.Difference
# 取交集的数据cayley var cFollows g.V( charlie ).Out( follows )cayley var dFollows g.V( dani ).Out( follows )cayley cFollows.Intersect(dFollows).All()cayley g.V( charlie ).Out( follows ).And(g.V( dani ).Out( follows )).All()# 取并集的数据cayley cFollows.Union(dFollows).All()cayley g.V( charlie ).Out( follows ).Or(g.V( dani ).Out( follows )).All()# 取差集的数据cayley cFollows.Except(dFollows).All()cayley g.V( charlie ).Out( follows ).Difference(g.V( dani ).Out( follows )).All()
path.Follow(morphism)path.FollowR(morphism)
# 定义一个morphism为 从给定节点出发 它follows的节点所follows的节点cayley friendOfFriend g.Morphism().Out( follows ).Out( follows )# 查找charlie follows的人所follows的人里 谁的有status为cool_person 结果为 bob 和 gregcayley g.V( charlie ).Follow(friendOfFriend).Has( status , cool_person ).All()# 从所有节点出发 找到谁follows的人里 follows了status为cool_person的人 结果是emily bob charlie 来自 bob charlie 来自greg cayley g.V().Has( status , cool_person ).FollowR(friendOfFriend).All()

本文链接: http://cayley.immuno-online.com/view-699580.html

发布于 : 2021-03-24 阅读(0)
品牌分类
其他
联络我们
服务热线:4000-520-616
(限工作日9:00-18:00)
QQ :1570468124
手机:18915418616
官网:http://