Skip to main content

Gremlin-Python for Apache TinkerPop

Project description

Apache TinkerPop™ is a graph computing framework for both graph databases (OLTP) and graph analytic systems (OLAP). Gremlin is the graph traversal language of TinkerPop. It can be described as a functional, data-flow language that enables users to succinctly express complex traversals on (or queries of) their application’s property graph.

Gremlin-Python implements Gremlin within the Python language and can be used on any Python virtual machine including the popular CPython machine. Python’s syntax has the same constructs as Java including “dot notation” for function chaining (a.b.c), round bracket function arguments (a(b,c)), and support for global namespaces (a(b()) vs a(__.b())). As such, anyone familiar with Gremlin-Java will immediately be able to work with Gremlin-Python. Moreover, there are a few added constructs to Gremlin-Python that make traversals a bit more succinct.

Gremlin-Python is designed to connect to a “server” that is hosting a TinkerPop-enabled graph system. That “server” could be Gremlin Server or a remote Gremlin provider that exposes protocols by which Gremlin-Python can connect.

A typical connection to a server running on “localhost” that supports the Gremlin Server protocol using websockets from the Python shell looks like this:

>>> from gremlin_python.process.anonymous_traversal import traversal
>>> from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
>>> g = traversal().with_remote(DriverRemoteConnection('ws://localhost:8182/gremlin','g'))

Once “g” has been created using a connection, it is then possible to start writing Gremlin traversals to query the remote graph:

>>> g.V().both()[1:3].to_list()
[v[2], v[4]]
>>> g.V().both()[1].to_list()
[v[2]]
>>> g.V().both().name.to_list()
[lop, vadas, josh, marko, marko, josh, peter, ripple, lop, marko, josh, lop]

Sample Traversals

The Gremlin language allows users to write highly expressive graph traversals and has a broad list of functions that cover a wide body of features. The Reference Documentation describes these functions and other aspects of the TinkerPop ecosystem including some specifics on Gremlin in Python itself. Most of the examples found in the documentation use Groovy language syntax in the Gremlin Console. For the most part, these examples should generally translate to Python with some modification. Given the strong correspondence between canonical Gremlin in Java and its variants like Python, there is a limited amount of Python-specific documentation and examples. This strong correspondence among variants ensures that the general Gremlin reference documentation is applicable to all variants and that users moving between development languages can easily adopt the Gremlin variant for that language.

Create Vertex

from gremlin_python.process.traversal import T
from gremlin_python.process.traversal import Cardinality

id = T.id
single = Cardinality.single

def create_vertex(self, vid, vlabel):
    # default database cardinality is used when Cardinality argument is not specified
    g.add_v(vlabel).property(id, vid). \
      property(single, 'name', 'Apache'). \
      property('lastname', 'Tinkerpop'). \
      next()

Find Vertices

def list_all(self, limit=500):
    g.V().limit(limit).element_map().to_list()

def find_vertex(self, vid):
    g.V(vid).element_map().next()

def list_by_label_name(self, vlabel, name):
    g.V().has(vlabel, 'name', name).element_map().to_list()

Update Vertex

from gremlin_python.process.traversal import Cardinality

single = Cardinality.single

def update_vertex(self, vid, name):
    g.V(vid).property(single, 'name', name).next()

NOTE that versions suffixed with “rc” are considered release candidates (i.e. pre-alpha, alpha, beta, etc.) and thus for early testing purposes only. These releases are not suitable for production.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gremlinpython-3.7.2.tar.gz (52.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gremlinpython-3.7.2-py2.py3-none-any.whl (78.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file gremlinpython-3.7.2.tar.gz.

File metadata

  • Download URL: gremlinpython-3.7.2.tar.gz
  • Upload date:
  • Size: 52.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for gremlinpython-3.7.2.tar.gz
Algorithm Hash digest
SHA256 58d12e4af81210d7770d54da8f1f586de816bc00858e095f76326d338ea34acd
MD5 23d4c831023b9e4ebe47d52d12209eb5
BLAKE2b-256 c93f798d17661a13d2da2721038e2c3602c33225b482da76a6bf2cbe23a034d7

See more details on using hashes here.

File details

Details for the file gremlinpython-3.7.2-py2.py3-none-any.whl.

File metadata

  • Download URL: gremlinpython-3.7.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 78.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for gremlinpython-3.7.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ff925632f70fc5afff6864627fa69de8df577090a6eea63f10f0fed7194f98bf
MD5 fe49b4c492ad64a92739dccb6f40ccbe
BLAKE2b-256 2035f70c50cf76419ed2f16d66d1362043796580ff357c2664eca7c023f8ca2d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page