gremlinpython-返回id和标签作为字符串
我在Python 3.7.2上使用gremlinpython 3.4.1,当获取顶点/边响应时,它为id提供gremlinpython-返回id和标签作为字符串,python,gremlin,Python,Gremlin,我在Python 3.7.2上使用gremlinpython 3.4.1,当获取顶点/边响应时,它为id提供,为标签提供。我如何让它在响应中为id和label提供字符串值呢?我的目标是获取输出并生成JSON 输出: python3 stackoverflow.py [{'name': ['USA'], 'parentname': ['USA'], 'shortname': ['US'], <T.id: 1>: 'country-us', 'parentid': ['country-u
,为标签提供
。我如何让它在响应中为id和label提供字符串值呢?我的目标是获取输出并生成JSON
输出:
python3 stackoverflow.py
[{'name': ['USA'], 'parentname': ['USA'], 'shortname': ['US'], <T.id: 1>: 'country-us', 'parentid': ['country-us'], <T.label: 3>: 'Country'}]
顺便说一句,我已经尝试使用.with(WithOptions.ids)
例如:
response=g.V().has('name','USA')。limit(1000)。hasLabel('Country')。valueMap(True)。with(WithOptions.ids)。toList()
对此,我得到以下错误:
gremlin_python.driver.protocol.GremlinServerError:599:{“requestId”:“bf74df44-f064-4411-a1cb-78b30f9d2cf6”,“code”:“InternalFailureException”,“detailedMessage”:“无法找到方法:NeptuneGraphTraversal.with([1])”
您可以尝试项目的结果
g.V().has('name', 'USA').limit(1000).hasLabel('Country') \
.project('id', 'label', 'name', 'parentname', 'shortname', 'parentid') \
.by(id) \
.by(label) \
.by('name') \
.by('parentname') \
.by('shortname') \
.by('parentid') \
.toList()
此外,对于已经给出的project()
示例,如果不能或不想指定属性名称,可以执行如下操作:
g.V().has('name', 'USA').limit(1000).hasLabel('Country').
map(union(project('id','label').
by(id).
by(label),
valueMap()).unfold().
group().
by(keys).
by(select(values))) // select(values).unfold() if you only have single values
您可以用实际值替换EnumMeta dict键。要使用此函数,需要在valueMap之后添加一个unfold()
from gremlin_python.process.traversal import T
def get_query_result_without_enum_metas(query_result):
return [replace_enum_metas(d) for d in query_result]
def replace_enum_metas(dict):
dict_key = (*dict,)[0]
if type(dict_key) is str:
return dict
elif type(dict_key) is T:
return {dict_key.name: dict[dict_key]}
input: [{'vertex_property': ['Summary']}, {<T.id: 1>: '4b30f448ee2527204a050596b'}, {<T.label: 3>: 'VertexLabel'}]
output: [{'vertex_property': ['Summary']}, {'id': '4b30f448ee2527204a050596b'}, {'label': 'VertexLabel'}]
来自gremlin\u python.process.traversal import T
def get_query_result_不带枚举元(查询结果):
返回[在查询结果中为d替换\u枚举\u元(d)]
定义替换枚举元(dict):
dict_key=(*dict,)[0]
如果类型(dict_键)为str:
返回指令
elif类型(dict_键)为T:
返回{dict_key.name:dict[dict_key]}
输入:[{'vertex_property':['Summary']},{'4b30f448ee2527204a050596b'},{'VertexLabel'}]
输出:[{'vertex_property':['Summary']},{'id':'4b30f448ee2527204a050596b'},{'label':'VertexLabel'}]
谢谢。这是一个小的修改,其中id是T.id,label是T.label:g.V()。has('name','USA')。limit(1000)。hasLabel('Country')\。project('id','label','name','parentname','parentname','shortname','parentid')\。by(T.id)\.by(T.label)\.by('name')\.by('parentname')\.by('parentname')\.by('shortname')\.by()非常感谢。我得到错误:NameError:name'union'未定义您尝试过吗?我不知道如何在Python中进行静态导入。我喜欢这种方法,因为它不需要我列出要包含在响应中的值。我喜欢这种方法,因为它不需要我列出要包含在响应中的值。。。但是,我不清楚get_query_result_而不使用_enum_metas函数的“self”参数-您是否可以更新您的答案以包括该函数的使用方式和位置?self完全不需要。我把它们放在那里只是因为这些方法是我创建的一个更大类的成员。我更新了答案。
from gremlin_python.process.traversal import T
def get_query_result_without_enum_metas(query_result):
return [replace_enum_metas(d) for d in query_result]
def replace_enum_metas(dict):
dict_key = (*dict,)[0]
if type(dict_key) is str:
return dict
elif type(dict_key) is T:
return {dict_key.name: dict[dict_key]}
input: [{'vertex_property': ['Summary']}, {<T.id: 1>: '4b30f448ee2527204a050596b'}, {<T.label: 3>: 'VertexLabel'}]
output: [{'vertex_property': ['Summary']}, {'id': '4b30f448ee2527204a050596b'}, {'label': 'VertexLabel'}]