Search code examples
gremlintinkerpop3azure-cosmosdb-gremlinapi

Count edges by vertex in Gremlin


Brand new to Gremlin. Have a graph like this Vertices = People, Items; Edges = Likes, Purchases.

I'm trying to count the number of purchases everyone has made with output as:

Greg, 4

Naomi, 0

Kyunghee, 2

Thank you,


Solution

  • Using this simple graph

     g.addV('Person').property('name','Greg').as('g').
       addV('Person').property('name','Naomi').as('n').
       addV('Person').property('name','Kyunghee').as('k').
       addV('Item').property('name','book').as('book').
       addV('Item').property('name','TV').as('tv').
       addV('Item').property('name','laptop').as('laptop').
       addV('Item').property('name','car').as('car').
       addE('Purchased').from('g').to('book').
       addE('Purchased').from('g').to('tv').
       addE('Purchased').from('g').to('laptop').
       addE('Purchased').from('g').to('car').
       addE('Purchased').from('k').to('book').
       addE('Purchased').from('k').to('tv')              
    

    One way to calculate the purchases is to just group the people using their name and their purchases.

    gremlin>  g.V().hasLabel('Person').
    ......1>    group().
    ......2>      by('name').
    ......3>      by(out('Purchased').count())
    
    ==>[Naomi:0,Kyunghee:2,Greg:4]