博客链接:https://xiuxiuing.gitee.io/blog/2018/11/14/jgstart/
本节将使用Gods图作为演示示例,此图在JanusGraph演示中广泛使用。该图如下图所示。这个抽象的数据模型对应图模型中的属性,这个特定的实例描述了罗马万神殿中人物和地点之间的关系。此外,图中的特殊文本和符号修饰符(如:粗体、下划线等)表示图中不同的示意图/类型。

标示 含义
加粗的key 图中的索引键
加粗带星的key 图中的索引键值必须是唯一的
带下划线的key 以顶点为中心的索引键
空心箭头的边 特定的边(不能重复)
尾部十字的边 单项边(只能在一个方向建立关系)

下载Janusgraph并运行Gremlin Console

JanusGraph可以从项目仓库的Releases分支下载。下载完成后解压文件夹就可以启动Gremlin Console。Gremlin Console是在JanusGraph包中自带的一个REPL(即交互式的shell),这跟标准的Gremlin Console有所不同,因为它是JanusGraph预安装和预加载的包。或者,用户可以从中心仓库下载JanusGraph包然后使用已有Gremlin Console来安装和激活JanusGraph。在下面的示例中,将使用janusgraph的压缩包,但是,请务必记得先解压下载的zip包。

注意:JanusGraph依赖Java 8(标准版)。 建议使用Oracle Java 8。 JanusGraph的shell脚本执行需要配置$ JAVA_HOME环境变量,它指向JRE或JDK的安装目录。

$ unzip janusgraph-0.3.1-hadoop2.zip
Archive:  janusgraph-0.3.1-hadoop2.zipcreating: janusgraph-0.3.1-hadoop2/
...
$ cd janusgraph-0.3.1-hadoop2
$ bin/gremlin.sh\,,,/(o o)
-----oOOo-(3)-oOOo-----
09:12:24 INFO  org.apache.tinkerpop.gremlin.hadoop.structure.HadoopGraph  - HADOOP_GREMLIN_LIBS is set to: /usr/local/janusgraph/lib
plugin activated: tinkerpop.hadoop
plugin activated: janusgraph.imports
gremlin>

Gremlin Console使用Apache Groovy解释执行命令。Groovy构建在Java语言之上,具有很多简洁的语法,使交互式编程变得更加容易。同样的,Gremlin-Groovy也是构建在Groovy之上,具有简洁的语法,使得图的遍历变得容易。下面的例子将展示如何处理numbers、strings和maps。本教程的其他部分将讨论如何构建特定的图。

gremlin> 100-10
==>90
gremlin> "JanusGraph:" + " The Rise of Big Graph Data"
==>JanusGraph: The Rise of Big Graph Data
gremlin> [name:'aurelius', vocation:['philosopher', 'emperor']]
==>name=aurelius
==>vocation=[philosopher, emperor]

提醒:有关Gremlin更多使用方式请参考:Apache TinkerPop、SQL2Gremlin和Gremlin Recipes。

加载Gods图到JanusGraph中

下面的示例将建立一个JanusGraph图实例并加载上图所示的Gods图数据集。JanusGraphFactory提供了一组静态方法,通过配置文件作为参数来获得graph实例。本教程将使用一份使用BerkeleyDB作数据库,Elasticsearch作索引的配置文件来获取graph实例,并使用辅助类GraphOfTheGodsFactory来加载Gods图。 本节将暂时跳过这些详细的配置信息,但有关数据库,索引及其他的配置信息,请参见第III部分“数据库”,第IV部分,“索引”和第15章“配置参考”。

gremlin> graph = JanusGraphFactory.open('conf/janusgraph-berkeleyje-es.properties')
==>standardjanusgraph[berkeleyje:../db/berkeley]
gremlin> GraphOfTheGodsFactory.load(graph)
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[standardjanusgraph[berkeleyje:../db/berkeley], standard]

JanusGraphFactory.open()GraphOfTheGodsFactory.load()方法构造图的具体操作如下:

  1. 在图中创建全局的以顶点为中心的索引集。
  2. 添加所有的顶点及其属性到图中。
  3. 添加所有的边及其属性到图中。

更多细节请参考GraphOfTheGodsFactory的源码。

