ashis dash
ashis dash

Reputation: 27

How to execute a Gremlin query in Python

In Python, I am trying to connect using Gremlin and execute a query.

Here I could connect to Neptune DB using gremlin and fetch vertices count using only print statement: print(g.V().has("system.tenantId", "hLWmgcH61m0bnaI9KpUj6z").count().next())

but while reading from a file and storing in a variable and passing in gremlin query is not working

Code

from __future__  import print_function  # Python 2/3 compatibility

from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

graph = Graph()

remoteConn = DriverRemoteConnection('wss://<URL>:<port>/gremlin','g')
g = graph.traversal().withRemote(remoteConn)

with open ('Orgid1.txt','r') as file:
#    orgstr = file.readlines()
#    orgstr = [orgstr.rstrip() for line in orgstr]
    for line in file:
        print(g.V().has("system.tenantId", line.rstrip()).count().next())

#        print(neptune)




#print(g.V().count())

#print(g.V().has("system.tenantId", "xyz123").count().next())
remoteConn.close()

Adding more details:

This is the code I am using:

input:

with open ('Orgid1.txt','r') as file:
    for line in file:
         print(g.V().has("system.tenantId", line.rstrip()).out().count().next())

output:

$ python3 gremlinexample.py
0

if I directly print it its working. input:

print(line.rstrip())

output:

$ python3 gremlinexample.py
0
xyz123

ps: xyz123 present in input file named as Orgid1.txt

Upvotes: 0

Views: 1265

Answers (1)

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14391

You need to verify that the text coming from the file exactly matches the property key value needed. I created a simple file as follows:

$ cat values.txt
AUS
LHR
SEA

and used the basic structure of your code with the air-routes data:

g = graph.traversal().withRemote(connection)
with open ('values.txt','r') as file:
    for line in file:
        print(g.V().has('code', line.rstrip()).out().count().next())

and it worked fine

93
221
122

Upvotes: 1

Related Questions