Python通过Gremlin连接Neptune时查询未执行仅返回步骤结构问题
问题原因
Gremlin Python 中的遍历执行是惰性求值的,你编写的代码仅完成了查询步骤的结构构建,没有触发实际的远程请求执行,所以打印出来的是遍历对象本身的结构描述,不是查询结果。
解决方法
需要在遍历的末尾添加终端执行步骤,触发查询实际运行:
- 单值返回场景(比如你的count查询):追加
next()方法 - 多结果返回场景(比如查询多个顶点):追加
toList()方法 - 无返回值的操作场景(比如增删改数据):追加
iterate()方法
修改后的代码示例
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://my_end_point:8182/gremlin','g') g = graph.traversal().withRemote(remoteConn) # 追加next()触发执行,获取count的结果 print(g.V().has("system.tenantId", "sample_tenantId").count().next()) remoteConn.close()
之前你参考官方示例能正常拿到结果,是因为示例中的查询调用了对应的终端步骤触发了执行,没有触发步骤的遍历本质上只是一个查询结构定义,不会真的向Neptune发送请求。
内容的提问来源于stack exchange,提问作者ashis dash
相关产品推荐
相关产品推荐