对于使用JanusGraph / Cassandra(或JanusGraph / HBase)的用户,请使用conf / janusgraph-cql-es.properties(或conf / janusgraph-hbase-es.properties)相关配置和GraphOfTheGodsFactory.load()

gremlin> graph = JanusGraphFactory.open('conf/janusgraph-cql-es.properties')
==>standardjanusgraph[cql:[127.0.0.1]]
gremlin> GraphOfTheGodsFactory.load(graph)
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[standardjanusgraph[cql:[127.0.0.1]], standard]

您也可以使用conf / janusgraph-cql.properties、conf / janusgraph-berkeleyje.properties或者conf / janusgraph-hbase.properties配置文件来获取graph实例,而不配置索引。 在这种情况下,你需要使用GraphOfTheGodsFactory.loadWithoutMixedIndex方法来加载Gods图,这样它将不会创建索引。

gremlin> graph = JanusGraphFactory.open('conf/janusgraph-cql.properties')
==>standardjanusgraph[cql:[127.0.0.1]]
gremlin> GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true)
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[standardjanusgraph[cql:[127.0.0.1]], standard]

全局图索引

使用图数据库中数据的典型模式是首先使用图索引来确定起始点。该起始点是一个元素(或一组元素) - 即顶点或边。从起始点,Gremlin路径描述描述了如何通过显示的图结构来遍历图中的其他点。

通过name属性上的唯一索引,可以检索到Saturn顶点,然后可以查到它的所有属性值(即Saturn属性的键值对)。如图所示,顶点Saturn的名字是“Saturn”,年龄是10000,类别是“titan”。 Saturn的孙子通过遍历语句可以表示为:“谁是Saturn的孙子?” (“父亲”的反面是“孩子”)。 检索到的结果是Hercules。

gremlin> saturn = g.V().has('name', 'saturn').next()
==>v[256]
gremlin> g.V(saturn).valueMap()
==>[name:[saturn], age:[10000]]
gremlin> g.V(saturn).in('father').in('father').values('name')
==>hercules

属性place也在图索引中。属性place是边的一个属性。因此JanusGraph可以通过图的索引索引到边。它可以查询Gods图中发生在Athens(纬度:37.97, 经度:23.72) 50km范围内的信息。然后通过这些信息查看涉及到哪些顶点。

gremlin> g.E().has('place', geoWithin(Geoshape.circle(37.97, 23.72, 50)))
==>e[a9x-co8-9hx-39s][16424-battled->4240]
==>e[9vp-co8-9hx-9ns][16424-battled->12520]
gremlin> g.E().has('place', geoWithin(Geoshape.circle(37.97, 23.72, 50))).as('source').inV().as('god2').select('source').outV().as('god1').select('god1', 'god2').by('name')
==>[god1:hercules, god2:hydra]
==>[god1:hercules, god2:nemean]

图索引是JanusGraph中的一种索引结构。JanusGraph会自动使用索引来检索满足一个或多个约束条件的所有顶点(g.V)或边(g.E)。JanusGraph中另外一种索引是以顶点为中心的索引。以顶点为中心的索引可以加快图的遍历。以顶点为中心的索引将在后续中讲解。

图遍历示例

Hercules是Jupiter和Alcmene的儿子,拥有超能力。Hercules是一个半神半人,因为他的爸爸是神他的妈妈是人类。Jupiter的妻子Juno对Jupiter的不忠感到愤怒,为了复仇,她弄瞎了Hercules的双眼导致他杀死了自己的妻子和孩子。为了赎罪,Delphi命令Hercules为Eurystheus服务,Eurystheus让他做12年的工人。

上一节中,证明了Saturn的孙子是Hercules。这也可以使用循环的方式来查询,实际上Hercules是从Saturn在in(‘father’) 路径上循环两次到达的点。

gremlin> hercules = g.V(saturn).repeat(__.in('father')).times(2).next()
==>v[1536]

Hercules是一个半神半人,为了证明Hercules半神半人,需要查询他父母的起源。可以从Hercules顶点去遍历他的母亲和父亲。最后可以去确定他们的类型是“god”和“human”。

gremlin> g.V(hercules).out('father', 'mother')
==>v[1024]
==>v[1792]
gremlin> g.V(hercules).out('father', 'mother').values('name')
==>jupiter
==>alcmene
gremlin> g.V(hercules).out('father', 'mother').label()
==>god
==>human
gremlin> hercules.label()
==>demigod

