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
With AGS running, let’s test things out using the Python example app.
-
Set up a Python virtual environment:
Terminal window python3 -m venv .venvsource .venv/bin/activate -
Open the Python example app:
Terminal window open ./python_example/python_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 like:Terminal window GraphTraversalSource g = traversal().withRemote(conn);g.V().has('property', 'value').out('edgeLabel').values('anotherProperty')On the second line, you can see our Gremlin command. Let’s break down each step:
g.V()
uses the traversal source declared previously asg
and then usesV()
to start with all vertices..has('property', 'value')
filters by a property..out('edgeLabel')
traverses outgoing edges..values('anotherProperty')
gets property values.To reiterate, each step processes or filters the graph elements from the previous step.
This will make more sense when looking at a real example. In the example app, lines 15-17 show the following:
python_example.py g.add_v('foo').\property('company', 'aerospike').\property('scale', 'unlimited').iterate()g.add_v('foo')
adds a vertex labeled"foo"
and.property()
is used twice to append two properties for this vertex.The
python_example.py
begins by importingDriverRemoteConnection
andtraversal
creating a connection with the AGS server. Read through the code comments to understand how Gremlin commands connect to the server, add a vertex, update a property, and delete a vertex. -
Install the required packages and run the example.
Terminal window python3 -m pip install gremlinpython async_timeoutpython3 ./python_example/python_example.pyExample response Terminal window Values:['aerospike', 'unlimited']Updated:['aerospike', 'infinite']