How to remove an edge and add a new edge between two vertices?

前端 未结 1 1186
我在风中等你
我在风中等你 2021-01-18 21:25

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(\"t         


        
相关标签:
1条回答
  • 2021-01-18 21:33

    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]
    

    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
    

    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
    

    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
    

    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();
    

    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.

    0 讨论(0)
提交回复
热议问题