Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple gremlin commands as a single transaction?

In Amazon Neptune I would like to run multiple Gremlin commands in Java as a single transactions. The document says that tx.commit() and tx.rollback() is not supported. It suggests this - Multiple statements separated by a semicolon (;) or a newline character (\n) are included in a single transaction.

Example from the document show that Gremlin is supported in Java but I don't understand how to "Multiple statements separated by a semicolon"

GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster));

    // Add a vertex.
    // Note that a Gremlin terminal step, e.g. next(), is required to make a request to the remote server.
    // The full list of Gremlin terminal steps is at https://tinkerpop.apache.org/docs/current/reference/#terminal-steps
    g.addV("Person").property("Name", "Justin").next();

    // Add a vertex with a user-supplied ID.
    g.addV("Custom Label").property(T.id, "CustomId1").property("name", "Custom id vertex 1").next();
    g.addV("Custom Label").property(T.id, "CustomId2").property("name", "Custom id vertex 2").next();

    g.addE("Edge Label").from(g.V("CustomId1")).to(g.V("CustomId2")).next();
like image 891
Gilad Avatar asked Oct 28 '25 12:10

Gilad


2 Answers

The doc you are referring is for using the "string" mode for query submission. In your approach you are using the "bytecode" mode by using the remote instance of the graph traversal source (the "g" object). Instead you should submit a string script via the client object

Client client = gremlinCluster.connect();
client.submit("g.V()...iterate(); g.V()...iterate(); g.V()...");  
like image 63
Ankit Gupta Avatar answered Oct 30 '25 06:10

Ankit Gupta


Gremlin sessions

Java Example

After getting the cluster object,

String sessionId = UUID.randomUUID().toString();
Client client = cluster.connect(sessionId);
client.submit(query1);
client.submit(query2);
.
.
.
client.submit(query3);
client.close();

When you run .close() all the mutations get committed.

You can also capture the response from the Query reference.

List<Result> results = client.submit(query);
results.stream()...
like image 36
HIMANSHU GOYAL Avatar answered Oct 30 '25 07:10

HIMANSHU GOYAL