到目前为止,这些例子涉及了罗马万神殿中各种人物的遗传关系。 图的属性模型足以表示各种的事物和关系。 通过这种方式,Gods图也展示了Hercules的各种英雄事迹 - 他著名的12年工作。 在上一节中,发现Hercules参与了雅典附近的两场战争。 可以通过Hercules顶点遍历battled边来检索这些事件。

gremlin> g.V(hercules).out('battled')
==>v[2304]
==>v[2560]
==>v[2816]
gremlin> g.V(hercules).out('battled').valueMap()
==>[name:[nemean]]
==>[name:[hydra]]
==>[name:[cerberus]]
gremlin> g.V(hercules).outE('battled').has('time', gt(1)).inV().values('name')
==>cerberus
==>hydra

这个在battled边上的time属性是通过点的顶点中心索引来建立的索引。根据约束/过滤通过time检索Hercules的battled边比线性检索所有的边和过滤(通常为O(log n),n代表边的数目)更快。JanusGraph非常智能,在可以使用的时候会使用以顶点为中心的索引。Gremlin的toString()表达式将分解为单个步骤来展示。

gremlin> g.V(hercules).outE('battled').has('time', gt(1)).inV().values('name').toString()
==>[GraphStep([v[24744]],vertex), VertexStep(OUT,[battled],edge), HasStep([time.gt(1)]), EdgeVertexStep(IN), PropertiesStep([name],value)]

更复杂的图遍历示例

下面将展示更多关于Gods图的Gremlin遍历的例子。每个遍历查询的说明在注释中显示。

Tartarus的同居者

gremlin> pluto = g.V().has('name', 'pluto').next()
==>v[2048]
gremlin> // who are pluto's cohabitants?
gremlin> g.V(pluto).out('lives').in('lives').values('name')
==>pluto
==>cerberus
gremlin> // pluto can't be his own cohabitant
gremlin> g.V(pluto).out('lives').in('lives').where(is(neq(pluto))).values('name')
==>cerberus
gremlin> g.V(pluto).as('x').out('lives').in('lives').where(neq('x')).values('name')
==>cerberus

Pluto的兄弟

gremlin> // where do pluto's brothers live?
gremlin> g.V(pluto).out('brother').out('lives').values('name')
==>sky
==>sea
gremlin> // which brother lives in which place?
gremlin> g.V(pluto).out('brother').as('god').out('lives').as('place').select('god', 'place')
==>[god:v[1024], place:v[512]]
==>[god:v[1280], place:v[768]]
gremlin> // what is the name of the brother and the name of the place?
gremlin> g.V(pluto).out('brother').as('god').out('lives').as('place').select('god', 'place').by('name')
==>[god:jupiter, place:sky]
==>[god:neptune, place:sea]

最后Pluto居住在Tartarus因为他不在乎死亡。而他的兄弟们根据这些地方的品质来选择他们居住的地方。

gremlin> g.V(pluto).outE('lives').values('reason')
==>no fear of death
gremlin> g.E().has('reason', textContains('loves'))
==>e[6xs-sg-m51-e8][1024-lives->512]
==>e[70g-zk-m51-lc][1280-lives->768]
gremlin> g.E().has('reason', textContains('loves')).as('source').values('reason').as('reason').select('source').outV().values('name').as('god').select('source').inV().values('name').as('thing').select('god', 'reason', 'thing')
==>[god:neptune, reason:loves waves, thing:sea]
==>[god:jupiter, reason:loves fresh breezes, thing:sky]

扫码关注微信公众号,更好的交流

