Chapter 3. Getting Started

这章节的这个例子扩了一个玩具图的扩展使用,是建在janusgraph叫 The Graph of the Gods。这个图如下图形。这个抽象的数据模型被认为是一个标准的图模型,这个特的例子描述了罗马众神的关系、类型和位置。此外,图形中的特殊文字和信号标识(如加粗、下划线等)表示图中不同的含义/类型。


Graph of the Gods

symbol meaning

3.1 下载janusgraph,并运行gremlin控制台

janusgraph可以在项目repo的releases页面下载。一旦拿到并解压缩,就能打开gremlin的控制台。这个gremlin控制台是一个REPL(交互shell界面)的,并与janusgraph一起建立,和标准版的gremlin控制台唯一的区别是janusgraph是一个预安装和预加载的包。相应的,用户也可以选择install和激活janusgraph到一个已存在的gremlin console,从中央库下载janusgraph包。下面的例子里,janusgraph.zip被使用,要记得unzip这个压缩文件。

janusgraph需要java 8.建议是oracle java 8.janusgraph的shell脚本需要配置JAVA_HOME环境变量。
$ 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终端解释命令使用apache groovy。groovy是一个java的超集,有多样的速记声明,能够更容易的互动式编程。同样的,gremlin-groovy是一个groovy的超集,有多种速记声明使得图事务更容易。下面的基础例子示范了处理数字、字符串和map。这个教程的剩下部门将讨论图特有的结构。

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]
tip:可以参考 Apache TinkerPop, SQL2Gremlin, and Gremlin Recipes,有更多信息使用gremlin。

3.2 load到janusgraph(The Graph of the Gods)

下面的例子将打开janusgraph实例,load众神图数据集。 JunasGraphFactory 提供一个静态打开方法的集合,每一个将需要配置作为参数,返回图实例。这个教程用了那些open方法中的一个,后端存储用的是BerkeleyDB和es索引,然后load图用一个帮助类GraphOfTheGodsFactory。这章跳过了配置细节,但是额外的存储、索引和配置在 Part III, “Storage Backends”, Part IV, “Index Backends”, and Chapter 15, Configuration Reference.

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 source code源码了解细节

对于使用cassandra或hbase,请确保使用了conf/janusgraph-cql-es.properties (or 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, or conf/janusgraph-hbase.properties配置文件,没有配置索引情况下来打开图。这样的话讲需要使用另一个load方法*GraphOfTheGodsFactory.loadWithoutMixedIndex() *,这样讲不会尝试使用后端索引。

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]

3.3 全局图索引

图数据库访问数据的典型方法是使用图索引首先定位图的入口点。入口点是一个元素或一组元素,例如一个顶点或边。从入口的元素,一个gremlin路径解释器通过详细的图结构描述如何构图出其它元素。

根据这里有的唯一索引name属性,Saturn顶点可以被取出。详细的map(Saturn的kv对)能够被查出。Saturn的顶点有name=saturnage=10000type=titan。他的孙子(father的inverse是child)可以被取出通过构图遍历。结果是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也是一个图索引,是一个边属性。因而,janusgraph可以通过边进行索引。可以查询Ahtens(37.97,23.72)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]

图索引是一个索引结构类型在janusgraph。图索引被janusgraph自动选择来回应全部顶点(g.V)或全部边(g.E),满足1个或多个限制条件(例如has or interval)。索引的第二个部分是中央顶点索引,中央顶点索引用来加速构图。中央顶点索引将在后面描述。

3.3.1 构图例子

Hercules,Jupiter和Alcmene的儿子,天生神力。Hercules是一个Demigod因为他父亲是神而母亲是人。Juno是Jupiter的妻子,为Jupiter的不忠而愤怒。为了报复,她使Hercules精神失常杀了自己的妻子和孩子。....

在前面章节,示范了Saturn的孙子是Hercules。这能用一个环表述,本质上,Hercules是一个顶点举例Saturn有2步远(father路径)

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

证明Hercules是个半神,从他开始构图到他的父母,最终确定他们的类型是godhuman

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

上面例子是众神基因线。属性图模型扩展可以体现多种类型和关系。也体现了Hercules的多个英雄事迹。前面了解到他有两次事件在Athens附近,可以查出这些事件通过Hercules顶点构图battle边。

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

边属性time次数在battle边被索引一个顶点的中央顶点索引。查出battle边发生于Hercules根据限制/过滤time,会比线性查询所有边和过滤更快(O(logn)n是关系边的次数)。当可用时,janusgraph足够智能去用中央顶点索引。加一个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)]
3.3.2 更多复杂图构建例子

gremlin构建下面的更复杂例子,每一个图构建的说明在上一行的注释。

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
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]
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]
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,780评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,424评论 1 305
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,397评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,576评论 0 218
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,997评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,945评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,107评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,850评论 0 208
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,625评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,804评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,285评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,613评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,291评论 3 242
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,164评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,963评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,096评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,886评论 2 278

推荐阅读更多精彩内容