Skip to content

Commit

Permalink
Merge pull request #4 from aerospike/nel/java-quick-start
Browse files Browse the repository at this point in the history
Add Java Quickstart Example
  • Loading branch information
ishaanb authored Jan 29, 2025
2 parents cf3934e + ebf3e5f commit 6e902ac
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.venv

.vscode/
.DS_Store
.DS_Store

examples/*/target
26 changes: 26 additions & 0 deletions examples/java-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## To run Java example

#### Reqs: Java 11+

```
cd ags_java_example
mvn clean install -q
java -jar target/ags-java-example-1.0-jar-with-dependencies.jar
```
Example output:
```
Connected to Aerospike Graph Service; Adding Data...
Adding some users, accounts and transactions.
Data written successfully...
QUERY 1: Transactions initiated by Alice:
Transaction Amount: 200, Receiver Account ID: A2
Transaction Amount: 722, Receiver Account ID: A1
Transaction Amount: 282, Receiver Account ID: A5
...
...
...
Dropping Dataset.
Closing Connection...
```
64 changes: 64 additions & 0 deletions examples/java-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>aerospike.com</groupId>
<artifactId>ags-java-example</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<tinkerpop.version>3.7.2</tinkerpop.version>
<junit.version>4.13.1</junit.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-core</artifactId>
<version>${tinkerpop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>${tinkerpop.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>aerospike.com.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
168 changes: 168 additions & 0 deletions examples/java-example/src/main/java/aerospike/com/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package aerospike.com;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;

import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;
import org.apache.tinkerpop.gremlin.process.traversal.P;


import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Random;


public class Main {
// Define the host and port for connecting to the Aerospike Graph service
private static final String HOST = "localhost";
private static final int PORT = 8182;

// Build the Cluster object with connection details
private static final Cluster.Builder BUILDER = Cluster.build()
.addContactPoint(HOST) // Add the contact point (host)
.port(PORT) // Specify the port
.enableSsl(false); // Disable SSL for this example (use true in production if needed)

public static void main(final String[] args) {
// Create a Cluster object for connecting to the graph database
final Cluster cluster = BUILDER.create();
// Initialize a GraphTraversalSource to interact with the graph
final GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster));

System.out.println("Connected to Aerospike Graph Service; Adding Data...");

// Add users, accounts, and transactions
System.out.println("Adding some users, accounts and transactions");

// Add Users
final Vertex user1 = g.addV("User").property("userId", "U1").property("name", "Alice").property("age", 30).next();
final Vertex user2 = g.addV("User").property("userId", "U2").property("name", "Bob").property("age", 35).next();
final Vertex user3 = g.addV("User").property("userId", "U3").property("name", "Charlie").property("age", 25).next();
final Vertex user4 = g.addV("User").property("userId", "U4").property("name", "Diana").property("age", 28).next();
final Vertex user5 = g.addV("User").property("userId", "U5").property("name", "Eve").property("age", 32).next();

// Add Accounts
final Vertex account1 = g.addV("Account").property("accountId", "A1").property("balance", 5000).next();
final Vertex account2 = g.addV("Account").property("accountId", "A2").property("balance", 3000).next();
final Vertex account3 = g.addV("Account").property("accountId", "A3").property("balance", 4000).next();
final Vertex account4 = g.addV("Account").property("accountId", "A4").property("balance", 2000).next();
final Vertex account5 = g.addV("Account").property("accountId", "A5").property("balance", 6000).next();

// Link Users to Accounts
g.addE("owns").from(user1).to(account1).property("since", "2020").iterate();
g.addE("owns").from(user2).to(account2).property("since", "2021").iterate();
g.addE("owns").from(user3).to(account3).property("since", "2022").iterate();
g.addE("owns").from(user4).to(account4).property("since", "2023").iterate();
g.addE("owns").from(user5).to(account5).property("since", "2024").iterate();

// Add Transactions
g.addE("Transaction")
.from(account1).to(account2)
.property("transactionId", "T1")
.property("amount", 200)
.property("type", "debit")
.property("timestamp", convertTimestampToLong("2023-01-15"))
.iterate();

g.addE("Transaction")
.from(account2).to(account1)
.property("transactionId", "T2")
.property("amount", 150)
.property("type", "credit")
.property("timestamp", convertTimestampToLong("2023-01-16"))
.iterate();

// Add Transactions
final Random random = new Random();
for (final int i = 1; i <= 50; i++) {
final Vertex fromAccount = g.V().hasLabel("Account").sample(1).next();
final Vertex toAccount = g.V().hasLabel("Account").sample(1).next();
final int amount = random.nextInt(1000) + 1; // Random amount between 1 and 1000
final String transactionId = "T" + i;
final String type = random.nextBoolean() ? "debit" : "credit";
final String timestamp = String.format("2025-%02d-%02d", random.nextInt(11) + 1,random.nextInt(28) + 1); // Random date in January 2025

g.addE("Transaction")
.from(fromAccount).to(toAccount)
.property("transactionId", transactionId)
.property("amount", amount)
.property("type", type)
.property("timestamp", convertTimestampToLong(timestamp))
.iterate();
}
System.out.println("Data written successfully...");

// Query Example 1: Find all transactions initiated by a specific user
System.out.println("\nQUERY 1: Transactions initiated by Alice:");
g.V().has("User", "name", "Alice")
.out("owns")
.outE("Transaction")
.as("transaction")
.inV()
.values("accountId")
.as("receiver")
.select("transaction", "receiver")
.by("amount")
.by()
.forEachRemaining(result -> {
System.out.println("Transaction Amount: " + result.get("transaction") + ", Receiver Account ID: " + result.get("receiver"));
});
// Query Example 2: Aggregate total transaction amounts for each user
System.out.println("\nQUERY 2: Total transaction amounts initiated by users:");
g.V().hasLabel("Account")
.group()
.by("accountId")
.by(
__.outE("Transaction")
.values("amount")
.sum()
)
.forEachRemaining(System.out::println);
System.out.println("\nQUERY 3: Users who transferred greater than 100 to Alice:");
g.V().has("User", "name", "Alice")
.out("owns")
.inE("Transaction")
.has("amount", P.gte(100))
.outV()
.in("owns")
.valueMap("name")
.forEachRemaining(result -> {
System.out.println("User: " + result);
});

// Query Example 4: List all properties of a specific user
System.out.println("\nQUERY 4: Properties of Bob:");
final Vertex bob = g.V().has("User", "name", "Bob").next();
bob.properties().forEachRemaining(property -> {
System.out.println(property.key() + " : " + property.value());
});


// Clean up
g.V().drop().iterate();
System.out.print("Dropping Dataset.");
try {
System.out.println("Closing Connection...");
cluster.close();
} catch (Exception e) {
System.err.println("Failed to Close Connection: " + e);
}
}

/**
* Converts a date string (e.g., "2023-01-15") into a long representing epoch milliseconds.
* @param date The date string in "yyyy-MM-dd" format.
* @return The epoch timestamp in milliseconds as a long.
*/
private static long convertTimestampToLong(final String date) {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// Parse the date string to LocalDate
final LocalDate localDate = LocalDate.parse(date, formatter);
// Convert LocalDate to epoch milliseconds
return localDate.atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli();
}
}

0 comments on commit 6e902ac

Please sign in to comment.