I am trying to run a Gremlin query on AWS Neptune database from inside Python code and want to store the returned data into Python List. This is working fine for simple Gremlin queries but some more complex ones seem to be having an issue.
Below is the code, The first Gremlin query is working fine but not the second one. The Node does not exist so this can be tried without importing any data.
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://sdm-neptune-db-instance-1.cduuicw2rgrv.us-east-1.neptune.amazonaws.com:8182/gremlin','g')
g = graph.traversal().withRemote(remoteConn)
cust_List=g.V('AZ50K115E39AX').hasLabel('tp21tpcust').count().toList()
for p in cust_List:
print('Data Fetched: ' + str(p))
cust_List=g.V('XXXXXXXX').hasLabel('tp21tpcust').local(__.repeat(__.out().simplePath()).until(__.not_(__.out())).path().by(id).limit(100)).local(__.unfold().union(__.limit(1),__.tail()).fold()).dedup().toList()
for p in cust_List:
print('Data Fetched' + p)
remoteConn.close()
Here is the error, any guidance will be appreciated
Data Fetched: 1
Traceback (most recent call last):
File "TestPythonGremlin.py", line 19, in <module>
cust_List=g.V('XXXXXXXX').hasLabel('tp21tpcust').local(__.repeat(__.out().simplePath()).until(__.not_(__.out())).path().by(id).limit(100)).local(__.unfold().union(__.limit(1),__.tail()).fold()).dedup().toList()
File "/usr/local/lib/python3.7/site-packages/gremlin_python/process/traversal.py", line 58, in toList
return list(iter(self))
File "/usr/local/lib/python3.7/site-packages/gremlin_python/process/traversal.py", line 48, in __next__
self.traversal_strategies.apply_strategies(self)
File "/usr/local/lib/python3.7/site-packages/gremlin_python/process/traversal.py", line 573, in apply_strategies
traversal_strategy.apply(traversal)
File "/usr/local/lib/python3.7/site-packages/gremlin_python/driver/remote_connection.py", line 149, in apply
remote_traversal = self.remote_connection.submit(traversal.bytecode)
File "/usr/local/lib/python3.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 55, in submit
result_set = self._client.submit(bytecode)
File "/usr/local/lib/python3.7/site-packages/gremlin_python/driver/client.py", line 127, in submit
return self.submitAsync(message, bindings=bindings).result()
File "/opt/Python-3.7.9/Lib/concurrent/futures/_base.py", line 428, in result
return self.__get_result()
File "/opt/Python-3.7.9/Lib/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/usr/local/lib/python3.7/site-packages/gremlin_python/driver/connection.py", line 66, in cb
f.result()
File "/opt/Python-3.7.9/Lib/concurrent/futures/_base.py", line 428, in result
return self.__get_result()
File "/opt/Python-3.7.9/Lib/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/opt/Python-3.7.9/Lib/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/local/lib/python3.7/site-packages/gremlin_python/driver/protocol.py", line 74, in write
request_id, request_message)
File "/usr/local/lib/python3.7/site-packages/gremlin_python/driver/serializer.py", line 144, in serialize_message
message = self.build_message(request_id, processor, op, args)
File "/usr/local/lib/python3.7/site-packages/gremlin_python/driver/serializer.py", line 154, in build_message
return self.finalize_message(message, b"\x21", self.version)
File "/usr/local/lib/python3.7/site-packages/gremlin_python/driver/serializer.py", line 157, in finalize_message
message = json.dumps(message)
File "/opt/Python-3.7.9/Lib/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/opt/Python-3.7.9/Lib/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/opt/Python-3.7.9/Lib/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/opt/Python-3.7.9/Lib/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type builtin_function_or_method is not JSON serializable
Thanks
The clue is in the error message. In Python id
is a built in function. Try T.id
instead.
T
is a special class/enum that can be used when you need to refer to T.id
or T.label
. It is part of the Apache TinkerPop Gremlin Python client (and also available in general as part of TinkerPop).
You will just need to include the file containing the definition into your code.
from gremlin_python.process.traversal import T