0

I am using a Github example to generate a graph (https://github.com/Azure-Samples/azure-cosmos-db-graph-java-getting-started). And now I want to query it and hold a vertex instance in my hand to traverse further depending on further inputs from the user in a knowledge graph.

Submitting this gremlin query: g.V().hasLabel('schedule').inE().outV().hasLabel('url').as('a').dedup() .where(and(out().hasLabel('schedule').has('name','3'),out() .hasLabel('states').has('name', 'federal'))).select('a')

// Submitting remote query to the server.
ResultSet results = client.submit(query);

CompletableFuture<List<Result>> completableFutureResults = results.all();
List<Result> resultList = completableFutureResults.get();

for (Result result : resultList) {
    System.out.println("My vertex--"+result.getVertex());
    System.out.println("\nQuery result:");
    System.out.println("resultssssss-"+result.toString());
}

How can I do that as right now I am getting a class cast exception as mentioned below:

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to org.apache.tinkerpop.gremlin.structure.Vertex
    at org.apache.tinkerpop.gremlin.driver.Result.getVertex(Result.java:131)
    at GetStarted.Dynamic.main(Dynamic.java:155)
Hyyan Abo Fakher
  • 3,497
  • 3
  • 21
  • 35
Varun Tahin
  • 299
  • 1
  • 2
  • 15
  • Thanks .But this won't help , I want a gremlin query that can give me list of vertices at the end that I can convert it into tinkerpop vertex or otherwise. – Varun Tahin Sep 13 '18 at 06:36

1 Answers1

0

The select('a') seems redundant to me - I think that can be removed. It is selecting an output that is already the current output of where() which is already that vertex at "a" which means your traversal should be:

g.V().hasLabel('schedule').
  in().hasLabel('url').
  dedup().
  where(and(out().hasLabel('schedule').has('name','3'),
            out().hasLabel('states').has('name', 'federal')))

I guess a further optimization would be to do the out() before the and() so you only traverse those edges once:

g.V().hasLabel('schedule').
  in().hasLabel('url').
  dedup().
  where(out().and(has('schedule','name','3'),
                  has('states', 'name', 'federal')))

Now...that returns a Vertex, however, what you had should have also returned a Vertex. At least according to TinkerPop specifications, when doing a select() on a single label you should get back a single object. However, if you do multiple labels then you would get a Map. You can see this demonstrated with TinkerGraph here:

gremlin> g.V().hasLabel('person').as('a').out().as('b').select('a')
==>v[1]
==>v[1]
==>v[1]
==>v[4]
==>v[4]
==>v[6]
gremlin> g.V().hasLabel('person').as('a').out().as('b').select('a','b')
==>[a:v[1],b:v[3]]
==>[a:v[1],b:v[2]]
==>[a:v[1],b:v[4]]
==>[a:v[4],b:v[5]]
==>[a:v[4],b:v[3]]
==>[a:v[6],b:v[3]]

It makes me wonder if CosmosDB has that little nuance covered properly - if not, then that's technically a "bug".

stephen mallette
  • 45,298
  • 5
  • 67
  • 135