Prerequisite
Install and set JAVA_HOME variable.
Install and start Cassandra server
Step 1: Download latest janusgraph library from below location.
https://github.com/JanusGraph/janusgraph/releases
Step 2: Extract the downloaded JanusGraph zip file.
Running the Gremlin Console
Post extraction, you can see a bin folder in JanusGraph distribution. The distribution looks like below.
bash-3.2$ ls
LICENSE.txt bin data ext lib scripts
NOTICE.txt conf examples javadocs logs
Execute below command to launch Gremlin console.
.\bin\gremlin.bat
(OR)
bin/gremlin.sh
bash-3.2$ gremlin.sh
\,,,/
(o o)
-----oOOo-(3)-oOOo-----
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/krishna/Documents/softwares/janusgraph-0.6.1/lib/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/krishna/Documents/softwares/janusgraph-0.6.1/lib/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
plugin activated: tinkerpop.server
plugin activated: tinkerpop.tinkergraph
20:27:08 WARN org.apache.hadoop.util.NativeCodeLoader - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
plugin activated: tinkerpop.hadoop
plugin activated: tinkerpop.spark
plugin activated: tinkerpop.utilities
plugin activated: janusgraph.imports
gremlin>
Setup Cassandra as a storage backend
Let’s configure Cassandra as a storage to janusgraph. Create a properties file with configurations.
cassandra.properties
gremlin.graph=org.janusgraph.core.JanusGraphFactory storage.backend=cql storage.hostname=localhost storage.port=9042 storage.username=cassandra storage.password=cassandra storage.cql.keyspace=janusgraph
Now go to gremlin console, and execute below command.
graph = JanusGraphFactory.open("{Cassandra.properties file path}")
gremlin> graph = JanusGraphFactory.open("/Users/Shared/cassandra.properties") 20:29:15 WARN com.datastax.oss.driver.api.core.auth.PlainTextAuthProviderBase - [JanusGraph Session] localhost/127.0.0.1:9042 did not send an authentication challenge; This is suspicious because the driver expects authentication 20:29:16 WARN com.datastax.oss.driver.internal.core.loadbalancing.helper.OptionalLocalDcHelper - [JanusGraph Session|default] You specified datacenter1 as the local DC, but some contact points are from a different DC: Node(endPoint=localhost/0:0:0:0:0:0:0:1:9042, hostId=null, hashCode=3711cf05)=null; please provide the correct local DC, or check your contact points 20:29:16 WARN com.datastax.oss.driver.api.core.auth.PlainTextAuthProviderBase - [JanusGraph Session] localhost/127.0.0.1:9042 did not send an authentication challenge; This is suspicious because the driver expects authentication 20:29:16 WARN com.datastax.oss.driver.internal.core.control.ControlConnection - [JanusGraph Session] Error connecting to Node(endPoint=localhost/0:0:0:0:0:0:0:1:9042, hostId=null, hashCode=27a0594e), trying next node (ConnectionInitException: [JanusGraph Session|control|connecting...] Protocol initialization request, step 1 (OPTIONS): failed to send request (io.netty.channel.StacklessClosedChannelException)) 20:29:16 WARN com.datastax.oss.driver.api.core.auth.PlainTextAuthProviderBase - [JanusGraph Session] localhost/127.0.0.1:9042 did not send an authentication challenge; This is suspicious because the driver expects authentication 20:29:16 WARN com.datastax.oss.driver.internal.core.loadbalancing.helper.OptionalLocalDcHelper - [JanusGraph Session|default] You specified datacenter1 as the local DC, but some contact points are from a different DC: Node(endPoint=localhost/0:0:0:0:0:0:0:1:9042, hostId=null, hashCode=27a0594e)=null; please provide the correct local DC, or check your contact points 20:29:16 WARN com.datastax.oss.driver.api.core.auth.PlainTextAuthProviderBase - [JanusGraph Session] localhost/127.0.0.1:9042 did not send an authentication challenge; This is suspicious because the driver expects authentication ==>standardjanusgraph[cql:[localhost]] gremlin>
Since I didn’t setup any authentication to my Cassandra local instance, you can see some warnings here.
Now the graph is connected.
Get Graph traversal object.
gremlin> g = graph.traversal() ==>graphtraversalsource[standardjanusgraph[cql:[localhost]], standard] gremlin>
Let’s query all the vertexes in the graph.
gremlin> g.V() 17:25:55 WARN org.janusgraph.graphdb.transaction.StandardJanusGraphTx - Query requires iterating over all vertices [()]. For better performance, use indexes gremlin>
Since I didn’t configure any vertexes yet, I got nothing.
Let’s create new person vertex by executing below snippet.
ram = g.addV('person').property('name','Ram').property('gender','M')
gremlin> ram = g.addV('person').property('name','Ram').property('gender','M') ==>v[4208] gremlin> gremlin> g.V() 19:23:41 WARN org.janusgraph.graphdb.transaction.StandardJanusGraphTx - Query requires iterating over all vertices [()]. For better performance, use indexes ==>v[4208]
Let’s insert one more person.
sailu = g.addV('person').property('name','Sailu').property('gender','F')
gremlin> sailu = g.addV('person').property('name','Sailu').property('gender','F') ==>v[8304]
Print all the vertexes.
gremlin> g.V().valueMap(true) 19:37:22 WARN org.janusgraph.graphdb.transaction.StandardJanusGraphTx - Query requires iterating over all vertices [()]. For better performance, use indexes ==>[id:4208,label:person,gender:[M],name:[Ram]] ==>[id:8304,label:person,gender:[F],name:[Sailu]]
Create an edge or relationship ‘knows’ between ram and sailu
Let’s create a ‘knows’ relationship (ram knows sailu).
g.V(4208L).addE('knows').to(__.V(8304L))
Here 4208 is the id of the vertex ‘ram’ and 8304 is the id of vertex sailu.
gremlin> g.V(4208L).addE('knows').to(__.V(8304L)) ==>e[3ke-38w-36d-6eo][4208-knows->8304] gremlin> gremlin> g.E() 19:42:45 WARN org.janusgraph.graphdb.transaction.StandardJanusGraphTx - Query requires iterating over all vertices [()]. For better performance, use indexes ==>e[3ke-38w-36d-6eo][4208-knows->8304]
In the above example,
a. 3ke-38w-36d-6eo is the id of the edge
b. 4208-knows->8304 specifies vertex with id 4208 knows vertex with id 8304.
That’s it you are done!!!!!
No comments:
Post a Comment