Usage
The following example demonstrates how to use the AgensGraph Python driver with psycopg2
to interact with a graph database. You will see how to create a graph, set the graph path, insert a vertex, and query data using Cypher.
import psycopg2
import agensgraph
conn = psycopg2.connect("dbname=test host=127.0.0.1 user=agens")
cur = conn.cursor()
cur.execute("DROP GRAPH IF EXISTS t CASCADE")
cur.execute("CREATE GRAPH t")
cur.execute("SET graph_path = t")
cur.execute("CREATE (:v {name: 'AgensGraph'})")
conn.commit();
cur.execute("MATCH (n) RETURN n")
v = cur.fetchone()[0]
print(v.props['name'])
This example shows the basic workflow for working with AgensGraph:
Establish a connection using
psycopg2
.Create a new graph and set it as the active path.
Insert a vertex with properties.
Query the graph using Cypher.
Access properties of the returned vertex.
You can extend this pattern to create edges, perform complex queries, and manage larger graph structures in your Python applications.
Test
You may run the following command to test AgensGraph Python Driver.
python setup.py test
Before running the command, set the following environment variables to specify which database you will use for the test.
Variable Name |
Meaning |
---|---|
|
database name to connect to |
|
database server host |
|
database server port |
|
database user name |
|
user password |