How to run multiple gremlin commands as a single transaction?

放肆的年华 提交于 2021-01-28 06:41:59

问题


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();

回答1:


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()...");  



回答2:


You can also use the SessionedClient, which will run all queries in the same transaction upon close().

More information is here: https://docs.aws.amazon.com/neptune/latest/userguide/access-graph-gremlin-sessions.html#access-graph-gremlin-sessions-glv




回答3:


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()...


来源:https://stackoverflow.com/questions/56883983/how-to-run-multiple-gremlin-commands-as-a-single-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!