from:http://s3.thinkaurelius.com/docs/titan/1.0.0/getting-started.html
在一开始,有两个神被称为天王星和盖亚。他们生了Titan(一种强大的生物)。土星,时间的Titan,在运动中设定现实。最终,时间产生了天空、海洋和生命死亡的终结。为了统治这些概念,土星有三个儿子:木星(天空)、海王星(海)和冥王星(黑社会)。土星的儿子不是Titan,而是一种看似不那么强大的神灵的种族。由于担心儿子们会推翻他,土星将他们吞噬并囚禁在他的胃里。这引起了Titan诸神之间的大战。最终,众神赢了,朱庇特成为了众神的领袖。
这部分的例子充分利用了一个被称为“神的图形”的土卫六的玩具图。这张图被画在下面。抽象数据模型被称为属性图模型,这个特殊的实例描述了罗马万神殿的存在与位置之间的关系。此外,图中的特殊文本和符号修饰符(如粗体、下划线等)表示图形中不同的图式/类型。
图3.1。诸神的图
visual symbol |
meaning |
bold key |
a graph indexed key |
bold key with star |
a graph indexed key that must have a unique value |
underlined key |
a vertex-centric indexed key |
hollow-head edge |
a functional/unique edge (no duplicates) |
tail-crossed edge |
a unidirectional edge (can only traverse in one direction) |
2.1 下载Titan并运行remlinShell
众神不知道的是,还有一个土卫六。这个Titan不能被看到,没有名字,而且只有在现实存在的情况下才显现出来。在这个迷失的Titan的肩膀上,所有的现实都挂在一个起伏不平的关系网里。
Titan可以从项目存储库的下载部分下载。一旦检索到并打开,就可以打开Gremlin控制台。GremlinREPL(即交互式shell)是由Titan分发的,与标准Gremlin控制台不同的是,Titan是一个预先安装和预加载的包。或者,用户可以通过从中央存储库下载Titan包来选择在现有的Gremlin控制台安装和激活Titan。在下面的例子中,titan。但是,使用zip确保解压下载的zip文件。
$unziptitan-1.0.0-hadoop1.zip
Archive: titan-1.0.0-hadoop1.zip
creating:titan-1.0.0-hadoop1/
...
$cdtitan-1.0.0-hadoop1
$bin/gremlin.sh
\,,,/
(oo)
-----oOOo-(3)-oOOo-----
09:12:24INFO org.apache.tinkerpop.gremlin.hadoop.structure.HadoopGraph -HADOOP_GREMLIN_LIBSissetto:/usr/local/titan/lib
plugin activated:tinkerpop.hadoop
plugin activated:aurelius.titan
gremlin>
注意,本教程假定了tinkerpop已集成。更简洁语法的sugar包。有关包处理的更多信息,请参考Tinkerpop文档。
Gremlin终端是一个Groovy shell。Groovy是Java的一组超集,它有各种简化的符号,使交互编程更容易。同样,Gremlin是Groovy的一种超集,它使用各种简化符号,使很容易对图形进行遍历。下面的基本示例演示处理数字、字符串和映射。本教程的其余部分将讨论特定于图形的构造。
gremlin>100-10
==>90
gremlin>"Titan:"+" The Rise of Big Graph Data"
==>Titan:TheRiseofBigGraphData
gremlin>[name:'aurelius',vocation:['philosopher','emperor']]
==>name=aurelius
==>vocation=[philosopher,emperor]
|
2.2 加载诸神图到Titan
下面的示例将打开Titan图形实例并加载上面的神像数据集图。Titan工厂提供一组静态访问方法,每一种方法都以配置为参数,并返回一个图形实例。本教程将在使用BerkeleyDB存储后端和Elasticsearch索引后端的配置上调用其中一个访问方法,然后使用辅助类GraphOfTheGodsFactory加载诸神的图形。本部分跳过了配置细节,但是关于存储后端、索引后端的附加信息,以及它们的配置在第三部分“存储后端”、第IV部分、“索引后端”和第12章配置引用中都有。
gremlin>graph=TitanFactory.open('conf/titan-berkeleyje-es.properties')
==>standardtitangraph[berkeleyje:../db/berkeley]
gremlin>GraphOfTheGodsFactory.load(graph)
==>null
gremlin>g=graph.traversal()
==>graphtraversalsource[standardtitangraph[berkeleyje:../db/berkeley],standard]
TitanFactory.open() 和GraphOfTheGodsFactory.load() 方法在返回之前,对新构造的图进行以下操作:
1. 在图上创建一个全局和以vertex为中心的索引集合。
2. 将所有的顶点加上它们的属性。
3. 将所有边缘添加到图中,并添加它们的属性。
相关详细信息,请参阅GraphOfTheGodsFactory源代码。
使用 Titan/Cassandra (或者 Titan/HBase), 一定要配置 conf/titan-cassandra-es.properties (或者conf/titan-hbase-es.properties) 和 GraphOfTheGodsFactory.load().
gremlin>graph=TitanFactory.open('conf/titan-cassandra-es.properties')
==>standardtitangraph[cassandrathrift:[127.0.0.1]]
gremlin>GraphOfTheGodsFactory.load(graph)
==>null
gremlin>g=graph.traversal()
==>graphtraversalsource[standardtitangraph[cassandrathrift:[127.0.0.1]],standard]
2.3 全球指数图
在图形数据库中访问数据的典型模式是使用图形索引首先将入口点定位到图中。该入口点是一个元素(或元素集)-即一个顶点或边。从入口元素,Gremlin路径描述描述了如何通过显式图形结构遍历图中的其他元素。
由于有一个独特的名称属性索引,可以检索土星顶点。然后可以检查属性图(即土星的键/值对)。正如所证明的,土星的顶点有一个“土星”的名字,一个10000岁的年龄,以及一种“Titan”。“土星的孙子可以用一个遍历来检索:“谁是土星的孙子?”(“父亲”的倒数是“孩子”)。结果是大力神。
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
属性位置也在一个图形索引中。属性位置是一个边缘属性。因此,Titan可以在图形索引中索引边缘。在雅典50公里以内发生的所有事件,都有可能查询到神的曲线图(纬度:37.97和纬度:23.72)。然后,考虑到这些事件中涉及到的信息。
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)或所有边(例如有或间隔)。Titan的索引的第二个方面被称为以vertex为中心的索引。利用以vertex为中心的索引来加速图中的遍历。后面描述了以vertexl为中心的索引。
2.3.1 图遍历的例子
赫尔克里斯是木星和阿尔克门的儿子,他有着超人的力量。赫拉克勒斯是一个半神,因为他的父亲是神,他的母亲是人。木星的妻子朱诺对朱庇特的不忠感到愤怒。为了报仇雪恨,她用一种短暂的精神错乱把她弄瞎了,并导致他杀死了自己的妻子和孩子。为了赎罪,赫拉克勒斯被德尔福的神谕命令去为欧律斯修斯服务。欧律斯修斯任命赫卡里斯为12名工人。
在上一节中,它证明了土星的孙子是大力神。这可以用一个循环来表示。从本质上讲,大力神是沿着(“父亲”)路径从土星上走2步的顶点。
gremlin>hercules=g.V(saturn).repeat(__.in('father')).times(2).next()
==>v[1536]
赫拉克勒斯是一个神。为了证明大力神是半人半神,他的父母的起源必须被检验。从赫拉克勒斯的顶点到他的母亲和父亲是可能的。最后,有可能确定他们中每个人的类型——屈服的“上帝”和“人”。
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
到目前为止,这些例子都是关于罗马万神殿中不同行动者的遗传线。属性图模型足以表示多种类型的事物和关系。在这种情况下,众神的曲线图也确定了赫尔克里斯的各种英雄事迹——他著名的12个劳作。在上一节中,人们发现赫尔克里斯卷入了雅典附近的两场战役。通过穿越与大力神顶点的边界,探索这些事件是可能的。
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
边的边缘属性时间被一个顶点的以vertex为中心的索引索引。根据时间的约束/过滤器,在时间上的约束/过滤比对所有边的线性扫描和滤波(通常是O(logn),n是数字事件的边缘)要快。Titan有足够的智能,可以在可用的时候使用以vertex为中心的索引。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)]
2.3.2 更复杂的图遍历示例
在塔塔卢斯的深处,冥王星的生命。他与赫尔克里斯的关系因赫尔克里斯与他的宠物赛伯罗斯的争斗而紧张。然而,赫尔克里斯是他的侄子——他应该如何让大力神为他的傲慢付出代价呢?
下面的Gremlin traversals提供了更多关于神的图表的例子。每条遍历的解释都提供在前面的行作为//注释。
1. 同居者的地狱
gremlin>pluto=g.V().has('name','pluto').next()
==>v[2048]
gremlin>// who are pluto'scohabitants?
gremlin>g.V(pluto).out('lives').in('lives').values('name')
==>pluto
==>cerberus
gremlin>// pluto can't behis 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
2. 冥王星的兄弟
gremlin>// where do pluto'sbrothers live?
gremlin>g.V(pluto).out('brother').out('lives').values('name')
==>sky
==>sea
gremlin>// which brotherlives in which place?
gremlin>g.V(pluto).out('brother').as('god').out('lives').as('place').select()
==>[god:v[1024],place:v[512]]
==>[god:v[1280],place:v[768]]
gremlin>// what is the nameof the brother and the name of the place?
gremlin>g.V(pluto).out('brother').as('god').out('lives').as('place').select().by('name')
==>[god:jupiter,place:sky]
==>[god:neptune,place:sea]
最后,布鲁托住在塔尔塔斯,因为他不关心死亡。另一方面,他的兄弟们根据他们对这些地点的某些品质的爱来选择他们的地点。
gremlin>g.V(pluto).outE('lives').values('reason')
==>nofearofdeath
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:loveswaves,thing:sea]
==>[god:jupiter,reason:lovesfreshbreezes,thing:sky]