AT

Trying to find a Vertex using a variable injected earlier in the traversal

Dduffrey9/11/2023
I am trying to add a series of vertices and edges to an existing graph. The newly created Vertex will be the to Vertex. From business logic, I will know the ID of the from Vertex for the new Edge . I am able to create the new Vertex without any issue, but when I am having trouble grabbing the from Vertex to create the edge. Is there a way to do this/what am I doing wrong?
g.inject(["myID": "2", "parentID": "1", "properties": ["key1":"value1"]]).
addV("MyVertex").as("newVertex").
property(id, select("myID")).
sideEffect(
select("properties").unfold().as("kv")
select("newVertex").
property(
select("kv").by(keys),
select("kv").by(values),
)
).
AddE("MyEdge").to("newVertex").from(V(select("parentID")))
g.inject(["myID": "2", "parentID": "1", "properties": ["key1":"value1"]]).
addV("MyVertex").as("newVertex").
property(id, select("myID")).
sideEffect(
select("properties").unfold().as("kv")
select("newVertex").
property(
select("kv").by(keys),
select("kv").by(values),
)
).
AddE("MyEdge").to("newVertex").from(V(select("parentID")))
Sspmallette9/12/2023
This pattern is hard to implement for situations where you need to dynamically use the values in the Map to look-up an existing Vertex. While there is probably a way to make it work, this pattern is a bit antiquated given the introduction of mergeV() and mergeE() which generally simplifies that query to something more like:
gremlin> g.mergeV([(T.id): "2", key1: 'value1']).
......1> mergeE([(T.label): "MyEdge", (to): "2", (from): "1"])
==>e[1][1-MyEdge->2]
gremlin> g.mergeV([(T.id): "2", key1: 'value1']).
......1> mergeE([(T.label): "MyEdge", (to): "2", (from): "1"])
==>e[1][1-MyEdge->2]
Is there any chance you could use this approach instead or are you using a version of Gremlin prior to 3.6.0?
Dduffrey9/12/2023
Yeah, I can do that. I am on 3.6.2. I think the reason that I wanted to use the add functions instead of the merge function is that I wanted it to fail if there was a collision. Do you know if there is a way to set the OnMatch to throw an error? In this case I could do an addV and then know that the edge doesn't exist so the mergeE could only be a create. But if the merge functions are going to be more broadly capable it would still be good to know if there is a way to detect a collision from the merge
Sspmallette9/12/2023
you can use fail() step - the example from the docs shows its usage with onCreate but you could just as easily apply it to onMatch:
gremlin> g.mergeV([(T.id): 1]).
......1> option(onCreate, fail("vertex did not exist")).
......2> option(onMatch, [modified: 2022])
fail() Step Triggered
======================================================================================================================================================================
Message > vertex did not exist
Traverser> false
Bulk > 1
Traversal> fail("vertex did not exist")
Parent > TinkerMergeVertexStep [mergeV([(T.id):((int) 1)]).option(Merge.onCreate,__.fail("vertex did not exist")).option(Merge.onMatch,[("modified"):((int) 2022)])]
Metadata > {}
======================================================================================================================================================================
gremlin> g.mergeV([(T.id): 1]).
......1> option(onCreate, fail("vertex did not exist")).
......2> option(onMatch, [modified: 2022])
fail() Step Triggered
======================================================================================================================================================================
Message > vertex did not exist
Traverser> false
Bulk > 1
Traversal> fail("vertex did not exist")
Parent > TinkerMergeVertexStep [mergeV([(T.id):((int) 1)]).option(Merge.onCreate,__.fail("vertex did not exist")).option(Merge.onMatch,[("modified"):((int) 2022)])]
Metadata > {}
======================================================================================================================================================================
https://tinkerpop.apache.org/docs/current/reference/#mergevertex-step
Dduffrey9/12/2023
Awesome. Thanks

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Does Gremlin support API for CRUD operations?Currently using g.V() for read and g.addV() for write.Individual Vertex per property or Vertex with grouped propertiesI'm building an identity graph that also stores User profile data - things like email address, phoneFilter out empty resultsgremlin> g.V().hasLabel('metadata').valueMap() ==>{} ==>{} ==>{oncall_roster=[oncall_schedule]} HowQuestion on running queries in windows env.I get an error `RuntimeError: Event loop is closed`, but after troubleshooting I notice that my scriPropertiesStep.hashcode() not always uniqueAs background... We're working with Gremlin (groovy) to write queries against an in memory graph modConditionally update one vertex property when another property matches a certain provided valuehttps://stackoverflow.com/questions/76971695/update-vertex-properties-when-property-a-matches-properTrying to run a local version for a test, what is the correct serializer?Windows machine, local host. I can't find the Running this, https://github.com/bricaud/gremlin-servadding edges to multiple vertices at onceHey all. Working with tinkerpop on Cosmos Gremlin DB which is horrific. Wondering is there anyway aDoes .math() always return a Double?I have the following query, how can I get the result as a Long instead of a Double? In context, I wTrying to update a property value based on another propertyI have a query that looks something like the following g.V('9999').hasLabel('someLabel').propertiesCasting issue with Gremlin JavaI wrote the following query and I can't get it to compile, tried a ton of casting but it just isn't valueMap and MultivaluesI was going to use the recipe from @KelvinL 's book to return lists only when the property has multiAWS Neptune bulk load notificationsI wonder if anyone has knowledge of a way to receive a notification event(s) for bulk loading. RightVertexProgram filter graph before terminationI have a VertexProgram that operates on vertices of type A and B. B vertices are "below" A verticesStraightforward way to render a force directed graph svg/pngI was wondering if there is a "simple" way in java for me to take a GraphTraversal and render a forcCan't do explain() traversal step using Gremlin-Python ..Hi I just started messing around in gremlin-python this week, so likely to be doing something wrong