Tutorial: Graph and Java basics
Objectives
By the end of this tutorial, you will be able to:
- Connect to Aerospike Graph Service from a Java application using the Gremlin driver.
- Create vertices and edges to model user accounts and transactions.
- Execute graph traversal queries to analyze transaction patterns.
- Modify the graph data by inserting new vertices, updating properties, and deleting edges.
This tutorial shows you how to use Aerospike Graph Service (AGS) with Java to model and query financial transaction data. You will create a graph of users, accounts, and transactions, then run queries to analyze the relationships between them using the Gremlin Java driver.
Building a financial transaction graph
You will work with a transaction network containing five users, each owning one account, with 50 random transactions between accounts.
The example application uses the Gremlin Java driver to connect to AGS through the standard TinkerPop API, creating two types of vertices:
- User vertices with name, userId, and age properties
- Account vertices with accountId and balance properties
You link users to accounts with ownership edges and represent money transfers with transaction edges.
The Java driver integrates seamlessly with your application, letting you write expressive Gremlin queries with strongly-typed Java code. You’ll use methods like addV, addE, property, and fluent traversal chains.
Querying connected data with Gremlin
Graph databases excel at modeling and querying connected data through traversal operations.
You will execute four query patterns commonly used in fraud detection and financial analysis:
- Finding transactions by user: Traverse from a specific user through their account to all their transactions.
- Calculating transaction totals: Use aggregation with
groupandsumto calculate total amounts per account. - Identifying large transfers: Filter with predicates like
P.gte()to find users who sent significant amounts to a specific recipient. - Retrieving user details: Use
valueMapto fetch detailed properties for individual users.
Each query demonstrates how Gremlin’s declarative syntax lets you navigate relationships efficiently. The Java driver provides type-safe access to vertices, edges, and properties through standard Java objects and collections.
After completing the read-only walkthrough, you’ll extend the example by modifying the graph (adding a merchant vertex, updating loyalty tiers, and cleaning up low-value edges) and then wrap up by profiling a traversal and shutting down the Docker environment.