简体   繁体   中英

how to print all result from Gremlin traversal

I have a topology, it is aws_vpc<------(composition)------test---(membership)---->location

So I use query

graph.traversal().V().hasLabel("test").or(
                    __.out("membership").hasLabel("location"),
                    __.out("composition").hasLabel("aws_vpc"))

to select it, but how to print all elements' name, I want to output : test, membership, location,composition, aws_vpc.

Is there a way to achieve this?

You've written a traversal that only detects if "test" vertices have outgoing "membership" edges that have adjacent "location" vertices OR outgoing "composition" edges that have adjacent "aws_vpc" vertices, so all that traversal will return is "test" vertices that match that filter. It does not "select" anything more than that. In fact, the or() is immediately satisfied as soon as a single __.out("membership").hasLabel("location") or __.out("composition").hasLabel("aws_vpc") is returned in the order they are provided to or() so you don't even traverse all of those paths (which is a good thing for a filtering operation).

If you want to return all of the data you describe, you need to write your query in such a way so as to traverse it all and transform it into a format to return. A simple way to do this in your case would be to use project() :

g.V().hasLabel('test').
  project('data','memberships', 'compositions').
    by(__.elementMap()).
    by(__.outE("membership").as('e').
          inV().hasLabel("location").as('v').
          select('e','v').
            by(elementMap()).
          fold()).
    by(__.outE("composition").as('e').
          inV().hasLabel("aws_vpc").as('v').
          select('e','v').
            by(elementMap()).
          fold())

This takes each "test" vertex and transforms it to a Map with three keys: "data", "memberships" and "competitions" and then each by() modulator specifies what to do with the current "test" vertex being transformed and places it in the respective key. Note that I chose select() to get the edge and vertex combinations but that could have been a project() step as well if you liked. The key there is to end with fold() so that you reduce the stream of edge data for each "test" vertex to a List of data that can put in the related "memberships" and "compositions" keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM