如何删除两个顶点之间的边? [英] How to remove edge between two vertices?
问题描述
我想删除两个顶点之间的边,所以我在java tinkerpop3中的代码如下
I want to remove edge between two vertices, so my code in java tinkerpop3 as below
private void removeEdgeOfTwoVertices(Vertex fromV, Vertex toV,String edgeLabel,GraphTraversalSource g){
if(g.V(toV).inE(edgeLabel).bothV().hasId(fromV.id()).hasNext()){
List<Edge> edgeList = g.V(toV).inE(edgeLabel).toList();
for (Edge edge:edgeList){
if(edge.outVertex().id().equals(fromV.id())) {
TitanGraph().tx();
edge.remove();
TitanGraph().tx().commit();
return;//Remove edge ok, now return.
}
}
}
}
是否有一种更简单的方法通过直接查询到两个边之间的边来删除该边并将其删除?感谢您的帮助.
Is there a simpler way to remove edge between two vertices by a direct query to that edge and remove it? Thank for your help.
推荐答案
下面是一个如何在两个顶点之间放置边的示例(您只需拥有这些顶点的ID:
Here's an example of how to drop edges between two vertices (where you just have the ids of those vertices:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).bothE()
==>e[9][1-created->3]
==>e[7][1-knows->2]
==>e[8][1-knows->4]
在此示例中,假设我们要在顶点1和顶点2之间放置边.我们可以使用以下命令找到这些边缘:
For purpose of the example, let's say we want to drop edges between vertex 1 and vertex 2. We could find those with:
gremlin> g.V(1).bothE().where(otherV().hasId(2))
==>e[7][1-knows->2]
,然后使用以下命令将其删除:
and then remove it with:
gremlin> g.V(1).bothE().where(otherV().hasId(2)).drop()
gremlin> g.V(1).bothE()
==>e[9][1-created->3]
==>e[8][1-knows->4]
如果您具有实际的顶点,则可以这样做:
If you have the actual vertices, then you could just do:
gremlin> g.V(v1).bothE().where(otherV().is(v2)).drop()
gremlin> g.V(1).bothE()
==>e[9][1-created->3]
==>e[8][1-knows->4]
您可以将函数重写为:
private void removeEdgeOfTwoVertices(Vertex fromV, Vertex toV,String edgeLabel,GraphTraversalSource g){
g.V(fromV).bothE().hasLabel(edgeLabel).where(__.otherV().is(toV)).drop().iterate();
g.tx().commit();
}
这篇关于如何删除两个顶点之间的边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!