how to return subgraph from gremlin that is in an easily consumable format for Java

874 views Asked by At

I am very frustrated about very simple things when I try to do a single traversal and bring a lot of stuff from DSE Graph 5.0 at once using Gremlin..

In my simplified case I have:

  • 1 entity with specific uuid
  • entity can have zero (see optional) or more types
  • I need to be able to return the entity and the types

What I have so far that works is very ugly :(

List list = g.V().hasLabel("Entity").has("uuid","6708ec6d-4518-4159-9005-9e9d642f157e").as("entity")
        .optional(outE("IsOfType").as("types"))
        .select("entity", "types").toList();
List<Edge> typeEdges = new ArrayList<>();
Vertex entityV = null;
for (Object obj : list) {
    entityV = ((Vertex)((LinkedHashMap) obj).get("entity"));
    Edge typeEdge = ((Edge)((LinkedHashMap) obj).get("types"));
    typeEdges.add(typeEdge);
}

each row in the list has the entity and one of the types :/

I am doing all this because Vertex doesn't come with populated edges() based on the traversal in DSE 5.0 Fluent API. So either I am stuck with multiple traversals or a single huge terrible traversal that is very difficult to deserialize in Java Objects or I have to pass gremlin queries as String but that will not return Gremlin Vertex objects but DSE instead :(

In my less simplified case I want to return multiple entities of the above with their respective types how can this be done?

Finally what is a good approach that will lead to reusable code for custom object mapping of a subgraph with different type of objects?

Thank you in advance for the help!

1

There are 1 answers

9
Daniel Kuppitz On BEST ANSWER

If Entity:Type is a 1:n relationship, then you won't even need optional().

g.V().has("Entity","uuid","6708ec6d-4518-4159-9005-9e9d642f157e").
  project("entity","types").by().by(outE("IsOfType").fold())

The result will be of type List<Map<String, Object>>.

UPDATE

Following the short toList() discussion in the comments below, this is how you can work with a traversal result without storing the whole thing in a collection:

g.V().has("Entity","uuid","6708ec6d-4518-4159-9005-9e9d642f157e")
        .project("entity","types").by().by(outE("IsOfType").fold())
        .forEachRemaining(m -> {
            final Vertex entityV = (Vertex) m.get("entity");
            final List<Edge> typeE = (List<Edge>) m.get("types");
            // whatever ...
        })