简体   繁体   中英

How to copy edges from one vertex to a new vertex in Gremlin

Background: I tried to use this question as a baseline but because I'm using Cosmos and had a slightly different scenario I wasn't able to make it work.

I want to know if it is possible to copy all the edges (in and out) from one vertex to a new vertex in CosmosDb Gremlin. The reason being is that I am using the username as the partition key eg) User-User123 and in Cosmos DB this cannot be changed once set.

The idea would be that if a user wanted to change their username, from User123 to User123db,

  • a new vertex would be created with a new partition key of User-User123db.
  • all the edges from User-User123 would point to User-User123db
  • the Vertex User-User123 would then be deleted.

Using the tinkerpop graph below, how would it be possible to create a new vertex with a name of marko2 and point all the in and out edges from marko to the new marko2 vertex and then remove the old marko.

Thanks to anyone that can help =)

Here are the available Gremlin steps that can be used with CosmosDB

在此输入图像描述

I guess the problem is the usage of addE(<traversal>) ..? At least that's what's failing in a pure TinkerPop 3.2 environment. Since there's no way to set the edge label dynamically in TP 3.2, you'll have to know all possible edge labels, coming in and going out from the user vertex, and handle each one separately.

Based on the original traversal from the linked answer:

g.V(4).as('source').
  addV().
    property(label, select('source').label()).as('clone').
  sideEffect(                                                // copy vertex properties
    select('source').properties().as('p').
    select('clone').
      property(select('p').key(), select('p').value())).
  sideEffect(                                                // copy knows out-edges
    select('source').outE('knows').as('e').
    select('clone').
    addE('knows').as('eclone').
      to(select('e').inV()).
    select('e').properties().as('p').                        // copy knows out-edge properties
    select('eclone').
      property(select('p').key(), select('p').value())).
  sideEffect(                                                // copy knows out-edges
    select('source').outE('knows').as('e').
    select('clone').
    addE('knows').as('eclone').
      to(select('e').inV()).
    select('e').properties().as('p').                        // copy knows out-edge properties
    select('eclone').
      property(select('p').key(), select('p').value())).
  sideEffect(                                                // copy created out-edges
    select('source').outE('created').as('e').
    select('clone').
    addE('created').as('eclone').
      to(select('e').inV()).
    select('e').properties().as('p').                        // copy created out-edge properties
    select('eclone').
      property(select('p').key(), select('p').value())).
  sideEffect(                                                // copy knows in-edges
    select('source').inE('knows').as('e').
    select('clone').
    addE('knows').as('eclone').
      from(select('e').outV()).
    select('e').properties().as('p').                        // copy knows in-edge properties
    select('eclone').
      property(select('p').key(), select('p').value()))

So you'll basically just add one sideEffect step per edge label (per direction).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM