3

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.

  • few more details:

From Console.

[query result as run from gremlin console][1]

gremlin> client.submit("g.V().hasLabel('PERSON')")

==>result{object=v[11] class=org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex} ==>result{object=v[13] class=org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex} ==>result{object=v[15] class=org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex}

From Java Client Query -> g.V().hasLabel('PERSON')

The result -> result{object={@type=g:List, @value=[{@type=g:Vertex, @value={id={@type=g:Int64, @value=11}, label=PERSON, properties={AGE=[{@type=g:VertexProperty, @value={id={@type=g:Int64, @value=12}, value={@type=g:Int32, @value=11}, label=AGE}}]}}}, {@type=g:Vertex, @value={id={@type=g:Int64, @value=13}, label=PERSON, properties={AGE=[{@type=g:VertexProperty, @value={id={@type=g:Int64, @value=14}, value={@type=g:Int32, @value=12}, label=AGE}}]}}}, {@type=g:Vertex, @value={id={@type=g:Int64, @value=15}, label=PERSON, properties={AGE=[{@type=g:VertexProperty, @value={id={@type=g:Int64, @value=16}, value={@type=g:Int32, @value=13}, label=AGE}}]}}}]} class=java.util.LinkedHashMap}

Sony Joseph
  • 189
  • 2
  • 13

1 Answers1

2

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.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • -> What I expect to get is moe number of Results in the resultset instead of a whole big list of vertices in a single Map. So if it works like in your first query, it is great. But, the problem is, from a gremlin console I too get similar resp and not when I use a java client. probably due to groovy vs java thing. – Sony Joseph Nov 08 '18 at 12:44
  • I'm sorry, but i don't follow your comment. There should be no difference in this case with Groovy vs Java in this case - the behavior should be the same. If I've not answered your question I think you will need to clarify further. – stephen mallette Nov 08 '18 at 12:59
  • your understanding of my comment is correct. Ideally, with both groovy and Java the query resulstset must be of same kind. But unfortunately, I get them as different. I ran the query from both java and console to the same remote DB. – Sony Joseph Nov 09 '18 at 04:12
  • when I ran from groovy I got a resultset with multiple results but from Java I got one result with entire data in it. – Sony Joseph Nov 09 '18 at 04:13
  • `client.submit()` always returns one `ResultSet` object - same for Groovy and Java. However, `ResultSet` is an `Iterable`. So, in the first example above that uses `submit()` you get one `ResultSet` with 4 items in it. You would get the same in Java. In the Gremlin Console however, it notices the `ResultSet` is an `Iterable` and shows you what's inside. It does the same for the second `submit()` example, but in that case we used `fold()` and thus created a result with only one `List` so when the Console iterates the `ResultSet` you only get one result. – stephen mallette Nov 09 '18 at 11:25
  • Please see this tutorial for more information on result iteration - http://tinkerpop.apache.org/docs/current/tutorials/the-gremlin-console/#result-iteration – stephen mallette Nov 09 '18 at 11:25
  • Thanks @stephen mallet in trying to understand the issue. I have edited the question to add more info. If you see that you would observer that the only result obtained in Java client have all the items clubbed into a LinkedHashMap. – Sony Joseph Nov 12 '18 at 03:45
  • Please show your code for how you are connecting and sending your query using java. – stephen mallette Nov 12 '18 at 11:38
  • the SpringBean configurations:\n Bean public Cluster cluster() { return Cluster.build(addr).serializer(Serializers.DEFAULT_RESULT_SERIALIZER) //.credentials(username, password) .enableSsl(false) .port(port).create(); } @Bean Client client() { return cluster().connect(); } and the code connecting this is ->\n CompletableFuture future = client.submitAsync(query); – Sony Joseph Nov 12 '18 at 12:32
  • ah! you're using GraphSON 1.0. `DEFAULT_RESULT_SERIALIZER` should probably be deprecated (or changed to not use that). I'd forgotten that was even an available field. Don't set the `serializer` option and it should default to GraphSON 3.0 and then your results will be the same as mine (which I assume is what you want). If you are stuck on GraphSON 1.0 for some reason, I'm not aware of a workaround for doing what you want....that format was a bit limited. – stephen mallette Nov 12 '18 at 12:41
  • good - could you please accept the answer since things are settled now. cheers – stephen mallette Nov 12 '18 at 16:37