JanusGraph快速开始相关推荐

  1. java对象底层原存储结构图解_图解图库JanusGraph系列-一文知晓“图数据“底层存储结构...

    大家好,我是洋仔,JanusGraph图解系列文章,实时更新~ 图数据库文章总目录: 转载文章请保留以下声明: 一:存储模式 留言或私信我,邀请你加入"图数据库交流"微信群! 1. ...

  2. janusgraph整合mysql_图解图库JanusGraph系列-一文知晓“图数据“底层存储结构

    527-7.jpg 大家好,我是洋仔,JanusGraph图解系列文章,实时更新~ 图数据库文章总目录: 版本:JanusGraph-0.5.2 转载文章请保留以下声明: 一:存储模式 留言或私信我, ...

  3. 图数据库HugeGraph简介与快速入门

    图数据库HugeGraph简介与快速入门 作者:胡佳辉(家辉),日期:2019年2月10日 CSDN博客:https://blog.csdn.net/gobitan 1. HugeGraph简介 1. ...

  4. 图解图库JanusGraph系列-一文知晓“图数据“底层存储结构(JanusGraph data model)

    图解图库JanusGraph系列-一文知晓"图数据"底层存储结构(JanusGraph data model) 大家好,我是洋仔,JanusGraph图解系列文章,实时更新 图数据 ...

  5. Gephi使用详解 实现图可视化(janusgraph)

    描述 Gephi 是一款网络分析领域的数据可视化处理软件,开发者对它寄予的希望是:成为 "数据可视化领域的Photoshop" ,可运行在Windows,Linux及Mac os系 ...

  6. 图解图库Janusgraph系列-一文知晓图数据底层存储结构

    Python实战社群 Java实战社群 长按识别下方二维码,按需求添加 扫码关注添加客服 进Python社群▲ 扫码关注添加客服 进Java社群▲ 作者丨匠心Java 来源丨匠心Java(code-t ...

  7. JanusGraph服务器

    JanusGraph 服务器 JanusGraph使用Gremlin服务器引擎作为服务器组件来处理和回答客户机查询.当封装在JanusGraph中时,Gremlin服务器称为JanusGraph服务器 ...

  8. 「JanusGraph」图形数据库 - 技术选型调研

    JanusGraph各组件版本兼容性匹配表 JanusGraph JanusGraph提供多种后端存储和后端索引,使其能够更灵活的部署.本章介绍了几种可能的部署场景,以帮助解决这种灵活性带来的复杂性. ...

  9. 【JanusGraph】第七章:JanusGraph服务

    JanusGraph使用Gremlin Server作为服务组件对客户端请求进行请求处理响应.当JanusGraph打包的时候,Gremlin Server就被称为JanusGraph服务. Janu ...

  10. 快速排查feign.FeignException: status 500 …

    feign.FeignException: status 500 - 总结一下feign报500的时候快速排查问题的方法, 这个bug容易出现的地方分别为: 1. 远程调用的时候feign的注册信息有 ...

最新文章

  1. 15、Kubernetes集群资源监控
  2. Ruby中的设计模式
  3. Eclipse单元测试Android编程,在Eclipse中进行Android单元测试-Fun言
  4. Apache Flink 官方文档--概览
  5. android串口通讯奇偶校验,串口通讯奇偶数校验及CRC校验如何使用详解
  6. Spark集群搭建+基于zookeeper实现高可用HA
  7. 深度学习之caffe1——软件配置与测试
  8. Windows与Linux之间海量文件的传输与Linux下大小写敏感问题
  9. Mybatis+mysql动态分页查询数据案例——房屋信息的接口(IHouseDao)
  10. 深井软岩巷道群支护技术与应用_深井软岩巷道深浅孔帷幕注浆技术
  11. vmware12安装vmtools
  12. 人工智能技术与计算机科学区别,智能科学与技术与人工智能的区别
  13. 离散数学及其应用(英文版 第7版)及答案
  14. go语言-LiteIDE 安装及使用教程
  15. 基于C#实现与新大陆扫码枪通信
  16. OracleJDK 17真的香?
  17. 投资学U20 期权市场与交易策略 习题解读
  18. server port不起作用
  19. java 百度网盘上传_[Java教程]使用pcs api往免费的百度网盘上传下载文件
  20. Pr 入门教程:如何调整音频轨道音量?

热门文章

  1. 零基础爱好者如何入门音乐制作/自学音乐制作导航
  2. PLC系统的选型技巧
  3. java编程练习题三
  4. Java实现将阿拉伯数字转换为中文数字123=》一二三
  5. C4D快速入门教程——倒角
  6. GBaseDataStudio管理工具
  7. 如何使用NFC读写器读卡器ACR122U-A9|ACR1251U-M1|ACR1252U读写NDEF格式的智能海报|网址|文本等数据的方法与步骤
  8. 商务办公软件应用与实践【9】
  9. 电大网络计算机绘图,计算机绘图与AUTOCAD设计[031005217]-中国大学mooc-题库零氪
  10. SAP常用TCODE