前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >3. JanusGraph快速开始

3. JanusGraph快速开始

作者头像
公号:咻咻ing
发布2019-08-06 15:30:51
3.2K0
发布2019-08-06 15:30:51
举报
文章被收录于专栏:公众号:咻咻ing公众号:咻咻ing

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

Graph of the Gods

标示

含义

加粗的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的安装目录。

代码语言:javascript
复制
$ unzip janusgraph-0.3.1-hadoop2.zip
Archive:  janusgraph-0.3.1-hadoop2.zip
  creating: 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。本教程的其他部分将讨论如何构建特定的图。

代码语言:javascript
复制
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章“配置参考”。

代码语言:javascript
复制
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()

代码语言:javascript
复制
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图,这样它将不会创建索引。

代码语言:javascript
复制
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。

代码语言:javascript
复制
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范围内的信息。然后通过这些信息查看涉及到哪些顶点。

代码语言:javascript
复制
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') 路径上循环两次到达的点。

代码语言:javascript
复制
gremlin> hercules = g.V(saturn).repeat(__.in('father')).times(2).next()
==>v[1536]

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

代码语言:javascript
复制
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边来检索这些事件。

代码语言:javascript
复制
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()表达式将分解为单个步骤来展示。

代码语言:javascript
复制
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的同居者

代码语言:javascript
复制
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的兄弟

代码语言:javascript
复制
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因为他不在乎死亡。而他的兄弟们根据这些地方的品质来选择他们居住的地方。

代码语言:javascript
复制
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]

欢迎扫码关注公众号,更好的交流

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-04-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 咻咻ing 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 下载Janusgraph并运行Gremlin Console
  • 加载Gods图到JanusGraph中
  • 全局图索引
  • 图遍历示例
  • 更复杂的图遍历示例
相关产品与服务
图数据库 KonisGraph
图数据库 KonisGraph(TencentDB for KonisGraph)是一种云端图数据库服务,基于腾讯在海量图数据上的实践经验,提供一站式海量图数据存储、管理、实时查询、计算、可视化分析能力;KonisGraph 支持属性图模型和 TinkerPop Gremlin 查询语言,能够帮助用户快速完成对图数据的建模、查询和可视化分析。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档