Search code examples
graphgremlingraph-databasestinkerpop3

Group paths by starting node to print - Gremlin


I am fairly new to the Gremlin language and still learning its basics. I would like to group my outputs from my source node.

As an example, take the ToyGraph example, created using graph = TinkerFactory.createModern(). Suppose for each software, I would like to calculate the mean age of its creaters, I would have to do something like g.V('software').in('created').mean(), however this would give me the mean of all creaters of all softwares, how would I get an output of the form: {softA : 31.0, softB : 40.6, ...}. I have tried the group clause and aggregrate, but not really sure how to go about it.


Solution

  • This is a great example of when you would want to look at using a project() step. A project() step will create a map of values with the specified labels starting from the current location in the graph. In this case we find all software vertices and then project() the name and ages out from each software vertex. I put an example of this below which also includes all the age values it found to show that it is calculating the mean() correctly.

    g.V().hasLabel('software').project('software', 'ages', 'mean_age').
    by().
    by(__.in('created').values('age').fold()).
    by(__.in('created').values('age').mean())
    
    ==>[software:v[3],ages:[29,32,35],mean_age:32.0]
    ==>[software:v[5],ages:[32],mean_age:32.0]