Gremlin Query to return multiple Result in a ResultSet

May be my understanding of gremlin query is wrong :). I am trying to execute a query from Java client and the query is: g.V().hasLabel('MYLABEL'). Have multiple (say 20) vertices that match the label and the ResultSet just have one Result with the data of all twenty vertices included. I would like to have the ResultSet with 20 Results. What way that I need to rearrange the query. please suggest.

1 answer

  • answered 2018-11-08 11:50 stephen mallette

    Just use fold() as in - you can see my example here:

    gremlin> cluster = Cluster.open()
    ==>localhost/127.0.0.1:8182
    gremlin> client = cluster.connect()
    ==>org.apache.tinkerpop.gremlin.driver.Client$ClusteredClient@51efb731
    gremlin> r = client.submit("g.V().hasLabel('person')").all().get()
    ==>result{object=v[1] class=org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex}
    ==>result{object=v[2] class=org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex}
    ==>result{object=v[4] class=org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex}
    ==>result{object=v[6] class=org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex}
    gremlin> r = client.submit("g.V().hasLabel('person').fold()").all().get()
    ==>result{object=[v[1], v[2], v[4], v[6]] class=java.util.ArrayList}
    

    Note that the downside to fold() in this example is that the result won't be streamed back to the client. You will build the entire list in memory on the server and then it will serialize that list as a single payload. If that list is sufficiently large and you generate enough of such lists you may hit memory/GC issues.