Gremlin-Python Connecting to existing JanusGraph
Asked Answered
I

1

6

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...

Inorganic answered 7/11, 2018 at 8:12 Comment(0)
C
12

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()
Chalcedony answered 7/11, 2018 at 9:51 Comment(9)
Nice and very detailed answer. I would only mention that gremlin-python only needs to be installed on the server if Python lambdas should be used. Otherwise the server already includes everything needed for Gremlin-Python to work. It just needs to be able to deserialize GraphSON for that.Hognut
Thanks Benoit for the details steps. Still I could not get where do we specify graph name in the python code? e.g. in the last section of your python code snippet where do you specify graph name i.e. "MYGRAPH" ? My python app wants to connect with "MYGRAPH" graph only where graphdb may be having more than one named graphs.Inorganic
I updated my answer to access "MYGRAPH" from Python. The trick is to load the graph when the gremlin server starts (see init.groovy) and to bind a variable to the graphTraversalSource. That way you can bind multiple variables to multiple graph traversals and choose which one to traverse based on your Python logic.Chalcedony
@BenoitGuigal, This is what exactly I was looking for. Thank you so much. I am unblocked now and everything is working. You made my day.... Thanks.Inorganic
@BenoitGuigal First of all, thanks for the answer - in the end I have managed to solve this problem using your solution, BUT with a few changes. I had to make the changes in gremlin-server.yaml not gremlin-server-configuration.yaml (why are there two? They look identical...) and in my .yaml file there is only a plugins section under scriptEngines.gremlin-groovy, so instead of scriptEngines.gremlin-groovy.scripts: [scripts/init.groovy] I have a scriptEngines.gremlin-groovy.plugins:{org.apache.tinkerpop.gremlin.jsr223.ScriptFileGremlinPlugin: {files: ['scripts/init.groovy']}}Trici
(And I run JanusGraph 0.3.0)Trici
If you do this and someone drops your graph using ConfiguredGraphFactory.drop() the myGraphTraversal will be dangling and strange things happenMuumuu
@BenoitGuigal i mistakenly installed "3.2.9" gremlin-python, now i am getting error for already exist. how to install "3.4.0". I cant connect from a remote address actuallyAmati
While trying to executing the following command getting Configuration not found error: ./bin/gremlin-server.sh -i org.apache.tinkerpop gremlin-python 3.2.9Dachshund

© 2022 - 2024 — McMap. All rights reserved.