0

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

1 Answer 1

0

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)
Sign up to request clarification or add additional context in comments.

5 Comments

So by "endpoint" you mean the Neptune DB endpoint name that we see on the AWS console right? Because when I replace "endpoint" with the what I see on the console, I get a InvalidURL error. I also tried using "wss://endpoint:8182/gremlin" but received a Failed to interpret Gremlin query: Query parsing failed at line 1, character position at 2, error message : no viable alternative at input \'g.with_\' error message. (Sorry if I'm missing something, I'm new to Neptune and gremlin so I'm trying to figure things out as I go)
Since you are using string query you don't need to use python escaping for special keywords such as "with". Instead of "with_" use "with". Change the string query to : "g.with('evaluationTimeout',50000).V('{}').emit().times({}).path().toList()".format(n,max_hops)
and just to confirm that yes, the endpoint should be in the wss://endpoint:8182/gremlin form.
This worked, thanks! Also we don't have to specify "g" here right? My understanding is that since here we are using client to connect we don't have to specify "g = traversal().withRemote(conn)" as we would do when using DriverRemoteConnection. Is that correct? Or am I understanding this wrong?
@KelvinLawrence What is difference between using DriverRemoteConnection and Client class? Is there any benefit to using Client class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.