Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gremlin-Python Connecting to existing JanusGraph

I Have created a graph using gremlin console

gremlin> ConfiguredGraphFactory.graphNames
==>MYGRAPH
gremlin> ConfiguredGraphFactory.getConfiguration('MYGRAPH')
==>storage.backend=cql
==>graph.graphname=MYGRAPH
==>storage.hostname=127.0.0.1
==>Template_Configuration=false
gremlin> g.V().properties()
==>vp[name->SFO]
==>vp[country->USA]
==>vp[name->ALD]
==>vp[country->IND]
==>vp[name->BLR]
==>vp[country->IND]
gremlin>

I want to connect with MYGRAPH using gremlin-python. Can someone please tell me how to access graph named "MYGRAPH" using gremlin-python.

Thanks in advance...

like image 451
Servesh Singh Avatar asked Dec 17 '22 20:12

Servesh Singh


1 Answers

First of all you will need to install some jar files for JanusGraph to handle gremlin-python scripts:

./bin/gremlin-server.sh -i org.apache.tinkerpop gremlin-python 3.2.9

Please note that the version of gremlin-python you install must match the Tinkerpop version JanusGraph is compatible with. You can find compatibility information on the JanusGraph releases page. For example JanusGraph 0.2.2 is compatible with Tinkerpop 3.2.9.

Next you need to start a JanusGraph server using ConfiguredGraphFactory. You just have to use the file conf/gremlin-server/gremlin-server-configuration.yaml from the ditribution.

bin/gremlin-server.sh conf/gremlin-server/gremlin-server-configuration.yaml

This file differs from the traditional conf/gremlin-server/gremlin-server.yaml in those few lines

graphManager: org.janusgraph.graphdb.management.JanusGraphManager
graphs: {
  ConfigurationManagementGraph: conf/janusgraph-cql-configurationgraph.properties
}

Then we need to load the graph MYGRAPH during the initialization script of the server. Please create an init script scripts/init.groovy. Here you can load as many different graphs as you want.

def globals = [:]
myGraph = ConfiguredGraphFactory.open("MYGRAPH")
globals << [myGraphTraversal : myGraph.traversal()]

Make sure this script is executed when gremlin server starts in conf/gremlin-server/gremlin-server-configuration.yaml

scriptEngines: {
  gremlin-groovy: {
    imports: [java.lang.Math],
    staticImports: [java.lang.Math.PI],
    scripts: [scripts/init.groovy]}}

Finally in your Python project, install the gremlin-python package that matches the Tinkerpop version of your version of JanusGraph. In case of JanusGraph 0.2.2, this is version 3.2.9.

pip install gremlin-python==3.2.9

Start a Python shell and start coding:

>>> from gremlin_python import statics
>>> from gremlin_python.structure.graph import Graph
>>> from gremlin_python.process.graph_traversal import __
>>> from gremlin_python.process.strategies import *
>>> from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

>>> graph = Graph()
>>> myGraphTraversal = graph.traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','myGraphTraversal'))
>>> myGraphTraversal.V().count()
like image 94
Benoit Guigal Avatar answered Jan 06 '23 01:01

Benoit Guigal