java 完整子图,如何从gremlin返回子图,它是Java的易用格式

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 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!

解决方案

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

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 typeE = (List) m.get("types");

// whatever ...

})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值