Search code examples
graphtransactionsgremlinamazon-neptunegremlinpython

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?


Solution

  • 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')