如何删除边并在两个顶点之间添加新边? [英] How to remove an edge and add a new edge between two vertices?
问题描述
我试图放置一条边线,并在两个顶点之间添加一条新边线.我该如何在Tinkerpop3中做到这一点?
I'm trying to drop an edge and add a new edge between two vertices. How do I do that in Tinkerpop3?
def user = g.V().has("userId", 'iamuser42').has("tenantId", 'testtenant').hasLabel('User');
user.outE("is_invited_to_join").where(otherV().has("groupId", 'test123')).drop();
def group = g.V().has("groupId", 'test123').has("tenantId", 'testtenant').hasLabel('Group').next();
user.addEdge("is_member_of", group);
这是我在gremlin shell上遇到的错误:
This is the error I get on gremlin shell:
No signature of method: org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal.addEdge() is applicable for argument types: (java.lang.String, com.thinkaurelius.titan.graphdb.vertices.CacheVertex) values: [is_member_of, v[8224]]
谢谢.
推荐答案
Gremlin控制台教程对此问题进行了一些讨论.您没有在遍历遍历.请考虑以下内容:
The Gremlin Console tutorial discusses this issue a bit. You are not iterating your traversal. Consider the following:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> person = g.V().has('name','marko')
==>v[1]
太好了!人员Vertex
存储在人员"变量中...或者是
Great! The person Vertex
is stored in the "person" variable...or is it?
gremlin> person.class
==>class org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal
显然它不是Vertex
. "person"是Traversal
,控制台会这样看待它并为您进行迭代,这就是为什么您得到"v
Apparently it is not a Vertex
. "person" is a Traversal
, the console sees it as such and iterates it for you which is why you get "v1" in the output. Note that the traversal is now "done":
gremlin> person.hasNext()
==>false
您需要将Traversal
迭代到变量中-在这种情况下,请使用next()
:
You need to iterate the Traversal
into your variable - in this case, using next()
:
gremlin> person = g.V().has('name','marko').next()
==>v[1]
gremlin> person.class
==>class org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex
请注意,由于使用的是现在将Vertex
用作Traversal
的脚本,因此脚本中的行会进一步出现问题,因此此行:
Note that you will have further problems down the line in your script because you are using what will now be a Vertex
as a Traversal
, so this line:
user.outE("is_invited_to_join").where(otherV().has("groupId", 'test123')).drop();
现在将失败,并出现类似的错误,因为Vertex
将没有outE()
.您将需要执行以下操作:
will now fail with a similar error because Vertex
will not have outE()
. You would need to do something like:
g.V(user).outE("is_invited_to_join").where(otherV().has("groupId", 'test123')).drop();
如果您想添加一条边并以相同的遍历将旧的边放下,则可以执行以下操作:
If you would like to add an edge and drop the old one in the same traversal, you could do something like this:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).outE('knows')
==>e[7][1-knows->2]
==>e[8][1-knows->4]
gremlin> g.V(1).as('a').outE().as('edge').inV().hasId(2).as('b').addE('knew').from('a').to('b').select('edge').drop()
gremlin> g.V(1).outE('knew')
==>e[12][1-knew->2]
gremlin> g.V(1).outE('knows')
==>e[8][1-knows->4]
可以说,这可能不如将其分为两个或多个单独的遍历那样可读,但是可以做到.
Arguably, that might not be as readable as breaking it into two or more separate traversals, but it can be done.
这篇关于如何删除边并在两个顶点之间添加新边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!