0

When using the following Gremlin statement to query AWS Neptune, the completableFuture is not returning the desired result. As i tried the following:

graphTraversal.promise(Traversal::hasNext());

Scenario 1:

It works for single vertex g.V('id-1').promise(Traversal::hasNext()) returns true

But, when i use g.V('id-1','id-2').promise(Traversal::hasNext()) it still returns true rather than false since id-2 is not available in db

Scenario 2:

when creating list of edges as follows, how to make the query return false if any of the edges is not created.

CompletableFuture<Boolean> method1() {
    List<Edge> edgeList = new ArrayList();  
    edgeList.add(edge1);// vertex2 -> vertex3`  
    edgeList.add(edge2);// vertex1(not present) -> vertex2  
    edgeList.add(edge3);// vertex3 -> vertex4
    
    GraphTraversal g = null;
    
    loop(edgeList: e) {
        g = graphTraversalQueryMethod(g, e);
    }
    
    return g.promise(Traversal::hasNext);

}

GraphTraversal graphTraversalQueryMethod(GraphTraversal g, Edge e) {
    
    g.V(e.sourceVertexId).addE("EDGE_LABEL").property(e.propKey, e.propValue).to(e.destVertexId);
    
}

Its returning false but, the useCase: (suppose the source / destination vertices of the edge is not present)

  • edge1 is getting created
  • edge2 not created since source vertex1 is missing
  • edge3 also not created even if it has both source and destination vertices available in the db
  • so result is returned as false with creating 1 (edge1) valid edge out of 2 (edge1, edge3)

Is there any option to rollback the whole transaction using gremlin in neptune? to get this query executed after the missing vertex1 got created by some other way.

Or any idea to handle / capture the missing transaction. Please correct me if i need to change the query formation.

Note: At-least i need to capture the edges / any of the transaction is not succeeded on query execution. This is using Java.

Would be helpful, if got suggestion for any best practices to achieve this!

CdVr
  • 323
  • 3
  • 15

1 Answers1

0

I think you have misunderstood the methods.

  1. hasNext() can be used to check if the Vertex is available or not. A simple example is, to check if a vertex with name xyzzyx is available in the DB, you can use this method. It returns a boolean.

    g.V().has('name', 'xyzzyx').hasNext()

  2. next() can be used to get the next n number of results from the traversal. You can use this to get the Vertex or edges. The result will be a Traversal instance, which is a type of Iterator.

    g.V().has('name', 'xyzzyx').next()

  3. iterate() can be used to execute a task and not expect anything as result. Usually used for insert or delete kind of queries.

    g.addVertex(label,'person', 'name', 'xyzzyx').iterate()

  4. toList() can be used to get the result in List.

    g.V().valueMap(true).toList()

There are lot more methods in Gremlin based on your need. There 4 are good starting point.

For transactions, Neptune already maintains transactions for the query.

Neptune opens a new transaction at the beginning of each Gremlin traversal and closes the transaction upon the successful completion of the traversal. The transaction is rolled back when there is an error. Multiple statements separated by a semicolon (;) or a newline character (\n) are included in a single transaction. Every statement other than the last must end with a next() step to be executed. Only the final traversal data is returned.

Manual transaction logic using tx.commit() and tx.rollback() is not supported.

Reference link for NeptuneDB: https://docs.aws.amazon.com/neptune/latest/userguide/access-graph-gremlin-differences.html

codetiger
  • 2,650
  • 20
  • 37
  • Yes, i got that and i referred this [link](https://tinkerpop.apache.org/docs/current/reference/#terminal-steps). But, how to make it work like @Transactional in hibernate. I mean, if can't create an edge, rollback the entire query / traversal execution. As i'm trying in multi-threaded environment using Java's CompletableFuture. Thus, using `promise(Traversal::methodName)` method to execute these terminal methods! How do i log the edges that are not created by whatever reason! – CdVr Aug 05 '20 at 13:30
  • Added response to transaction behaviour in Neptune. – codetiger Aug 06 '20 at 04:16
  • That is fine. My question is how to log the unsuccessful transactions out of 'n' number of submitted transaction. Moreover, the example is for string query submission. I've seen that already. But, as mentioned in question, i'm looking for the java implementation. – CdVr Aug 06 '20 at 06:04