Search code examples
gremlintinkerpop3amazon-neptunejava-client

Is it possible to re-use GraphTraversal object to build query chain?


I know that you can do this to add multiple vertices in a single transaction

g.addV(...).addV(...).addV(...).iterate();

But is there a way to re-use the GraphTraversal object to construct the query chain using a loop to add multiple vertices in a single transaction? This is what I'm trying to do

public static void addVertices(int max) {
        // build the cluster
        //...
        //
        GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster));

        GraphTraversal<Vertex, Vertex> traversal = null;
        for (int j = 0; j < max; j++) {
            traversal = g.addV("account").property(T.id, String.valueOf(j));
        }
        
        if (traversal != null) {
            traversal.iterate();
        }
    }

I've also tried the below, as mentioned here

for (int j = 0; j < max; j++) {
    traversal = g.addV("account").property(T.id, String.valueOf(j)).asAdmin().clone();
}

but both methods didn't work, it only inserted one vertex.

I'm using gremlin-java client to insert into AWS Neptune.


Solution

  • You can keep adding to a Traversal right up until the terminal step like iterate is used. So for example

    Traversal t = g.addV("test") ;
    t.addV("another-one");
    t.addV("one-more");
    t.iterate();