Gremlin - Apache ThinkerPop#
Gremlin is a graph traversal language that is part of the Apache TinkerPop graph computing framework.
As an open source technology, multiple databases support it.
- class graphistry.gremlin.GremlinMixin(*args, gremlin_client=None, **kwargs)#
Bases:
object
Universal Gremlin<>pandas/graphistry functionality across Gremlin connectors
Currently serializes queries as strings instead of bytecode in order to support cosmosdb
- Parameters:
gremlin_client (Any | None)
- connect()#
Use previously provided credentials to connect. Disconnect any preexisting clients.
- drop_graph()#
Remove all graph nodes and edges from the database
- fetch_edges(batch_size=1000, dry_run=False, verbose=False, ignore_errors=False)#
Enrich edges by matching g._edges to gremlin edges
- Return type:
Plottable | List[str]
- fetch_nodes(batch_size=1000, dry_run=False, verbose=False, ignore_errors=False)#
Enrich nodes by matching g._node to gremlin nodes If no g._nodes table available, first synthesize g._nodes from g._edges
- Return type:
Plottable | List[str]
- gremlin(queries)#
Run one or more Gremlin queries and return the result as a PyGraphistry graph object.
This method allows you to execute Gremlin queries, either as a single string or an iterable of strings, and retrieve the results in a format that PyGraphistry can process and visualize. To support databases like CosmosDB, the queries are sent as strings.
- Parameters:
queries (Union[str, Iterable[str]]) – One or more Gremlin queries to execute. Can be a single query (as a string) or multiple queries (as a list of strings).
- Returns:
A PyGraphistry Plottable graph object containing the query results.
- Return type:
- Example: Execute a Gremlin Query and Plot
import graphistry from gremlin_python.driver.client import Client from gremlin_python.driver.serializer import GraphSONSerializersV2d0 # Create a Gremlin client for CosmosDB my_gremlin_client = Client( f'wss://MY_ACCOUNT.gremlin.cosmosdb.azure.com:443/', 'g', username=f"/dbs/MY_DB/colls/{self.COSMOS_CONTAINER}", password='MY_COSMOS_PRIMARY_KEY', message_serializer=GraphSONSerializersV2d0() ) # Run a Gremlin query and visualize the result graphistry .gremlin_client(my_gremlin_client) .gremlin('g.V().hasLabel("person").limit(5)') .fetch_nodes() .plot()
In this example, the Gremlin query selects the first 5 vertices with the label “person” and plots the result in PyGraphistry.
- Example: Running Multiple Gremlin Queries
queries = [ 'g.V().hasLabel("person").limit(5)', 'g.E().limit(10)' ] graphistry .gremlin_client(my_gremlin_client) .gremlin(queries) .fetch_nodes() .plot()
This example demonstrates how to run multiple Gremlin queries, fetch their results, and visualize them.
- gremlin_client(gremlin_client)#
Set the Gremlin client to interact with the Gremlin-enabled database.
This method allows you to pass a custom Gremlin Python client to interact with databases like CosmosDB using Gremlin queries. It stores the client for subsequent queries in the Graphistry workflow.
- Parameters:
gremlin_client (gremlin_python.driver.client.Client) – Instance of the Gremlin Python client.
- Returns:
The instance of Graphistry, updated with the Gremlin client.
- Return type:
- Example: Login and plot
import graphistry from gremlin_python.driver.client import Client my_gremlin_client = Client( f'wss://MY_ACCOUNT.gremlin.cosmosdb.azure.com:443/', 'g', username=f"/dbs/MY_DB/colls/{self.COSMOS_CONTAINER}", password=self.COSMOS_PRIMARY_KEY, message_serializer=GraphSONSerializersV2d0()) g = (graphistry .gremlin_client(my_gremlin_client) .gremlin('g.E().sample(10)') .fetch_nodes() # Fetch properties for nodes ) g.plot()
- gremlin_run(queries, throw=False)#
- Parameters:
queries (Iterable[str])
- Return type:
Any
- resultset_to_g(resultsets, mode='infer', verbose=False, ignore_errors=False)#
Convert traversal results to graphistry object with ._nodes, ._edges If only received nodes or edges, populate that field For custom src/dst/node bindings, passing in a Graphistry instance with .bind(source=.., destination=…, node=…) Otherwise, will do src/dst/id For dict results (ex: valueMap/elementMap), specify mode=’nodes’ (‘edges’), else will inspect field ‘type’
- Parameters:
resultsets (Any | Iterable[Any])
mode (str)
- Return type: