Developing with Python
In the aerospike-graph
directory, the Python example.py
app demonstrates useful development patterns for connecting to an AGS deployment and adding vertices when building a proof of concept application.
Explore the Python example app
With AGS running, let’s test things out using the Python example app.
For this example app, we see the property graph model in action:
- Vertices represent entities such as users and accounts.
- Edges represent relationships between those entities, such as ownership or money movement. In this example app:
owns
edges connect users to their accounts.Transaction
edges connect accounts to indicate money transfers.
- Both vertices and edges can have properties that describe them.
-
Set up a Python virtual environment:
Terminal window python3 -m venv .venvsource .venv/bin/activate -
Open the Python example app:
Terminal window open ./python/basic/example.py
Let’s pause for a moment and understand Gremlin syntax. A typical Gremlin command is a chain of traversal steps separated by dots, read from left to right.
It starts with a graph traversal source declaration such as:
# Create a GraphTraversalSource to remote serverprint("Connecting to Aerospike Graph Service...")cluster = DriverRemoteConnection("ws://{host}:{port}/gremlin".format(host=HOST, port=PORT), "g")g = traversal().with_remote(cluster)HOST
andPORT
are variables containing the server’s hostname and port. Thews://
protocol specifies WebSocket, which Gremlin uses for communication.The second line creates a
GraphTraversalSource
that points to the remote Gremlin server. Instead of executing queries locally, it sends them to the remote Gremlin server (such as Aerospike Graph running in Docker or another deployment environment).g
is now the entry point for all Gremlin queries.
Following the server connection, you can see various Gremlin queries such as:
user1 = g.add_v("User").property("userId", "U1").property("name", "Alice").property("age", 30).next()Let’s break down each step:
g.add_v("User")
starts a traversal that creates a new vertex labeled"User"
. In this case, we are creating a vertex representing a user entity with three properties. When you explore the rest of the example, you’ll also see the use ofadd_e
, which creates edges (relationships) between vertices..property("userId", "U1")
assigns a propertyuserId
with the value"U1"
to the vertex..property("name", "Alice")
adds another property name with the value"Alice"
..property("age", 30)
adds another propertyage
with the value30
..next()
executes the traversal and returns the vertex that was just added. Gremlin will not execute the traversal unless a terminal step such as.next()
is called.
Read through the rest of the code comments to understand how Gremlin is used to add vertices (
add_v
) and edges (add_e
), filter and list vertices, and delete all vertices/edges (g.V().drop().iterate()
). -
Install the required packages and run the example.
Terminal window python3 -m pip install gremlinpythonpython3 ./python/basic/example.pyExample response Terminal window Connecting to Aerospike Graph Service...Connected to Aerospike Graph Service; Adding Data...Adding some users, accounts and transactionsData written successfully...QUERY 1: Transactions initiated by Alice:Transaction Amount: 200, Receiver Account ID: A2Transaction Amount: 886, Receiver Account ID: A5Transaction Amount: 392, Receiver Account ID: A3Transaction Amount: 886, Receiver Account ID: A4Transaction Amount: 936, Receiver Account ID: A2Transaction Amount: 18, Receiver Account ID: A5Transaction Amount: 18, Receiver Account ID: A4Transaction Amount: 534, Receiver Account ID: A3Transaction Amount: 923, Receiver Account ID: A3Transaction Amount: 334, Receiver Account ID: A4Transaction Amount: 531, Receiver Account ID: A3QUERY 2: Total transaction amounts initiated by users:{'A1': 5658, 'A2': 6755, 'A3': 5802, 'A4': 2997, 'A5': 2200}QUERY 3: Users who transferred greater than 100 to Alice:User: {'name': ['Bob']}User: {'name': ['Bob']}User: {'name': ['Charlie']}User: {'name': ['Charlie']}User: {'name': ['Charlie']}User: {'name': ['Diana']}User: {'name': ['Diana']}User: {'name': ['Eve']}User: {'name': ['Eve']}QUERY 4: Properties of Bob:age: 35name: BobuserId: U2Dropping Dataset.Closing Connection...The duplicate names in Query 3 happen because multiple edges satisfy the query and results are not automatically deduplicated unless
.dedup()
is used.