Connecting from Python
Gremlin traversals can be constructed with Gremlin-Python just like in Gremlin-Java or Gremlin-Groovy. Refer to Gremlin Query Language for an introduction to Gremlin and pointers to further resources.
Important
Some Gremlin step and predicate names are reserved words in Python.
Those names are simply postfixed with _
in Gremlin-Python, e.g.,
in()
becomes in_()
, not()
becomes not_()
, and so on. The other
names affected by this are: all
, and
, as
, from
, global
,
is
, list
, or
, and set
.
Getting Started with JanusGraph and Gremlin-Python
To get started with Gremlin-Python:
- Install Gremlin-Python:
pip install gremlinpython==3.7.3
-
Create a text file
gremlinexample.py
and add the following imports to it:from gremlin_python import statics from gremlin_python.structure.graph import Graph from gremlin_python.process.graph_traversal import __ from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
-
Create a
GraphTraversalSource
which is the basis for all Gremlin traversals:from gremlin_python.process.anonymous_traversal import traversal connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g') # The connection should be closed on shut down to close open connections with connection.close() g = traversal().withRemote(connection) # Reuse 'g' across the application
-
Execute a simple traversal:
hercules_age = g.V().has('name', 'hercules').values('age').next() print(f'Hercules is {hercules_age} years old.')
next()
is a terminal step that submits the traversal to the Gremlin Server and returns a single result.
JanusGraph Specific Types and Predicates
JanusGraph contains some types and predicates that are not part of Apache TinkerPop and are therefore also not supported by Gremlin-Python.