titan - 如何去除两个顶点之间的边?

标签 titan gremlin tinkerpop3

我想删除两个顶点之间的边,所以我在 java tinkerpop3 中的代码如下

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

是否有一种更简单的方法可以通过直接查询该边并删除它来删除两个顶点之间的边?感谢您的帮助。

最佳答案

这是一个如何删除两个顶点之间的边的示例(您只有这些顶点的 id:

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 之间的边。我们可以找到那些:
gremlin> g.V(1).bothE().where(otherV().hasId(2))
==>e[7][1-knows->2]

然后删除它:
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]

如果你有实际的顶点,那么你可以这样做:
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();    
}

关于titan - 如何去除两个顶点之间的边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34589215/

相关文章:

titan - Gremlin:找到两个顶点之间的边的有效方法是什么?

gremlin - 如何使用 tinkerpop gremlin 和 neptune 将远程图导出到 json?

cluster-computing - Titan(不是后端存储)集群是如何工作的?

datastax - 如何在Datastax DSE 5.0 Graph中以简洁的方式通过顶点id进行查询?

Gremlin:查找一组中与另一组有连接的所有节点

graph - Gremlin删除所有顶点

graph - 在两个不相关的顶点之间添加边

titan - tinkerpop/titan 中使用什么机制来确定顶点的绝对顺序?

python - 如何让 Titan 图形数据库与 Python 一起工作?

gremlin - 如何在 Tinkerpop 3 中定义自定义步骤?