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

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
ashis dash
  • 27
  • 5
  • When you say not working, in what way? Are you getting an error message or are all the counts zero or something else? – Kelvin Lawrence Nov 03 '21 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 – ashis dash Nov 03 '21 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. – Kelvin Lawrence Nov 03 '21 at 14:00

1 Answers1

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
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • 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 – ashis dash Nov 03 '21 at 16:23
  • Hi Kelvin, I have added more details in description – ashis dash Nov 03 '21 at 16:26
  • If you are now using `out().count()` are there actually any edges present for that vertex? – Kelvin Lawrence Nov 03 '21 at 16:40
  • 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. – Kelvin Lawrence Nov 03 '21 at 16:44