本文屬於入門級的實踐,若有疏漏請大佬們不吝賜教。
複製代碼
本文準備寫入的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學習筆記】 你們有什麼疑問歡迎留言和我交流。若有幫助,請點贊鼓勵一下~