[全程实测]如何新建一张JanusGraph图?(详)

问题

  1. 存储Hbase,索引elasticsearch配置文件怎么写?
  2. 我会在数据库好比mysql建一张新表,我该怎么在JanusGraph建立一张表?
  3. 怎么写入顶点?边?属性数据?
  4. 怎么查看数据?可视化?

本文属于入门级的实践,若有疏漏请大佬们不吝赐教。
复制代码

实战

本文准备写入的demo数据以下(示意图): mysql

桃园三结义

(^▽^)不要在乎细节,祝你们中秋愉快~!git

出发

unzip janus-xxx.zipgithub

配置

后端存储使用hbase,索引使用es,修改janusgraph-hbase-es.properties:sql

cd janusgraph-0.4.0-hadoop2/conf
vim janusgraph-hbase-es.properties
复制代码

配置文件重点内容(必填),其余默认不动:shell

#storage.hostname=这个地方要配置Hbase集群的地址
storage.hostname=hostname1,hostname2,hostname3
# ES服务的节点地址及端口 ## index.sanguo.backend=elasticsearch
index.sanguo.hostname=hostname1:9200,hostname2:9200,hostname3:9200
# 重要,照抄便可
gremlin.graph=org.janusgraph.core.JanusGraphFactory
# 对应habase中表名字,没有它会自动建立,若是指定了,则数据写入Hbase对应的表中
storage.hbase.table=sanguo
# 在ES中的索引别名
index.sanguo.index-name=sanguosha
复制代码

注意:storage.hbase.table=sanguo -> index.sanguo.index-name 注意加粗字体的一致性,请参考评论区的讨论。数据库

如此配置就行了,不复杂。vim

图数据的Schema分析

顶点:人物、国、武器后端

边(关系):兄弟、战斗、使用(武器)、属于(国)bash

属性:名称(惟一),年龄elasticsearch

Gremlin命令行

JanusGraph根目录执行./bin/gremlin.sh

建立入口

graph = JanusGraphFactory.open('/opt/janus/janusgraph-0.4.0-hadoop2/conf/janusgraph-hbase-es.properties')
复制代码

配置文件路径写本身机器上的位置

建立顶点标签

mgmt = graph.openManagement();
mgmt.makeVertexLabel('person').make();
mgmt.makeVertexLabel('country').make();
mgmt.makeVertexLabel('weapon').make();
mgmt.getVertexLabels();
mgmt.commit()
复制代码

建立边标签

mgmt = graph.openManagement();
brother = mgmt.makeEdgeLabel("brother").make();
mgmt.makeEdgeLabel("battled").make();
mgmt.makeEdgeLabel("belongs").make();
mgmt.makeEdgeLabel("use").make();
mgmt.getRelationTypes(EdgeLabel.class);
mgmt.commit()
复制代码

建立属性

mgmt = graph.openManagement();
name = mgmt.makePropertyKey('name').dataType(String.class).cardinality(Cardinality.SET).make();
mgmt.buildIndex('nameUnique', Vertex.class).addKey(name).unique().buildCompositeIndex();
age = mgmt.makePropertyKey("age").dataType(Integer.class).make();
mgmt.buildIndex('age2', Vertex.class).addKey(age).buildMixedIndex("sanguo");
mgmt.getGraphIndexes(Vertex.class);
mgmt.commit()
复制代码

添加顶点

g = graph.traversal()

liubei = g.addV("person").property('name','刘备').property('age',45);
guanyu = g.addV("person").property('name','关羽').property('age',42);
zhangfei = g.addV("person").property('name','张飞').property('age',40);
lvbu = g.addV("person").property('name','吕布').property('age',38);
g.addV("country").property('name','蜀国');
g.addV("weapon").property('name','方天画戟');
g.addV("weapon").property('name','双股剑');
g.addV("weapon").property('name','青龙偃月刀');
g.addV("weapon").property('name','丈八蛇矛');

for (tx in graph.getOpenTransactions()) tx.commit();
复制代码

添加关系

g.addE('brother').from(g.V(4112)).to(g.V(8208));
g.addE('brother').from(g.V(4112)).to(g.V(4280);
g.addE('brother').from(g.V(4280)).to(g.V(4112));
g.addE('brother').from(g.V(8208)).to(g.V(4112));
g.addE('brother').from(g.V(8208)).to(g.V(4280));
g.addE('brother').from(g.V(4280)).to(g.V(8208));

g.addE('use').from(g.V(4112)).to(g.V(4312));
g.addE('use').from(g.V(4280)).to(g.V(4320));
g.addE('use').from(g.V(8208)).to(g.V(4152));
g.addE('use').from(g.V(4264)).to(g.V(4160));

g.addE('belongs').from(g.V(4112)).to(g.V(8360));
g.addE('belongs').from(g.V(4280)).to(g.V(8360));
g.addE('belongs').from(g.V(8208)).to(g.V(8360));

g.addE('battled').from(g.V(4264)).to(g.V(4112));
g.addE('battled').from(g.V(4264)).to(g.V(4280));
g.addE('battled').from(g.V(4264)).to(g.V(8208));
g.addE('battled').from(g.V(4112)).to(g.V(4264));
g.addE('battled').from(g.V(4280)).to(g.V(4264));
g.addE('battled').from(g.V(8208)).to(g.V(4264));

for (tx in graph.getOpenTransactions()) tx.commit()
复制代码

大功告成

gremlin查看顶点数和关系数:

gremlin> g.V().count()
22:48:22 WARN  org.janusgraph.graphdb.transaction.StandardJanusGraphTx  - Query requires iterating over all vertices [()]. For better performance, use indexes
==>9
gremlin> g.E().count()
22:49:05 WARN  org.janusgraph.graphdb.transaction.StandardJanusGraphTx  - Query requires iterating over all vertices [()]. For better performance, use indexes
==>19
复制代码

Show Time

查询刘备的兄弟:

gremlin> g.V().has('name','刘备').next()
==>v[4112]
gremlin> g.V(4112).out('brother').values()
==>张飞
==>40
==>关羽
==>42
复制代码

查询蜀国的全部人物:

gremlin> g.V().has('name','蜀国').next()
==>v[8360]
gremlin> g.V(8360).in('belongs').valueMap()
==>[name:[刘备],age:[45]]
==>[name:[张飞],age:[40]]
==>[name:[关羽],age:[42]]
复制代码

可视化一下

使用GraphExp工具进行可视化查询展现:传送门

比开头画的效果图还丑点

查看INFO:


结语

归档:【JanusGraph学习笔记】 你们有什么疑问欢迎留言和我交流。若有帮助,请点赞鼓励一下~

相关专栏

JanusGraph入门第一课-建立IDEA项目

JanusGraph问题与解决笔记-总集

相关文章
相关标签/搜索