Skip to content

Tutorial: Graph and Python basics

Objectives

By the end of this tutorial, you will be able to:

  • Connect to Aerospike Graph Service from a Python 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 upserting vertices, updating edge properties, and pruning relationships.

This tutorial shows you how to use Aerospike Graph Service (AGS) with Python 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 Python 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 Python driver (gremlinpython) to connect to AGS, 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 Python driver integrates seamlessly with your application, letting you write expressive Gremlin queries using Pythonic syntax. You’ll use methods like add_v(), add_e(), property_(), has_label(), and value_map() with fluent method chaining that feels natural in Python code.

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:

  1. Finding transactions by user: Traverse from a specific user through their account to all their transactions.
  2. Calculating transaction totals: Use aggregation with group() and sum() to calculate total amounts per account.
  3. Identifying large transfers: Filter with predicates like P.gte() to find users who sent significant amounts to a specific recipient.
  4. Retrieving user details: Use value_map() to fetch detailed properties for individual users.

Each query demonstrates how Gremlin’s declarative syntax lets you navigate relationships efficiently. The Python driver provides intuitive access to vertices, edges, and properties through standard Python dictionaries and lists.

After reviewing the read-only queries, you’ll extend the example by modifying the graph (upserting additional users, tagging high-value transactions, and pruning old edges) so you can see how updates, inserts, and deletes work in the same traversal pipeline. The tutorial concludes with a quick wrap-up where you profile a traversal and shut down the Docker environment you started earlier.

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?