【JanusGraph】第三章: 入门

本文详细介绍JanusGraph图数据库的安装、配置及使用过程,包括数据模型、索引建立、图遍历语法Gremlin的应用,以及复杂查询案例解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Figure 3.1. 诸神关系图
在这里插入图片描述

虚拟元素意义
粗体的关键字图的索引
粗体加星的关键字图的索引, 必须唯一
下划线的关键字顶点中心索引
空心箭头的边功能或者唯一的边(不能重复)
尾巴带横线的边单向的边, 只能按照一个方向进行访问

3.1 下载JanusGraph和运行Gremlin命令行

JanusGraph的下载地址可以从最新的发布页面获得. 下载后解压, 就可以找到Gremlin命令行. Gremlin命令行是一种REPL, 与标准Groovy命令行的的微小区别, JanusGraph仅仅是一种预安装和预加载的包.
用户可以通过从中心仓库下载JanusGraph包, 在Gremlin命令行中安装后激活JanusGraph.在下列的例子中, 确保下载janusgraph.zip文件, 并解压了这个压缩包

Important:
JanusGraph需要Java8(标准版本) 推荐使用Oracle Java8. JanusGraph 的脚本需要$JAVA_HOME环境变量指向JRE或者JDK的安装目录

$ unzip janusgraph-0.2.0-hadoop2.zip
Archive:  janusgraph-0.2.0-hadoop2.zip
  creating: janusgraph-0.2.0-hadoop2/
...
$ cd janusgraph-0.2.0-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命令行使用Groovy翻译用户输入的命令. Groovy是java的一个扩展, 设计了许多简短标记, 可以让交互性编程语言更容易使用. 同样Gremlin-Groovy是Groovy的一个扩展, 让图的遍历更加方便.以下基本例子展示了JanusGraph使用数字, 字符串和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:
Refer to Apache TinkerPop, SQL2Gremlin, and Gremlin Recipes for more information about using Gremlin.

3.2 读取诸神关系图到JanusGraph

以下例子创建了JanusGraph的实例, 后加载了诸神关系图. JanusGraphFactory提供一系列静态读取配置参数方法, 每个方法会根据配置参数的不同, 返回一个对应的实例. 这里的示例使用BerkeleyDB存储和Elasticsearch索引, 通过GraphOfTheGodsFactory类加载诸神关系图. 本章没有讨论配置的详细内容, 如果要查看存储、索引和配置的详细信息, 请跳到对应章节

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()创建了一个图对象, 主要分3步

1. 为图创建了一系列全局, 顶点中心索引
2. 为图添加了顶点, 和顶点的属性
3. 为图添加了边, 和边的属性

想要看更详细的信息, 可以查看对应源码

这里使用的是JanusGraph/Cassandra, 确定加载了对应的配置文件conf/janusgraph-cassandra-es.properties, 执行了GraphOfTheGodsFactory.load()方法, 若使用HBase和es, 请读取conf/janusgraph-hbase-es.properties配置

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

你可能不需要使用索引, 那就读取下边几种conf/janusgraph-cassandra.properties, conf/janusgraph-berkeleyje.properties, or conf/janusgraph-hbase.properties. 另一个方法GraphOfTheGodsFactory.loadWithoutMixedIndex()来加载数据

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

3.3. 全局索引

