The problem

  1. How do I write the Hbase index ElasticSearch configuration file?
  2. I’m going to create a new table in a database like mysql, how do I create a table in JanusGraph?
  3. How do I write vertices? Edge? Attribute data?
  4. How to view data? Visualization?

This article belongs to the entry level practice, if there are omissions, please big guys don't hesitate to give advice.Copy the code

In actual combat

The demo data to be written in this article is as follows (schematic diagram) :

(^ del ^) have spent Do not care about details, I wish you a happy Mid-Autumn Festival ~!

Set out

unzip janus-xxx.zip

configuration

Janusgraph-hbase-es. Properties (jG 0.4.0 does not support es 7.x integration, I use ES V6.5) :

CD janusgraph 0.4.0 - hadoop2 / conf vim janusgraph - hbase - es. PropertiesCopy the code

Key contents of the configuration file (mandatory). Other configurations are left unchanged by default.

#Storage. hostname= Address of the Hbase cluster to be configured
storage.hostname=hostname1,hostname2,hostname3
#Address and port of the ES service node## index.sanguo.backend=elasticsearch
index.sanguo.hostname=hostname1:9200,hostname2:9200,hostname3:9200
#Important, just copy
gremlin.graph=org.janusgraph.core.JanusGraphFactory
#If this parameter is specified, data is written to the Hbase table
storage.hbase.table=sanguo
#Index alias in ES
index.sanguo.index-name=sanguosha
Copy the code

Table =sanguo -> index. Sanguo. Index -name Note the consistency of bold font, please refer to the discussion in the comments section.

This configuration is fine. It’s not complicated.

Schema analysis of graph data

Apex: Character, nation, weapon

Side: brother, fight, use, belong to

Attributes: Name (unique), age

Gremlin command line

JanusGraph root directory to execute./bin/gremlin.sh

Create the entry

Graph = JanusGraphFactory. Open ('/opt/janus/janusgraph - 0.4.0 - hadoop2 / conf/janusgraph - hbase - es. The properties')Copy the code

The configuration file path writes its own location on the machine

Creating a vertex label

mgmt = graph.openManagement();
mgmt.makeVertexLabel('person').make();
mgmt.makeVertexLabel('country').make();
mgmt.makeVertexLabel('weapon').make();
mgmt.getVertexLabels();
mgmt.commit()
Copy the code

Create an edge label

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()
Copy the code

Create a properties

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()
Copy the code

Add the vertices

G = graph.traversal() liubei = g.addv ("person").property('name',' liubei ').property('age',45); Guanyu = g.a ddV (" person "). The property (' name ', 'guan yu). The property (' age', 42); Zhangfei = g.a ddV (" person "). The property (' name ', 'zhang fei'). The property (' age, 40); Lvbu = g.a ddV (" person "). The property (' name ', 'lu bu). The property (' age, 38); G.a ddV (" country "). The property (' name ', 'shu'); G. ddV("weapon"). Property ('name',' weapon '); G. ddV("weapon"). Property ('name',' double sword '); G. ddV("weapon"). Property ('name',' Green Dragon Crescent Blade '); G. ddV("weapon"). Property ('name',' zhangba snake spear '); for (tx in graph.getOpenTransactions()) tx.commit();Copy the code

Add the relationship

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()
Copy the code

You’re done

Gremlin looks at the number of vertices and relationships:

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

Show Time

Query Liu Bei’s brother:

gremlin> g.V().has('name'.'liu bei').next()
==>v[4112]
gremlin> g.V(4112).out('brother').values()==> Zhang Fei ==>40 ==> Guan Yu ==>42Copy the code

Query all characters in Shu:

gremlin> g.V().has('name'.'shu').next()
==>v[8360]
gremlin> g.V(8360).in('belongs').valueMap()= = > [name: [liu bei], age: [45]] = = > [name: [zhang fei], age: [40]] = = > [name: (guan yu), age: [42]]Copy the code

Visualize it

Use the GraphExp tool to visualize the query: portal

It’s a little uglier than the sketch I drew at the beginning

Look at the INFO:


conclusion

If you have any questions, please leave a message and communicate with me. If it’s helpful, please give me a thumbs up

Related column

JanusGraph first lesson – Creating an IDEA project

JanusGraph Problem and Solution Notes – Total collection