0

I've tried adding sideEffect or using property with gremlin:

const addSource = this.__.addV(source.label)
      .property(GREMLIN_ID, source.id)
      .property("created_timestamp", new Date().getTime());

    for (const property in source.properties) {
      console.log("Adding source property");
      addSource.property(property, source.properties[property]);
      addSource.sideEffect(this.__.select(source.label).property(property, source.properties[property]));
    }

    addSource.store("source");

    const addDestination = this.__.addV(destination.label)
      .property(GREMLIN_ID, destination.id)
      .property("created_timestamp", new Date().getTime());

    for (const property in destination.properties) {
      addDestination.property(property, destination.properties[property]);
    }

    addDestination.store("destination");

    const addAssociation = this.__.V(source.id).addE(association).to(this.__.V(destination.id)).store("association");

    const v = await this.client
      .getTraversal()
      .V(destination.id)
      .fold()
      .coalesce(this.__.unfold(), addDestination)
      .V(source.id)
      .fold()
      .coalesce(this.__.unfold(), addSource)
      .outE(association)
      .where(this.__.inV().hasId(destination.id))
      .fold()
      .coalesce(this.__.unfold(), addAssociation)
      .select("source", "destination", "association")
      .next();

    console.log( v.value.get("source"));
    console.log(v.value.get("destination"));   
    return {
      source: v.value.get("source"),
      destination: v.value.get("destination"),
      association: v.value.get("association"),
    };

However, this seems to work for new vertices but doesn't update/add properties on an existing vertex with gremlin. Appreciate any pointers.

Edit: I've been able to use sideEffect to add/modify property on an existing vertex but when attempting to do this dynamically when an edge is being created, exception is being thrown:

Error:


ERROR:root:
Received error message '{'requestId': 'e2ec8204-1cb1-4d39-a555-9840653400bb', 'status': {'message': 'The provided object does not have accessible keys: class org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex', 'code': 597, 'attributes': {'stackTrace': 'java.lang.IllegalArgumentException: The provided object does not have accessible keys: class org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex\n\tat org.apache.tinkerpop.gremlin.structure.Column$1.apply(Column.java:51)\n\tat org.apache.tinkerpop.gremlin.process.traversal.lambda.ColumnTraversal.addStart(ColumnTraversal.java:46)\n\tat org.apache.tinkerpop.gremlin.process.traversal.util.TraversalUtil.apply(TraversalUtil.java:108)\n\tat org.apache.tinkerpop.gremlin.process.traversal.util.TraversalUtil.applyNullable(TraversalUtil.java:147)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectOneStep.processNextStart(SelectOneStep.java:62)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep.next(AbstractStep.java:135)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep.next(AbstractStep.java:40)\n\tat org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversal.next(DefaultTraversal.java:230)\n\tat org.apache.tinkerpop.gremlin.process.traversal.util.TraversalUtil.apply(TraversalUtil.java:44)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.util.Parameters.get(Parameters.java:106)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.AddPropertyStep.sideEffect(AddPropertyStep.java:85)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SideEffectStep.processNextStart(SideEffectStep.java:39)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep.hasNext(AbstractStep.java:150)\n\tat org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversal.hasNext(DefaultTraversal.java:222)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.TraversalSideEffectStep.sideEffect(TraversalSideEffectStep.java:48)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SideEffectStep.processNextStart(SideEffectStep.java:39)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep.next(AbstractStep.java:135)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep.next(AbstractStep.java:40)\n\tat org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversal.next(DefaultTraversal.java:230)\n\tat org.apache.tinkerpop.gremlin.process.traversal.util.TraversalUtil.apply(TraversalUtil.java:44)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.util.Parameters.get(Parameters.java:106)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.map.AddEdgeStep.map(AddEdgeStep.java:100)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.map.AddEdgeStep.map(AddEdgeStep.java:48)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.map.ScalarMapStep.processNextStart(ScalarMapStep.java:40)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep.hasNext(AbstractStep.java:150)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.util.ExpandableStepIterator.next(ExpandableStepIterator.java:55)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.map.ScalarMapStep.processNextStart(ScalarMapStep.java:39)\n\tat org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep.hasNext(AbstractStep.java:150)\n\tat org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversal.hasNext(DefaultTraversal.java:222)\n\tat org.apache.tinkerpop.gremlin.server.op.AbstractOpProcessor.handleIterator(AbstractOpProcessor.java:97)\n\tat org.apache.tinkerpop.gremlin.server.op.AbstractEvalOpProcessor.lambda$evalOpInternal$5(AbstractEvalOpProcessor.java:263)\n\tat org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor.lambda$eval$0(GremlinExecutor.java:283)\n\tat java.util.concurrent.FutureTask.run(FutureTask.java:266)\n\tat java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)\n\tat java.util.concurrent.FutureTask.run(FutureTask.java:266)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\n\tat java.lang.Thread.run(Thread.java:748)\n', 'exceptions': ['java.lang.IllegalArgumentException']}}, 'result': {'data': None, 'meta': {}}}'

With results dictionary '{'e2ec8204-1cb1-4d39-a555-9840653400bb': <gremlin_python.driver.resultset.ResultSet object at 0x7fefd1bda9b0>}'
{'error': GremlinServerError('597: The provided object does not have accessible keys: class org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex',)}
    

Query:

%%gremlin
l = [[origin:'usa'],
     [origin:'ind']]
   
g.inject(l).
 unfold().as('properties').
  V('1669333c-8ed1-4955-bacb-2c2e8ef7bfa5').as('vertex').
 sideEffect(select('properties').
           unfold().as('kv').
              select('vertex').
              property(select('kv').by(Column.keys), select('kv').by(Column.values)))
.addE("ANONYMOUS")
.to(
__.inject(l).
 unfold().as('properties1').
  V('00000000000000000000000000000000').as('vertex1').
 sideEffect(select('properties1').
           unfold().as('kv1').
              select('vertex1').
              property(select('kv1').by(Column.keys), select('kv1').by(Column.values)))
)
  • It's a little hard from the question and the code block to understand exactly what you want to do. In general the pattern for updating properties if something already exists is to do it immediately after the `coalesce` step. So that step either finds or creates the vertex or edge, then right afterwards you make any property changes that need making. That way they get applied whether the vertex/edge already existed or was just created. – Kelvin Lawrence Jun 17 '22 at 14:14
  • Thanks Kelvin. Updated the question to include the query. – user17313446 Jun 17 '22 at 16:32
  • Are you wanting to apply these properties to the two vertices or to the starting vertex, and the new edge? – Kelvin Lawrence Jun 20 '22 at 13:57
  • Also, as written, both properties will be added to the starting vertex - or at least it will try to - depending on the backend this may behave with `single`, `list` or `set` cardinality so it is not clear what the intention is here. – Kelvin Lawrence Jun 20 '22 at 14:04
  • I would want to apply properties to both starting and destination vertices - is there a way to do this as a part of one large query ? – user17313446 Jun 21 '22 at 16:05
  • ```this.__.V(source.id).property("origin", "WEB").addE(association).to(this.__.V(destination.id).property("origin", "MOBILE")).store("association")``` – user17313446 Jun 21 '22 at 16:39
  • This doesn't upsert though – user17313446 Jun 21 '22 at 16:39
  • Just circling back, somehow I missed your last reply. Were you able to get your query working? – Kelvin Lawrence Aug 07 '22 at 21:09

0 Answers0