How to run query as a string using Gremlin Python

134 views Asked by At

I'm using lambda to query a Neptune graph where I'm trying to get output paths for a set of nodes. The query string is dynamically created in code as shown below. I was initially using Eval() to do this but on further research I see that Eval() comes with alot of issues so I wanted to know how I query a Neptune db with a string query?

for n in node_lst:
    database_url = "wss://neptune-db:8182/gremlin"
    connec = DriverRemoteConnection(
        database_url, "g",
message_serializer=serializer.GraphSONSerializersV2d0(),
transport_factory=lambda:AiohttpTransport(call_from_event_loop=True)
    )

    g = traversal().withRemote(conn)
    query_str = "g.with_('evaluationTimeout',50000).V(n).emit().times(max_hops).path().toList()"
    query_result = str(eval(query_str))
    conn.close()
    // Process graph output

I see from other posts here that there is a way to have the query as a string using client.submit() using this code but from what I understand we need to create a websocket connection to a locally running Gremlin Server. In this case, the server does not run locally.

I also tried with a local Gremlin Server but I get the below errors:

AttributeError: 'str' object has no attribute 'source_instructions'
Failed to interpret Gremlin query: Query parsing failed at line 1, character position at 2, error message : no viable alternative at input 'g.with_'"

I'm not able to find documentation regarding this. Could someone please help on how I could query Neptune using a string?

1

There are 1 answers

4
Kelvin Lawrence On

The Gremlin Python client can send queries either as byte code or as text, either way, it uses a web socket connection. However, if you are going to send the query as text you would not use DriverRemoteConnection you would instead use the Client class. Please see the example below.

from gremlin_python.driver import client

def create_client(endpoint):
    conn = client.Client(endpoint,'g')
    return conn
            
def run_query(conn, query):
    result = conn.submit(query)
    future_results = result.all()
    return future_results.result()

conn = create_client(endpoint)
query = 'g.V().limit(1)'
results = run_query(conn, query)