示例

这里将借助希腊诸神图来示例如何使用Janusgraph。这个图是基于Property Graph Model数据模型,描述了希腊诸神与其所居住的位置关系。其中使用到Gremlin查询语言,详细可参照Gremlin Query Language。

标记 含义
粗体关键字 图的索引。
带星的粗体关键字 图的索引且必须唯一
带下划线的关键字 vertex-centric索引关键字
空心箭头的边 无重复唯一的边
带短线的边 单向的边

将诸神图加载到JanusGraph

将一个图数据加载到Janusgraph主要通过JanusGraphFactory和GraphOfTheGodsFactory两个类。首先利用JanusGraphFactory提供的静态open方法,以配置文件路径作为输入参数,运行并返回graph实例(示例中的具体配置在配置章节中介绍)。然后利用GraphOfTheGodsFactory的load方法并将返回的graph实例作为参数运行,从而完成图加载到JanusGraph中。

将诸神图加载到带索引后端的Janusgraph

下面的conf/janusgraph-berkeleyje-es.properties配置文件配置的是以BerkeleyDB作为存储后端,Elasticsearch作为索引后端的Janusgraph。

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. 为图创建全局和vertex-centric的索引集合。
  • 2. 加载所有的顶点及其属性。
  • 3. 加载所有的边及其属性。

想更详细了解GraphOfTheGodsFactory可参考其源码。

conf/janusgraph-cql-es.properties配置文件可以让Janusgraph使用Cassandra作为存储后端。conf/janusgraph-hbase-es.properties配置文件可以让Janusgraph使用HBase作为存储后端。

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]

将诸神图加载到不带索引后端的Janusgraph

不带索引后端的配置文件有conf/janusgraph-cql.properties, conf/janusgraph-berkeleyje.properties, conf/janusgraph-hbase.properties, 或conf/janusgraph-inmemory.properties,可以根据实际后端存储选型而选择不同的配置文件。这里需要注意,使用的是GraphOfTheGodsFactory.loadWithoutMixedIndex()而不是之前的GraphOfTheGodsFactory.load()方法。

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语言的遍历路径进行遍历。全局索引能够提高遍历起始顶点或者边的查询性能。

假如在Janusgraph中以顶点的name属性建立了一个全局索引。那么我们可以查询出name为Saturn的顶点。因而我们可以获得Saturn顶点的所有属性:age是10000,type为"titan"。然后我们可以根据Gremlin遍历语言遍历查找出: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

同样在Janusgraph中以边的place属性建立一个全局索引。那么我们可以根据地理位置计算方法查询出在Athens周围50公里内发生的事件。然后利用上述事件查询出参与这些事件的诸神。如下所示:

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]

当利用g.V或者g.E去查询节点和边的时候,Janusgraph会自动利用建立的全局索引来提高查询效率,这也是全局索引的重要作用。但是全局索引并不是Janusgraph的唯一一种类型的索引。还有一种类型的索引叫vertex-centric,将在后面的章节中具体介绍。

图遍历示例

下面我们可以采用loop的方式来进行遍历从而得到Saturn的孙子是Hercules的结果,如下所示:

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

下面我们通过遍历诸神图的结果来证明Hercules是半人半神。我们以Hercules顶点为起始点去遍历他的父亲和母亲,并获取他父亲和母亲的type属性分别为"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

Property Graph Model图数据模型具有很强的表达能力,除了上述例子查询诸神的血缘关系外,还可以表示多种类型的事物和关系,我们同样可以根据不同类型的边去查询更丰富的结果,例如通过战斗的边来遍历hercules相关的战斗事迹。如下所示:

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属性创建了vertex-centric索引。当我们想根据time查询过滤边的时候效率就是大大提高。因为在没有为time属性建立vertex-centric索引的时候,我们往往需要将所有的边都查询来做条件过滤,那么这个过滤的效率随着边数量的增加而变的十分低效。下面就是利用vertex-centric索引对hercules参与的战斗进行遍历。如下所示:

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)]

更加复杂的图遍历示例

gremlin> pluto = g.V().has('name', 'pluto').next()
==>v[2048]
gremlin> // 谁是pluto的同居者?
gremlin> g.V(pluto).out('lives').in('lives').values('name')
==>pluto
==>cerberus
gremlin> // 同居者中去除pluto自己
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
gremlin> // pluto的兄弟住在哪里?
gremlin> g.V(pluto).out('brother').out('lives').values('name')
==>sky
==>sea
gremlin> // pluto所有兄弟的住所?
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> // pluto兄弟的名字及其住所?
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]
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. 图解Janusgraph系列-查询图数据过程源码分析

    图解Janusgraph系列-查询图数据过程源码分析 大家好,我是洋仔,JanusGraph图解系列文章,实时更新~ 图数据库文章总目录: 整理所有图相关文章,请移步(超链):图数据库系列-文章总目录 ...

  2. 图数据库JanusGraph实战[6]: JanusGraph+HBase+ElasticSearch的环境搭建

    图数据库JanusGraph实战[6]: JanusGraph+HBase+ElasticSearch的环境搭建 作者:胡佳辉(家辉)  日期:2019年01月14日 CSDN博客:https://b ...

  3. JanusGraph ,生产环境安装

    Part 1 Janusgraph 所需组的准备 ######安装ElasticSearch 需要组建包括casandra 和 elasticsearsh 我们需要先在服务器上安装并准备这两个环境.首 ...

  4. JanusGraph服务器

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

  5. janusgraph 引入 java_JanusGraph入门第一课和官方文档踩坑

    入门第一课是在IDEA里创建一个项目,有些小曲折.这里运行的Demo是读取janusgraph示例的"神之图"(Graph of the Gods)数据并打印,采用Hbase+ES ...

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

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

  7. 神图示例-AbutionGraph面向多维图谱的查询语言Aremlin与Gremlin一维的实现入门

    AbutionGraph是图特摩斯科技自研的首款GraphHOLAP图数据仓库,面向大规模实时图查询分析,在传统静态数据图谱的基础上,时序多维的动态知识图谱是其一大特色,从底层构建解决和优化一些既往图 ...

  8. 8. JanusGraph部署方案

    JanusGraph提供了多种存储和索引后端选项,可以灵活地部署它们.本章介绍了一些可能的部署方案,以帮助解决这种灵活性带来的复杂性. 在讨论不同的部署方案之前,了解JanusGraph本身和后端存储 ...

  9. 【JanusGraph】第三章: 入门

    本节中的例子展示了如何使用JanusGraph检索诸神关系图. 关系图如下图所示.抽象数据模型使用大家都熟知的Property Graph Model ,而且这个例子描述了罗马诸神中的人物和他们所在位 ...

  10. JanusGraph 入门

    Getting Started http://docs.janusgraph.org/latest/getting-started.html JanusGraph 第三章 入门 本节中的例子展示了如何 ...

最新文章

  1. python打飞机源代码-如何用 Python 打飞机 ?
  2. C#调用WebService出现“基础连接已经关闭:接收时发生错误”错误
  3. 产品问答 | PM该陪技术加班吗?要怎样培养技术认知?
  4. 北理工计算机博士怎么样,北京理工大学在职博士的含金量怎么样
  5. Oracle10g下载地址
  6. 【leetcode】Median of Two Sorted Arrays
  7. JS与Object-C交互补充
  8. jQuery AJAX 方法
  9. 基于SSM的智慧房屋租赁系统
  10. android打开超链接屏幕太小,手机屏幕太小,教你2种方法投屏到电脑上,小白也能轻松搞定!...
  11. 爬虫第七课:python爬取淘宝商品评论
  12. hdu5020 Revenge of Collinearity 求三点共线的点对个数
  13. 错误页面不暴漏,显示到一个漂亮页面
  14. 【C++】引用以及关联函数(详解)
  15. 如何实现链表的逆序?
  16. 汇编程序:将字符串倒序输出
  17. SAP系统中各Client设置说明
  18. 学习 《大话设计模式》笔记
  19. 第七十五章 SQL函数 LEFT
  20. minitab数据处理软件

热门文章

  1. 以接口请求的方式,解决移动端页面中字体文件过大的问题
  2. hprose php,hprose和swoole区别
  3. keil4for51与keil4forARM的安装与兼容
  4. 微信扫码点餐帮助商家赚钱的方法
  5. 上海计算机短期培训,上海日语短期培训速成班
  6. 阿铭Linux_网站维护学习笔记201903026
  7. MD5加密工具类--MD5Utils.java
  8. SVN回滚到某一版本
  9. php导入rtf文件获取内容,可以使用PHP在网页中显示RTF文件吗?
  10. 重磅!Pandownload开发者被抓,一代神器落幕!