问题
- 存储Hbase,索引elasticsearch配置文件怎么写?
- 我会在数据库比如mysql建一张新表,我该怎么在JanusGraph创建一张表?
- 怎么写入顶点?边?属性数据?
- 怎么查看数据?可视化?
本文属于入门级的实践,如有疏漏请大佬们不吝赐教。
实战
本文准备写入的demo数据如下(示意图):
(^▽^) 不要在意细节,祝大家中秋愉快~!
出发
unzip janus-xxx.zip
配置
后端存储使用hbase,索引使用es,修改janusgraph-hbase-es.properties(需要注意的是当前jg 0.4.0版本还不支持集成es 7.x版本,我使用的是es v6.5):
cd janusgraph-0.4.0-hadoop2/conf
vim janusgraph-hbase-es.properties
配置文件重点内容(必填),原来其他配置默认不动:
#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 注意加粗字体的一致性,请参考评论区的讨论。
如此配置就好了,不复杂。
图数据的Schema分析
顶点:人物、国、武器
边(关系):兄弟、战斗、使用(武器)、属于(国)
属性:名称(唯一),年龄
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学习笔记】 大家有什么疑问欢迎留言和我交流。如有帮助,请点赞鼓励一下~