Warm tip: This article is reproduced from serverfault.com, please click

Gremlin Value Map With Array of Edges

发布于 2020-11-30 17:59:45

I am trying to query a vertex with Node Gremlin and AWS Neptune for its properties and get all vertices connected to it by an 'out' edge and all of their properties as well in a single output. For instance, if I have a 'Baker' vertex with an edge 'bakes' to two 'Cake' vertices, I want to get a 'Baker' object with all his properties as well as an array on him of the two 'Cake' vertices as objects with all their properties.

I know I can effectively do this with a .project but I do not want to have to select for specific properties for any vertices because I want all of their properties.

I am currently working with a query like this: g.V(BakerId).as('Baker').out('bakes').as('Cake').select('Baker','Cake').by(valueMap(true)), but the problem with this is that this query gives me a list of two outputs, one with the Baker and the first Cake and another with the same Baker and the second cake. I want to avoid the redundancy and have it consolidated in a single output with the Baker appearing just once.

I also am hoping for a solution versatile enough (or simple enough to easily extend) to handle going yet a level deeper to do the same with 'out' edges on the 'Cake' vertices e.g. 'Cake' --> 'madeOf' --> 'Ingredient' so I can get a single Baker with each Cake on it just once each with each Ingredient on them just once each, and all with all their properties as well.

Questioner
jupiterjelly
Viewed
0
noam621 2020-12-01 05:39:13

You can use two project steps and use valueMap so you won't need to sespecifiy the properties:

g.V().hasLabel('Baker').
  project('Baker', 'Cake').
    by(valueMap(true)).
    by(out('bakes').
      project('Cake', 'Ingredients').
        by(valueMap(true)).
        by(out('madeOf').valueMap(true).
          fold()).fold())

example: https://gremlify.com/34e9o3r9gag