【问题标题】:How to remove edge between two vertices?如何去除两个顶点之间的边?
【发布时间】:2016-04-07 22:50:04
【问题描述】:

我想去除两个顶点之间的边,所以我在 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.
                }
            }
        }
    }

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

【问题讨论】:

    标签: titan gremlin tinkerpop3


    【解决方案1】:

    这是一个如何在两个顶点之间放置边的示例(您只有这些顶点的 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();    
    }
    

    【讨论】:

    • 谢谢。这对我很有帮助。 Tinkerpop3 是新的,没有太多的例子。如果可能的话,你能给我更多的资源、链接或参考资料,我可以找到更多的例子吗?我需要从示例中学习更多 gremlin 来优化代码。再次感谢您。
    • 更多文档正在开发中,但到目前为止,您只有参考文档,其中包含几乎所有可用步骤的示例。
    • 当我更改为 g.V(fromV).bothE(edgeLabel).where(__.otherV().is(toV)).drop().iterate(); g.tx().commit();
    • 我猜 drop() 不会自动迭代 - 我认为这是一个终止步骤 - 抱歉。
    • 没错,这种语言的问题是事情不会立即发生。高达drop()只是一个pipeline,它只有在你调用iterate()next()时才会执行它们在控制台中显示它,它会自动使用命令,所以就像它们所有的隐藏步骤一样说明
    猜你喜欢
    • 1970-01-01
    • 2012-10-09
    • 2019-12-07
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    相关资源
    最近更新 更多