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

此生再无相见时 提交于 2019-12-18 09:48:23

问题


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:


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 ...
        })


来源:https://stackoverflow.com/questions/41304450/how-to-return-subgraph-from-gremlin-that-is-in-an-easily-consumable-format-for-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!