首页 > 解决方案 > 如何通过 gremlin-python 获取所有边、关联顶点以及各自的 id、标签和属性?

问题描述

我想从我的AWS Neptune DB中获取所有边和关联的顶点。此外,我想要节点和边的 id、标签和属性。
我的数据存储为属性图,我正在使用gremlin-python来查询它。

以下是在 gremlin shell 中执行时提供所需数据的查询。但是,尝试使用 gremlin-python 执行相同的操作会引发错误。

g.V().bothE().otherV().limit(2).path().by(__.valueMap(true))

Python 变体

from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.traversal import T
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

# Define a graph instance
graph = Graph()

# Connect to the neptune instance
try:
    remoteConn = DriverRemoteConnection('wss://<YOUR_NEPTUNE_IP>:8182/gremlin', 'g')
    g = graph.traversal().withRemote(remoteConn)
except:
    print("Couldn't connect successfully with Neptune instance")
    exit()

g.V().bothE().otherV().limit(2).path().by(__.valueMap(True))

错误

GremlinServerError: 498: {"requestId":"...","code":"UnsupportedOperationException","detailedMessage":"org.apache.tinkerpop.gremlin.process.traversal.step.util.ImmutablePath cannot be cast to org.apache.tinkerpop.gremlin.structure.Element"}

有人可以为我提供一种获取所需信息的方法吗?通过成功将上述查询转换为 Python 或通过其他查询。

标签: pythongremlintinkerpopamazon-neptune

解决方案


您的遍历对我来说似乎有效,实际上似乎与 TinkerGraph 一起工作得很好:

gremlin> g.V().bothE().otherV().limit(2).path().by(__.valueMap(true))
==>[[id:1,name:[marko],label:person,age:[29]],[id:9,weight:0.4,label:created],[id:3,name:[lop],lang:[java],label:software]]
==>[[id:1,name:[marko],label:person,age:[29]],[id:7,weight:0.5,label:knows],[id:2,name:[vadas],label:person,age:[27]]]

我希望这能奏效。也许这是海王星的一个错误?请注意,您可以通过 using 获得相同的结果,select()但它有点冗长:

gremlin> g.V().as('a').bothE().as('b').otherV().as('c').limit(2).select('a','b','c').by(valueMap(true))
==>[a:[id:1,name:[marko],label:person,age:[29]],b:[id:9,weight:0.4,label:created],c:[id:3,name:[lop],lang:[java],label:software]]
==>[a:[id:1,name:[marko],label:person,age:[29]],b:[id:7,weight:0.5,label:knows],c:[id:2,name:[vadas],label:person,age:[27]]]

您当然可以通过生成unfold()Map实例select()获得与path()

gremlin> g.V().as('a').bothE().as('b').otherV().as('c').limit(2).select('a','b','c').by(valueMap(true)).map(unfold().select(values).fold())
==>[[id:1,name:[marko],label:person,age:[29]],[id:9,weight:0.4,label:created],[id:3,name:[lop],lang:[java],label:software]]
==>[[id:1,name:[marko],label:person,age:[29]],[id:7,weight:0.5,label:knows],[id:2,name:[vadas],label:person,age:[27]]]

推荐阅读