如何通过Java访问Gremlin中路径的顶点? [英] How to access the vertices of a path in Gremlin through Java?

查看:135
本文介绍了如何通过Java访问Gremlin中路径的顶点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java访问Gremlin.我有以下几行代码,用于在已创建的图形中的两个顶点之间创建路径:

I'm using Java to access Gremlin. I have the following lines of code for creating a path between two vertices in an already created graph:

//begin simple path finding
        Vertex fromNode = g.V().has("name", "i2").next();
        Vertex toNode = g.V().has("name", "state1").next();
        ArrayList list = new ArrayList();
        g.V(fromNode).repeat(both().simplePath()).until(is(toNode)).limit(1).path().fill(list);

但是,这时我被卡住了.我可以使用list.toString()打印出列表,并且得到(我相信)路径上的顶点.但是,我现在想自己获取这些顶点,访问并可能更改其数据.例如,如果我无法访问ID 32本身,仅知道v [32]在路径上是不够的.

However, at this point I"m stuck. I can print out the list using list.toString() and I get the vertices that (I believe) are on the path. However, I now want to get those vertices themselves, access and potentially mutate their data. For instance, it isn't enough to know that v[32] is on the path if I can't access the ID 32 itself.

我应该提到我对此有些陌生,所以如果我使用的是过时/错误的做法,请告诉我.

I should mention that I'm a bit new to this so please let me know if I'm using outdated/incorrect practices.

推荐答案

您最终得到一个由Path个对象填充的列表.我认为您正在寻找的是unfold()步骤(参见docs ),因此您可以展开/展平路径中的元素放入列表中.这是一个显示它的Gremlin Console会话:

You ended up with a list filled with Path objects. I think what you're looking for is the unfold() step (see the docs) so you can unroll/flatten out the elements in path to put into the list. Here's a Gremlin Console session showing it:

gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV("name", "i2").as("a").addV("name", "state1").addE("to").from("a").toList()
==>e[4][0-to->2]
gremlin> fromNode = g.V().has("name", "i2").next()
==>v[0]
gremlin> toNode = g.V().has("name", "state1").next()
==>v[2]
gremlin> l = new ArrayList()
gremlin> g.V(fromNode).repeat(both().simplePath()).until(is(toNode)).limit(1).path().fill(l)
==>[v[0],v[2]]
gremlin> l = []; g.V(fromNode).repeat(both().simplePath()).until(is(toNode)).limit(1).path().fill(l)
==>[v[0],v[2]]
gremlin> l = []; g.V(fromNode).repeat(both().simplePath()).until(is(toNode)).limit(1).path().unfold().fill(l)
==>v[0]
==>v[2]
gremlin> l[0].getClass()
==>class org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex
gremlin> l[0].property("name", "j3") // mutate the vertex's name
==>vp[name->i3]
gremlin> g.V(fromNode).valueMap(true).toList()
==>[id:0,name:[j3],label:vertex]

这篇关于如何通过Java访问Gremlin中路径的顶点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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