Skip to content

Modify the graph

You have already explored the read-only queries in the Java example. Now modify the graph by adding a helper method that runs three Gremlin write operations (addV/addE, property, and drop) and then verifies the results.

Prepare Main.java

  1. Open java/basic/src/main/java/aerospike/com/Main.java in your editor. The top of the file already looks like:

    package aerospike.com;
    import org.apache.tinkerpop.gremlin.driver.Cluster;
    import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
    import org.apache.tinkerpop.gremlin.process.traversal.P;
    import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
    import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
    import org.apache.tinkerpop.gremlin.structure.Vertex;
    import java.time.LocalDate;
    import java.time.ZoneOffset;
    import java.time.format.DateTimeFormatter;
    import java.util.Random;
    import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;
    ```java
    import java.util.LinkedHashMap;
    import java.util.Map;

    The rest of the required imports (GraphTraversalSource, Vertex, P, etc.) are already present in the starter file.

Add the mutation helper

  1. Paste the helper method below (place it above the existing main() method and inside the Main class).

    private static void applyMutations(GraphTraversalSource g) {
    System.out.println("\nMUTATION 1: Created merchant vertex and connected accounts A1 and A3 with purchased_from edges.");
    Vertex merchant = g.addV("Merchant")
    .property("merchantId", "M1")
    .property("name", "Pacific Outfitters")
    .property("industry", "Retail")
    .next();
    Map<String, Integer> purchases = new LinkedHashMap<>();
    purchases.put("A1", 640);
    purchases.put("A3", 430);
    for (Map.Entry<String, Integer> entry : purchases.entrySet()) {
    Vertex account = g.V().hasLabel("Account").has("accountId", entry.getKey()).next();
    g.addE("purchased_from")
    .from(account)
    .to(merchant)
    .property("amount", entry.getValue())
    .property("currency", "USD")
    .iterate();
    }
    System.out.println("MUTATION 2: Set loyaltyTier=PLATINUM for Alice and loyaltyTier=GOLD for Bob.");
    g.V().hasLabel("User").has("name", "Alice").property("loyaltyTier", "PLATINUM").iterate();
    g.V().hasLabel("User").has("name", "Bob").property("loyaltyTier", "GOLD").iterate();
    System.out.println("MUTATION 3: Removing Transaction edges with amount < 200.");
    long removed = g.E().hasLabel("Transaction").has("amount", P.lt(200)).count().next();
    if (removed > 0) {
    g.E().hasLabel("Transaction").has("amount", P.lt(200)).drop().iterate();
    System.out.printf("Removed %d Transaction edge(s) below the threshold.%n", removed);
    } else {
    System.out.println("No Transaction edges fell below the threshold.");
    }
    System.out.println("\nVERIFY MUTATIONS: Reading back new edges and loyalty tiers before cleanup.");
    var merchantEdges = g.E().hasLabel("purchased_from")
    .valueMap("amount", "currency").toList();
    var loyalty = g.V().hasLabel("User").has("loyaltyTier")
    .valueMap("name", "loyaltyTier").toList();
    System.out.println("Merchant edges: " + merchantEdges);
    System.out.println("Loyalty tiers: " + loyalty);
    }
    • Mutation 1 adds a Merchant vertex and connects two accounts with purchased_from edges.
    • Mutation 2 overwrites the loyaltyTier property on specific users.
    • Mutation 3 deletes every Transaction edge below a threshold, reporting how many edges were dropped.
    • Verification reads back the new edges and user properties before the dataset is cleared.
  2. Call the helper near the end of main().

    Replace everything inside public static void main(final String[] args) { ... } with the following block. Keep your existing query calls (the ones that print results) where the comment indicates:

    public static void main(final String[] args) {
    final Cluster cluster = BUILDER.create();
    try {
    final GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster));
    // existing example code...
    query1(g);
    query2(g);
    query3(g);
    query4(g);
    applyMutations(g);
    g.V().drop().iterate();
    System.out.println("Dropping Dataset.");
    } catch (Exception e) {
    System.err.println("Failed to run Java example: " + e.getMessage());
    return;
    } finally {
    try {
    System.out.println("Closing Connection...");
    cluster.close();
    } catch (Exception closeException) {
    System.err.println("Failed to Close Connection: " + closeException);
    }
    }
    }

    This preserves the example behavior on success and ensures the connection cleanup runs even when an unexpected error occurs.

Rebuild and run

  1. Rebuild the project to include the new code.

    Terminal window
    mvn clean install
    ```shell
    java -jar target/ags-java-example-1.0-jar-with-dependencies.jar
    ...
    QUERY 4: Properties of Bob:
    userId : U2
    name : Bob
    age : 35
    MUTATION 1: Created merchant vertex and connected accounts A1 and A3 with purchased_from edges.
    MUTATION 2: Set loyaltyTier=PLATINUM for Alice and loyaltyTier=GOLD for Bob.
    MUTATION 3: Removing Transaction edges with amount < 200.
    Removed 6 Transaction edge(s) below the threshold.
    VERIFY MUTATIONS: Reading back new edges and loyalty tiers before cleanup.
    Merchant edges: [{amount=[430], currency=[USD]}, {amount=[640], currency=[USD]}]
    Loyalty tiers: [{name=[Alice], loyaltyTier=[PLATINUM]}, {name=[Bob], loyaltyTier=[GOLD]}]
    Dropping Dataset.
    Closing Connection...
    Example response
  2. Verify that the console output shows the three mutation steps followed by the verification logs.

Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?