本文属于入门级的实践,若有疏漏请大佬们不吝赐教。
复制代码
本文准备写入的demo数据以下(示意图): mysql
(^▽^)不要在乎细节,祝你们中秋愉快~!git
unzip janus-xxx.zip
github
后端存储使用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
顶点:人物、国、武器后端
边(关系):兄弟、战斗、使用(武器)、属于(国)bash
属性:名称(惟一),年龄elasticsearch
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
复制代码
查询刘备的兄弟:
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学习笔记】 你们有什么疑问欢迎留言和我交流。若有帮助,请点赞鼓励一下~