dimmg
dimmg

Reputation: 3815

Get all vertices and edges as a map in a single query

I am new to gremlin and I'm trying to gather in a map all vertices and edges. This seems a relatively simple operation, however I am struggling finding out the best way to do this.

I've accomplished this by executing 2 separate queries:

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

Is it possible to achieve the above result using a single query?

Thanks

Upvotes: 1

Views: 684

Answers (2)

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14371

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.

Upvotes: 0

stephen mallette
stephen mallette

Reputation: 46206

I'm not sure of the circumstances under which you are doing this, but other than the smallest graphs, this type of query will be massively expensive. That said, there are number of ways that you might do this with a single request:

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

I guess if you had to absolutely had to maintain the structure you had in your comment you could do:

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

Again, these kinds of traversals should not be executed lightly as they represent a full graph scan.

Upvotes: 1

Related Questions