Gremlin: Rollback the query if an exception occurs - gremlin

I am trying to submit a batch like operation for creating multiple vertices and edges in the same query.
g.addV('os').property('name', 'linux').as('linux').
addV('os').property('name', 'windows').as('windows').
addV('os').property('name', 'mac').as('mac').
addE('competitor').from('linux').to('UNEXISTING OS'). # fail here
addE('competitor').from('linux').to('windows').
addE('competitor').from('windows').to('mac').
addE('competitor').from('linux').to('mac').
iterate()
The query is constructed to intentionally fail, however all vertices before the failing line are being created.
Is it possible to achieve a kind of transaction for the whole query? So that if one subquery fails, it should rollback the ones that were previously executed.
Thanks!

The query could not be executed in the Gremlin Console using TinkerGraph,
as per TinkerPop documentation, there isn't support for transactions for built-in TinkerGraph object.
But, as cygri pointed out, AWS Neptune offers support for transactions (see here), that can be executed under the form of original query from OP or by separating queries by a semicolon (;) or a newline character (\n)
g.addV('os').property('name', 'linux').next();
g.addV('os').property('name', 'windows').next();
g.addE('competitor').from('1101').to('1102')

You can also use Gremlin Sessions; create a sessioned-connection and it'll rollback queries in case of an error.

Related

Azure cosmosDB gremlin - how to update vertex property with another vertex's property

On an Azure cosmosDB gremlin instance,
I have 2 vertices A and B linked by and edge E.
Both vertices has a 'name' property.
I'd like to run a query which will take A's name and put it in B
when I run
g.V("AId").as("a").oute().inv().hasLabel('B').property("name",select('a').values('name'))
I get the following error :
GraphRuntimeException ExceptionMessage : Gremlin Query Execution Error: Cannot create ValueField on non-primitive type GraphTraversal.
It looks like the select operator is not correctly used.
Thank you for your help
EDITED based on discussion in comments
You have oute and inv in lower case. In general, the steps use camelCase naming, such as outE and inV (outside of specific GLVs), but in the comments it was mentioned that CosmosDB will accept all lower case step names. Assuming therefore, that is not the issue here, the query as written looks fine in terms of generic Gremlin. The example below was run using TinkerGraph, and uses the same select mechanism to pick the property value.
gremlin> g.V(3).as("a").outE().inV().has('code','LHR').property("name",select('a').values('city'))
==>v[49]
gremlin> g.V(49).values('name')
==>Austin
What you are observing may be specific to CosmosDB and it's probably worth contacting their support folks to double check.

How to handle Transaction (Rollback and Commit) in Gremlin - AWS Neptune

How to handle Transaction (Rollback and Commit) in Gremlin - AWS Neptune?
In a request, I have two details, <student and List[Subject]>
1. Create student vertex with properties
2. Create Edge b/w class & student
3. Create a list of subjects vertex in for loop & edge b/w subject and student.
The document says, Multiple statements separated by a semicolon (;) or a newline character (\n) are included in a single transaction.
How to handle for loop in a single transaction in GremlinPython?
Any suggestion/Help?
There are 3 main ways to send a Gremlin query to a server. One is as plain text. In this case you can use semicolons between queries and they are treated as a single transaction. The second is to use a Gremlin client driver that supports sending queries as bytecode. In this case each query sent is a transaction. The third is to use Gremlin Sessions. In this case you create a session and then send one or more queries. The whole session is treated as a single transaction and only committed when you close the session. In this third case you can have code and queries intermixed as needed.
All of that said, you really don't need to use a for loop, you can just send everything in one go. Here is a simple example:
g.addV('root').property('data',9).as('root').
addV('node').property('data',5).as('b').
addV('node').property('data',2).as('c').
addV('node').property('data',11).as('d').
addV('node').property('data',15).as('e').
addV('node').property('data',10).as('f').
addV('node').property('data',1).as('g').
addV('node').property('data',8).as('h').
addV('node').property('data',22).as('i').
addV('node').property('data',16).as('j').
addE('left').from('root').to('b').
addE('left').from('b').to('c').
addE('right').from('root').to('d').
addE('right').from('d').to('e').
addE('right').from('e').to('i').
addE('left').from('i').to('j').
addE('left').from('d').to('f').
addE('right').from('b').to('h').
addE('left').from('c').to('g')

