使用coalesce批量插边时Gremlin Python报'V未定义'错误
Gremlin Python批量边插入的问题与解决
问题场景
尝试使用coalesce实现批量边的upsert(存在则跳过,不存在则插入),参考Neptune文档编写了如下Gremlin代码:
g.V('v-1') .outE('KNOWS') .hasId('e-1') .fold() .coalesce(unfold(), V('v-1').addE('KNOWS') .to(V('v-2')) .property(id, 'e-1')) .V('v-3') .outE('KNOWS') .hasId('e-2').fold() .coalesce(unfold(), V('v-3').addE('KNOWS') .to(V('v-4')) .property(id, 'e-2')) .V('v-5') .outE('KNOWS') .hasId('e-3') .fold() .coalesce(unfold(), V('v-5').addE('KNOWS') .to(V('v-6')) .property(id, 'e-3')) .next()
遇到的错误
- 运行时触发
name 'V' is not defined错误,原因是Gremlin Python与原生Gremlin语法存在差异。 - 尝试将内部的
V改为__.V后,代码变为:
g.V('v-1') .outE('KNOWS') .hasId('e-1') .fold() .coalesce(unfold(), __.V('v-1').addE('KNOWS') .to(__.V('v-2')) .property(id, 'e-1')) ... .next()
此时出现新错误:
Received error message '{'requestId': 'None', 'status': {'code': 499, 'message': '{"detailedMessage":"Invalid OpProcessor requested [null]","code":"UnsupportedOperationException"}', 'attributes': {}}, 'result': {'meta': {}, 'data': None}}'
Gremlin连接代码如下:
def get_connection(endpoint): return DriverRemoteConnection(endpoint, 'g') def get_graph(connection): return traversal().withRemote(connection) connex = get_connection(neptune_endpoint) g = get_graph(connex)
最终解决方案
将内部的V替换为__.V,同时将property(id, 'e-1')中的id改为T.id,修正后的代码示例:
g.V('v-1') .outE('KNOWS') .hasId('e-1') .fold() .coalesce(unfold(), __.V('v-1').addE('KNOWS') .to(__.V('v-2')) .property(T.id, 'e-1')) .V('v-3') .outE('KNOWS') .hasId('e-2').fold() .coalesce(unfold(), __.V('v-3').addE('KNOWS') .to(__.V('v-4')) .property(T.id, 'e-2')) .V('v-5') .outE('KNOWS') .hasId('e-3') .fold() .coalesce(unfold(), __.V('v-5').addE('KNOWS') .to(__.V('v-6')) .property(T.id, 'e-3')) .next()
使用前需确保已导入__和T:
from gremlin_python.process.traversal import __, T
内容的提问来源于stack exchange,提问作者user20472255
相关产品推荐
相关产品推荐

