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?
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 theClient
class. Please see the example below.