首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >gremlinpython -返回id和标签为字符串

gremlinpython -返回id和标签为字符串
EN

Stack Overflow用户
提问于 2019-05-03 12:31:18
回答 3查看 1.5K关注 0票数 3

我在Python3.7.2上使用gremlinpython3.4.1,当获得顶点/边响应时,它为id提供了<T.id: >,为标签提供了<T.label: 3>。我如何让它在响应中提供id和label的字符串值?我的目标是获取输出并生成JSON

输出:

代码语言:javascript
运行
复制
python3 stackoverflow.py
[{'name': ['USA'], 'parentname': ['USA'], 'shortname': ['US'], <T.id: 1>: 'country-us', 'parentid': ['country-us'], <T.label: 3>: 'Country'}]

代码:

代码语言:javascript
运行
复制
from gremlin_python import statics
from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.process.traversal import T
from gremlin_python.process.traversal import Order
from gremlin_python.process.traversal import Cardinality
from gremlin_python.process.traversal import Column
from gremlin_python.process.traversal import Direction
from gremlin_python.process.traversal import Operator
from gremlin_python.process.traversal import P
from gremlin_python.process.traversal import Pop
from gremlin_python.process.traversal import Scope
from gremlin_python.process.traversal import Barrier
from gremlin_python.process.traversal import Bindings
from gremlin_python.process.traversal import WithOptions

CLUSTER_ENDPOINT = "removed"
PORT = "8182"
g = traversal().withRemote(DriverRemoteConnection('wss://' + CLUSTER_ENDPOINT + ':' + PORT + '/gremlin','g'))

response = g.V().has('name', 'USA').limit(1000).hasLabel('Country').valueMap(True).toList()
print(response)

顺便说一句,我曾尝试使用.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":"Could not locate method: NeptuneGraphTraversal.with([1])"}

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-05-03 23:00:23

此外,对于已经给出的project()示例,如果不能或不想指定属性名称,可以执行类似以下操作:

代码语言:javascript
运行
复制
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
票数 4
EN

Stack Overflow用户

发布于 2019-05-03 16:12:02

您可以尝试对结果执行project操作。

代码语言:javascript
运行
复制
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()
票数 1
EN

Stack Overflow用户

发布于 2019-05-04 04:14:20

您可以用实际的值替换您的EnumMeta dict键。要使用此函数,您需要在valueMap后面添加一个展开()。

代码语言:javascript
运行
复制
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'}]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55963311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档