gremlinpython-返回id和标签为字符串 [英] gremlinpython - return id and label as string
问题描述
我在Python 3.7.2上使用gremlinpython 3.4.1,并且在获得顶点/边缘响应时,它为id提供<T.id: >
,为标签提供<T.label: 3>
.我如何获取它以在响应中提供id和label的字符串值呢?我的目的是获取输出并生成JSON
I'm using gremlinpython 3.4.1 on Python 3.7.2, and when get vertex/edge responses it is providing <T.id: >
for id and <T.label: 3>
for the label. How would I get it to provide the string value for id and label in the response instead? My aim is take the output and generate JSON
输出:
python3 stackoverflow.py
[{'name': ['USA'], 'parentname': ['USA'], 'shortname': ['US'], <T.id: 1>: 'country-us', 'parentid': ['country-us'], <T.label: 3>: 'Country'}]
代码:
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)
例如:
BTW - I have attemped to use .with_(WithOptions.ids)
for example:
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])"}
推荐答案
此外,对于已经给出的project()
示例,如果您不能或不想指定属性名称:
Additionally, to the project()
example that was already given, you could do something like the following if you can't or don't want to specify the property names:
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
这篇关于gremlinpython-返回id和标签为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!