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
-
Open
java/basic/src/main/java/aerospike/com/Main.javain 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;```javaimport 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
-
Paste the helper method below (place it above the existing
main()method and inside theMainclass).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
Merchantvertex and connects two accounts withpurchased_fromedges. - Mutation 2 overwrites the
loyaltyTierproperty on specific users. - Mutation 3 deletes every
Transactionedge below a threshold, reporting how many edges were dropped. - Verification reads back the new edges and user properties before the dataset is cleared.
- Mutation 1 adds a
-
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
-
Rebuild the project to include the new code.
Terminal window mvn clean install```shelljava -jar target/ags-java-example-1.0-jar-with-dependencies.jar...QUERY 4: Properties of Bob:userId : U2name : Bobage : 35MUTATION 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 -
Verify that the console output shows the three mutation steps followed by the verification logs.