访问图数据库中的数据, 首先是根据图的索引定位到切入点. 切入点是一个元素(或者一组元素), 例如:一个顶点或者一个边. Gremlin路径描述了如何通过图的结构, 从切入点元素访问到其他元素.
根据图, name属性的索引是唯一的, 可以通过这种方法访问克罗诺斯(Saturn)顶点. 也就可以获得该顶点的属性map, 克罗诺斯顶点name是克罗诺斯, age是10000, type是巨人. 克罗诺斯的孙子是谁? 结果是海格力斯.

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可以把边放到索引中. 你可能会查询距离Athens(维度: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要访问全部顶点或者全部边, 图的索引会自动匹配。
JanusGraph中另一种索引被称为顶点中心索引. 顶点中心索引用来加速图的内部访问. 稍后做解释。

3.3.1 访问图的例子

海格力斯是朱庇特和埃尔克莫尼的儿子, 拥有超过常人的力量. 海格力斯父亲是神,母亲是人类, 所以他是半人半神,
Juno是朱庇特的妻子, 对他的不忠极其气愤. 为了报复, 她蒙蔽了朱庇特, 并让朱庇特杀了他的妻子和孩子.为了补偿, 海格力斯被特尔非神谕安排服役于Eurystheus.

上一节, 克罗诺斯的孙子是海格力斯, 这可以使用循环, 从克罗诺斯沿着两次父亲的路径, 可以找到海格力斯

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

例子展示了罗马诸神的基因关系. Property Graph Model已经足够处理事物的多个类型和关系. 在上一节, 海格力斯在Athens附近打了两次仗, 通过访问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属性会被顶点中心索引管理. 对指定的边进行查找或过滤, 要比扫描全部的边再过滤要更快.JanusGraph可以自动使用顶点中心索引.

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

3.3.2.2. 普鲁托的兄弟们

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]
<think>好的,我需要解决用户提到的在阿里云Maven仓库中找不到org.janusgraph:janusgraph-io:pom:1.1.0的问题。首先,我应该回忆一下常见的Maven依赖问题解决方法,并结合用户提供的引用内容中的类似案例进行分析。 用户提供的引用[1]中,问题是由于本地仓库被清空后,父POM无法找到,导致构建失败。解决方案可能涉及重新安装依赖或检查父项目的相对路径。引用[2]和[3]则是关于特定依赖版本在远程仓库中不存在,解决方法包括添加其他仓库或更换版本。这些案例提示我需要考虑仓库配置和依赖版本的问题。 接下来,针对JanusGraph的依赖问题,可能的步骤包括: 1. **确认依赖配置是否正确**:用户是否在pom.xml中正确声明了依赖项,特别是groupId、artifactId和版本号。需要检查是否有拼写错误或版本号错误。 2. **检查阿里云仓库是否包含该构件**:访问阿里云Maven仓库的网页,手动搜索org.janusgraph:janusgraph-io:1.1.0是否存在。如果不存在,可能需要添加其他仓库,比如JanusGraph的官方仓库或Maven中央仓库。 3. **添加其他仓库配置**:如果阿里云仓库没有该构件,可以在pom.xml或settings.xml中添加其他仓库地址,例如Maven Central或JanusGraph自己的仓库。 4. **使用正确版本号**:确认1.1.0是否为JanusGraph的有效版本。有时候版本号可能不正确,或者该版本可能已被移动到其他仓库。 5. **本地安装依赖**:如果构件确实无法从远程仓库获取,可能需要手动下载jar和pom文件,然后使用mvn install命令安装到本地仓库。 6. **检查网络和代理设置**:确保Maven能够正常访问远程仓库,没有网络问题或代理配置错误。 现在,我需要验证这些步骤是否适用。首先,访问阿里云仓库的URL:https://maven.aliyun.com/mvn/search,搜索janusgraph-io,查看是否存在1.1.0版本。假设搜索后发现不存在,那么需要添加其他仓库。JanusGraph的官方仓库可能需要查阅其文档,例如可能托管在Maven Central或自己的仓库。例如,Maven Central的仓库URL是https://repo1.maven.org/maven2/,可以检查是否存在该构件。 如果Maven Central有该构件,那么用户需要在pom.xml中添加Maven Central仓库的配置,或者确保阿里云镜像正确代理了该仓库。如果阿里云镜像没有同步该构件,可能需要直接使用中央仓库。 此外,用户可能错误地使用了不存在的版本号,比如1.1.0是否存在。根据JanusGraph的发布历史,确认是否有这个版本。例如,JanusGraph的最新版本可能是0.6.x或1.0.0,而1.1.0可能不存在,导致无法找到依赖。 如果版本号正确,但仓库中没有,可能需要手动下载并安装。用户需要从官方来源获取jar和pom文件,然后使用mvn install:install-file命令安装到本地仓库。 最后,确保用户的Maven settings.xml中没有错误的镜像配置,例如将所有请求重定向到阿里云,而阿里云缺少该构件,这时候需要排除某些仓库不被镜像覆盖。 总结,可能的解决方案包括添加正确的仓库、检查依赖配置、使用正确版本、手动安装依赖或调整镜像设置。</think>### 解决阿里云仓库找不到`org.janusgraph:janusgraph-io:pom:1.1.0`的方法 #### 1. 确认依赖配置是否正确 检查`pom.xml`中是否正确定义了依赖项: ```xml <dependency> <groupId>org.janusgraph</groupId> <artifactId>janusgraph-io</artifactId> <version>1.1.0</version> </dependency> ``` - **常见问题**:版本号错误或拼写错误(如`janusgraph-io`写成`janusgraph-io-core`)[^1]。 #### 2. 检查阿里云仓库是否包含该构件 访问阿里云Maven仓库搜索页面(https://maven.aliyun.com/mvn/search),输入`org.janusgraph:janusgraph-io`,确认是否存在`1.1.0`版本。 若阿里云仓库无此版本,需添加其他仓库(如Maven Central或JanusGraph官方仓库)。 #### 3. 添加其他仓库配置 在`pom.xml`中添加Maven Central仓库: ```xml <repositories> <repository> <id>central</id> <url>https://repo1.maven.org/maven2/</url> </repository> </repositories> ``` 或添加JanusGraph官方仓库(需确认其地址,例如GitHub Packages)[^2]。 #### 4. 验证版本号合法性 通过JanusGraph官方文档或Maven Central(https://search.maven.org/)确认`1.1.0`是否为有效版本。 若版本号错误,调整为可用版本(如`0.6.2`): ```xml <version>0.6.2</version> ``` #### 5. 手动安装依赖到本地仓库 若远程仓库无此构件,需手动下载并安装: 1. 从官方来源获取`janusgraph-io-1.1.0.jar`和`janusgraph-io-1.1.0.pom`。 2. 执行命令: ```bash mvn install:install-file \ -Dfile=janusgraph-io-1.1.0.jar \ -DpomFile=janusgraph-io-1.1.0.pom ``` #### 6. 检查镜像配置 确认`settings.xml`中未强制覆盖仓库镜像。若阿里云镜像未代理Maven Central,需调整配置: ```xml <mirror> <id>aliyunmaven</id> <mirrorOf>central</mirrorOf> <!-- 仅镜像Central,避免覆盖其他仓库 --> <url>https://maven.aliyun.com/repository/public</url> </mirror> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值