I'm using OrientDB 2.2.0 together with TinkerPop 2.6.0 and Java 8.
I have a sample graph which is set up as follows: City ---LOCATED_IN---> Country.
Both City and Country implement com.tinkerpop.frames.VertexFrame.
If I create a FramedGraph I can very easily query for a specific Country and get back a typed Iterable, for example like this:
Iterable<Country> countries = framedGraph.getVertices("countryCode", "NL", Country.class);
This works perfectly.
Now I wanted to achieve the same type of thing but then using Gremlin so I can do more complicated queries. So as a simple test I wanted to test and see if I could get the cities of the Country I fetched with the previous statement using Gremlin.
Vertex nl = countries.iterator().next().asVertex();
GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<>();
pipe.start(nl).inE("LOCATED_IN").outV();
for (Vertex v : pipe) {
System.out.println(v.getProperty("name").toString());
}
Now this works, I get back the following output:
Nijmegen
Nieuw-Vennep
Niewegein
Sittard
Sittard
Noordwijk
Lisse
What I would like to achieve however is to get back City objects from the pipeline instead of Vertex objects (as with the FramedGraph query).
It seems the pipeline is returning the correct types, since if I change the code slightly to this:
Vertex nl = countries.iterator().next().asVertex();
GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<>();
pipe.start(nl).inE("LOCATED_IN").outV();
for (Vertex v : pipe) {
System.out.println(v);
}
I get back:
v(City)[#38:3]
v(City)[#40:3]
v(City)[#37:3]
v(City)[#36:3]
v(City)[#33:4]
v(City)[#35:3]
v(City)[#39:3]
So I'm getting back vertices of type City. But if I try to do this:
Vertex nl = countries.iterator().next().asVertex();
GremlinPipeline<Vertex, City> pipe = new GremlinPipeline<>();
pipe.start(nl).inE("LOCATED_IN").outV();
for (City v : pipe) {
System.out.println(v);
}
I get:
java.lang.ClassCastException: com.tinkerpop.blueprints.impls.orient.OrientVertex cannot be cast to models.City
Also if I try this:
Vertex nl = countries.iterator().next().asVertex();
GremlinPipeline<Vertex, City> pipe = new GremlinPipeline<>();
pipe.start(nl).inE("LOCATED_IN").outV().cast(City.class);
for (City v : pipe) {
System.out.println(v);
}
I get the same class cast exception.
I'm obviously doing something wrong but I can't figure out what. Any pointers would be very welcome!
Thanks in advance, Jonck
Ok, after reading some more I found the answer to my own question. I needed to "frame" the vertices I got back from the pipeline.
So as follows:
Vertex nl = countries.iterator().next().asVertex();
GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<>();
pipe.start(nl).inE("LOCATED_IN").outV();
Iterable<City> cities = framedGraph.frameVertices(pipe, City.class);
for (City city : cities) {
System.out.println(city.getName());
}
And now I get back the output I was expecting. Hope this helps someone.