Gremlin OLAP traversal query error regarding local star-graph

I'm trying to execute an OLAP traversal on a query that needs to check if a vertex has a neighbour of certain type.
i keep getting
Local traversals may not traverse past the local star-graph on GraphComputer
my query looks something like:
g.V().hasLabel('label1').
where(_.out().hasLable('label2'))
I'm using the TraversalVertexProgram.
needless to say, when running the same query in oltp mode there is no problem
is there a way to execute such logic?
That is limitation of TinkerPop OLAP GraphComputer. It operate on 'star-graph' objects. The vertex and connected edges only. It uses message passing engine inside. So you have to rewrite you query.
Option 1: start from label2 and go t label1. This should return the same result
g.V().hasLabel('label2').in().hasLabel('label1')
Option2: try to use unique edge labels and check edge label instead
g.V().hasLabel('label1').where(_.outE('label1_label2'))

Azure CosmosDb Gremlin API, Clone vertex, Compilation Error

Based on this answer provided by daniel-luppitz, I am trying to clone a vertex in Azure CosmosDb but I am getting the following error:
Compilation Error: Unable to bind to method 'property', with arguments of type: (GraphTraversal,GraphTraversal)
The query:
IGraphTraversalSource g = coreClient.CreateTraversalSource();
ITraversal query = g.V(new PartitionKeyIdPair(pk, id)).As("source")
.AddV("clone").Property("partitionKey", pk).As("clone")
.SideEffect(__.Select<User>("source").Properties<String>().As("p").Select<User>("clone")
.Property(__.Select<object>("p").Key(), __.Select<string>("p").Value<string>()))
If I change the key and value traversals
.Property(__.Select<object>("p").Key(), __.Select<string>("p").Value<string>()
to constant values then the query works
.Property("test", "test")
Any idea how to achieve this in Azure CosmosDb?
I'm not sure which TinkerPop version is currently supported by Cosmos DB, but after skimming through the docs, I would say it's something close to 3.2.5. The 3.2 line did not support dynamic keys/values, that was added somewhere along the 3.3 line.
Thus, the only way to do that in Cosmos DB would be to split the query. Get the values you need and then submit follow-up queries based on the gathered values. Obviously, this won't perform very well will probably increase your usage costs dramatically, but I can't think of another way of doing it using old Gremlin versions (considering that lambdas are another thing that's not supported in Cosmos DB).

Cannot access specific vertex by ID, using TinkerGraph in Gremlin Console

I cannot select a specific vertex, by executing g.V(3640).valueMap(true).unfold(). Any command which contains an ID between the parentheses in the g.V() command does not seem to work.
This is what I did:
I'm new to Graph databases and experimenting with the Gremlin console. I started by creating an instance:
graph = TinkerGraph.open()
g=graph.traversal()
and loading sample data by importing a .graphml database file:
g.io(graphml()).readGraph('/full/path/to/air-routes-latest.graphml')
which seemed to work fine because a count gives a nice result back
gremlin> g.V().count()
==>3642
Unfortunately the following does not work:
gremlin> g.V(3640).valueMap(true).unfold()
Which I think is odd, because by executing the following
gremlin> g.V()
==>v[3640]
==>v[2306]
...
the ID does seem to exist. Any ideas why I cannot access a specific ID? I tried different commands but g.V() seems to work fine, and g.V(3640) does not. Is it because I use TinkerGraph instead of a Gremlin database, of what might be the problem?
EDIT:
It seems that my id's were saved as strings, because g.V("2").valueMap(true).unfold() does give me results.
I think you likely have an issue with the "type" of the identifier. I suspect that if you do:
g.V(3640L)
that you will get the vertex you want. By default, TinkerGraph handles id equality with equals() so if you try to find an integer when the id is a long it will act like it's not there. You can modify that default if you like with an IdManager configuration discussed here. Note that this is also discussed in more detail in Practical Gremlin.

Resources