JanusGraph
In this tutorial, we are first going to setup JanusGraph to work with YugabyteDB as the underlying database. Then, using the Gremlin console, we are going to load some data and run some graph commands.
Start a cluster on your local machine. Check that you are able to connect to YugabyteDB using by doing the following.
Connected to local cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.9-SNAPSHOT | CQL spec 3.4.2 | Native protocol v4]
Use HELP for help.
cqlsh>
cqlsh> DESCRIBE KEYSPACES;
system_schema system_auth system
cqlsh>
2. Download JanusGraph
$ wget https://github.com/JanusGraph/janusgraph/releases/download/v0.2.0/janusgraph-0.2.0-hadoop2.zip
$ unzip janusgraph-0.2.0-hadoop2.zip
$ cd janusgraph-0.2.0-hadoop2
- Start the Gremlin console by running
./bin/gremlin.sh
. You should see something like the following.
$ ./bin/gremlin.sh
- Now use the CQL config to initialize JanusGraph to talk to Yugabyte.
gremlin> graph = JanusGraphFactory.open('conf/janusgraph-cql.properties')
==>standardjanusgraph[cql:[127.0.0.1]]
- Open the YugabyteDB UI to verify that the
janusgraph
keyspace and the necessary tables were created by opening the following URL in a web browser:http://localhost:7000/
(replacelocalhost
with the ip address of any master node in a remote depoyment). You should see the following.
4. Load Sample Data
We are going to load the sample data that JanusGraph ships with - the Graph of the Gods. You can do this by running the following:
gremlin> GraphOfTheGodsFactory.loadWithoutMixedIndex(graph,true)
==>null
gremlin> g = graph.traversal()
- Retrieve the Saturn vertex
==>v[4168]
- Who is Saturn’s grandchild?
gremlin> g.V(saturn).in('father').in('father').values('name')
- Queries about Hercules
gremlin> hercules = g.V(saturn).repeat(__.in('father')).times(2).next()
==>v[4120]
// Who were the parents of Hercules?
gremlin> g.V(hercules).out('father', 'mother').values('name')
==>jupiter
==>alcmene
// Were the parents of Hercules gods or humans?
gremlin> g.V(hercules).out('father', 'mother').label()
==>god
==>human
// Who did Hercules battle?
gremlin> g.V(hercules).out('battled').valueMap()
==>[name:[hydra]]
==>[name:[nemean]]
==>[name:[cerberus]]
// Who did Hercules battle after time 1?
gremlin> g.V(hercules).outE('battled').has('time', gt(1)).inV().values('name')
==>cerberus
==>hydra
6. Complex Graph Traversal Examples
gremlin> pluto = g.V().has('name', 'pluto').next()
==>v[8416]
// who are pluto's cohabitants?
gremlin> g.V(pluto).out('lives').in('lives').values('name')
==>pluto
==>cerberus
// pluto can't be his own cohabitant
gremlin> g.V(pluto).out('lives').in('lives').where(is(neq(pluto))).values('name')
==>cerberus
gremlin> g.V(pluto).as('x').out('lives').in('lives').where(neq('x')).values('name')
==>cerberus
gremlin>
- Queries about Pluto’s Brothers.
// where do pluto's brothers live?
gremlin> g.V(pluto).out('brother').out('lives').values('name')
==>sea
==>sky
// which brother lives in which place?
gremlin> g.V(pluto).out('brother').as('god').out('lives').as('place').select('god', 'place')
==>[god:v[4248],place:v[4320]]
==>[god:v[8240],place:v[4144]]
// what is the name of the brother and the name of the place?
gremlin> g.V(pluto).out('brother').as('god').out('lives').as('place').select('god', 'place').by('name')
==>[god:neptune,place:sea]
NOTE: Secondary indexes in YugabyteDB are coming soon. These queries will iterate over all vertices to find the result.
- Geo-spatial indexes - events that have happened within 50 kilometers of Athens (latitude:37.97 and long:23.72).