Gremlin-Python

3.8.0 · active · verified Sun Mar 29

Gremlin-Python is the Python Language Variant (GLV) for Apache TinkerPop, a graph computing framework. It enables users to express complex graph traversals using Python syntax, connecting to any TinkerPop-enabled graph system (like Gremlin Server or Amazon Neptune). The library is actively maintained and releases are typically aligned with major Apache TinkerPop versions, with the current version being 3.8.0.

Warnings

Install

Imports

Quickstart

Connects to a local Gremlin Server (defaults to `ws://localhost:8182/gremlin`), executes a simple traversal to count vertices, adds a new vertex, and then queries its name. It properly handles connection closing.

import os
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.process.anonymous_traversal import traversal

GREMLIN_SERVER_URL = os.environ.get('GREMLIN_SERVER_URL', 'ws://localhost:8182/gremlin')

# Establish a remote connection to the Gremlin Server
connection = None
try:
    connection = DriverRemoteConnection(GREMLIN_SERVER_URL, 'g')
    g = traversal().withRemote(connection)

    # Example Traversal: Get the count of all vertices
    vertex_count = g.V().count().next()
    print(f"Number of vertices: {vertex_count}")

    # Add a vertex and then query it
    new_vertex = g.addV('person').property('name', 'Alice').next()
    print(f"Added vertex: {new_vertex.id} - {new_vertex.label}")

    alice_name = g.V(new_vertex.id).values('name').next()
    print(f"Name of added vertex: {alice_name}")

finally:
    # Ensure the connection is closed to release resources
    if connection:
        connection.close()

view raw JSON →