需要帮助将 Gremlin 查询的结果提取到 Python 数据结构中

Need Help in fetching result of Gremlin query into Python data structures

我正在尝试从 Python 代码内部对 AWS Neptune 数据库进行 Gremlin 查询 运行,并希望将返回的数据存储到 Python 列表中。这对于简单的 Gremlin 查询工作正常,但一些更复杂的查询似乎有问题。

下面是代码,第一个 Gremlin 查询工作正常,但第二个查询不行。该节点不存在,因此可以在不导入任何数据的情况下进行尝试。

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()

这里是错误,任何指导将不胜感激

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

谢谢

线索在错误信息中。在 Python id 中是一个内置函数。请尝试 T.id

T是一个特殊的class/enum,可以在需要参考T.idT.label的时候使用。它是 Apache TinkerPop Gremlin Python 客户端的一部分(通常也作为 TinkerPop 的一部分提供)。

您只需将包含定义的文件包含到您的代码中。

from gremlin_python.process.traversal import T