AWS Neptune中gremlinpython执行mergeV查询失败求助
问题:Neptune中mergeV查询解析失败,简单查询可正常运行
我正尝试按照教程操作,但直接从AWS Neptune文档提取的mergeV示例无法运行,而简单查询g.V().limit(10)可正常工作。
尝试的代码
from gremlin_python.driver import client import os # Neptune connection setup neptune_endpoint = os.getenv('NEPTUNE_ENDPOINT') neptune_port = os.getenv('NEPTUNE_PORT') neptune_uri = f'wss://{neptune_endpoint}:{neptune_port}/gremlin' conn = client.Client(neptune_uri,'g') query = """ g.mergeV([(T.id): 'v-1']). option(onCreate, [(T.label): 'PERSON', email: 'person-1@example.org', age: 21]). option(onMatch, [age: 22]). id() """ result = conn.submit(query) results = result.all().result() print(results)
错误信息
Received error message '{'requestId': '00f7a9b8-8a42-4a94-8e62-a2f28f3e2385', 'status': {'code': 499, 'message': '{"detailedMessage":"Failed to interpret Gremlin query: Query parsing failed at line 2, character position at 2, error message : token recognition error at: 'mer'","requestId":"00f7a9b8-8a42-4a94-8e62-a2f28f3e2385","code":"MalformedQueryException"}', 'attributes': {}}, 'result': {'meta': {}, 'data': None}}'
解决方案
问题根源
你提交的查询是Python语法风格,但Neptune的Gremlin端点使用Groovy语法解析请求,导致解析器无法识别(T.id)、onCreate这类Python式写法,触发解析错误。
修正后的代码
将查询改为Groovy兼容的语法:
from gremlin_python.driver import client import os # Neptune connection setup neptune_endpoint = os.getenv('NEPTUNE_ENDPOINT') neptune_port = os.getenv('NEPTUNE_PORT') neptune_uri = f'wss://{neptune_endpoint}:{neptune_port}/gremlin' conn = client.Client(neptune_uri,'g') query = """ g.mergeV([id: 'v-1']). option(onCreate, [label: 'PERSON', email: 'person-1@example.org', age: 21]). option(onMatch, [age: 22]). id() """ result = conn.submit(query) results = result.all().result() print(results)
关键修改说明
- 替换Python风格的
(T.id)为Groovy的id,直接用属性名作为map键 - 保留
onCreate/onMatch作为Groovy内置的枚举常量(Neptune环境已默认导入相关类) - Groovy的map语法无需括号包裹键,直接使用
[key: value]格式
如果仍有问题,可改用字符串形式的键名:
g.mergeV(['id': 'v-1']). option('onCreate', ['label': 'PERSON', 'email': 'person-1@example.org', 'age': 21]). option('onMatch', ['age': 22]). id()
内容的提问来源于stack exchange,提问作者Péricles Serotini
相关产品推荐
相关产品推荐

