python - 如何通过 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 或通过其他查询。
解决方案
您的遍历对我来说似乎有效,实际上似乎与 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]]]
推荐阅读
- java - Java:初学者问题:为什么我没有从这段代码中得到数字(1、4、9、16、25、36、49)[已解决]
- python - 如何根据比较两列中的值来组合熊猫数据框中的行?
- mysql - 连接到不特定的数据库名称
- typescript - 没有正确推断泛型的类型?
- json - 将 json 值转换为枚举值
- java - 如何知道 Room 的 onCreate() 回调结束
- sql - 如何使用联合获得不同的记录 postgresql
- reporting-services - SSRS 切换添加列,将报告导出到 Excel 时
- excel - 如何获取形状名称(Groupshapes 名称)
- angular - 允许在 Angular 9 中选择禁用的表单控件