在单个查询中将所有顶点和边线获取为地图

时间:2019-04-08 10:52:48

标签: python gremlin amazon-neptune

我是gremlin的新手,我正试图在地图上收集所有顶点和边线。这似乎是一个相对简单的操作,但是我正在努力寻找最佳的方法。

我通过执行2个单独的查询来完成此操作:

{
    "nodes": g.V().valueMap().toList(),
    "edges": g.E().valueMap().toList()
}

是否可以通过单个查询获得以上结果?

谢谢

2 个答案:

答案 0 :(得分:1)

我不确定您在什么情况下会执行此操作,但是除了最小的图形以外,这种查询类型将非常昂贵。也就是说,您可以通过多种方法对单个请求执行此操作:

g.V().
  project('v','edges').
  by(valueMap()).
  by(outE().valueMap().fold())

我想,如果您必须绝对维护评论中的结构,则可以:

g.V().store('vertices').by(valueMap()).
  outE().store('edges').by(valueMap()).
  cap('vertices','edges')

同样,此类遍历不能轻易执行,因为它们代表了完整的图形扫描。

答案 1 :(得分:0)

As Stephen mentioned this is going to be an expensive query on a large graph. However, I was able to run the following Python code using Gremlin-Python and Neptune with no issues.

mymap = (
g.V().
  project('v','edges').
  by(__.valueMap()).
  by(__.outE().valueMap().fold())).toList()
print(mymap)  

I was not able to run the other query (below) even from the Gremlin console until I either increased the amount of data that can be accepted by the client or limited the query result. Even with a small graph of mine your second query is blowing the default 64K result frame size that the Gremlin console is configured for. That right there shows that this is an expensive query! That said, I would use the query above rather than the store and cap form for the sake of simplicity but in any large graph in general this is a bit of an anti pattern as it has the potential to return huge amounts of data. As to the error from your second query I was able to run it on the console when I added a limit step but still saw Python issues.

# Notice the added limit which allows it to work from the console
g.V().store('vertices').by(__.valueMap()).
        outE().limit(10).store('edges').by(__.valueMap()).
        cap('vertices', 'edges').toList() 

Even with a limit(10) I still see the same error as you when using the query from Python. It looks like the Python client is unable to process the result of the query. This needs more investigation. For now you can just use the project version of the query.