如何从gremlin返回子图,它是Java的易用格式 [英] how to return subgraph from gremlin that is in an easily consumable format for Java

查看:435
本文介绍了如何从gremlin返回子图,它是Java的易用格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试进行一次遍历并使用Gremlin一次从DSE Graph 5.0中带来很多东西时,我对非常简单的事情感到非常沮丧。

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个具有特定uuid的实体

  • 实体可以为零(见可选)或更多类型

  • 我需要能够返回实体类型

  • 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 :/

我之所以这样做,是因为根据DSE 5.0中的遍历,顶点没有附带填充的 edges()流利的API。因此,要么我陷入多重遍历,要么陷入一个巨大的可怕遍历,这在Java对象中很难反序列化,或者我必须将gremlin查询作为String传递,但不会返回Gremlin Vertex对象,而是返回DSE:(

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?

在此先感谢您的帮助!

推荐答案

如果 Entity:Type 1:n 关系,那么您甚至不需要 optional()

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())

结果将是类型 List< Map< St环,对象>>

更新

在下面的注释中简短的 toList()讨论之后,这是遍历结果而不将整个内容存储在集合中的方法:

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

这篇关于如何从gremlin返回子图,它是Java的易用格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