0

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

3
  • When you say not working, in what way? Are you getting an error message or are all the counts zero or something else? Commented Nov 3, 2021 at 11:47
  • If I am using direct command like: print(g.V().has("system.tenantId", "xyz123").count().next()). its working fine. but if using any variable to pass it then output is "0" in console Commented Nov 3, 2021 at 11:55
  • I would start by checking that the value coming from the file is exactly the same as the property value you are looking for. It may have additional whitespace or something like that. Commented Nov 3, 2021 at 14:00

1 Answer 1

1

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

4 Comments

This is the code I am using: input: ------ with open ('/home/ec2-user/ashis/NeptuneCount/Orgid1.txt','r') as file: for line in file: print(g.V().has("ystem.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
Hi Kelvin, I have added more details in description
If you are now using out().count() are there actually any edges present for that vertex?
I suggest you print len(line) and see if there are any unexpected characters in the line. The code I pasted works fine so long as the input file is correctly encoded.

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.