DEV Community

Safi
Safi

Posted on • Edited on

Harnessing the Power of Apache AGE with Python: An Overview of the AGE Python

The Apache AGE (A Graph Extension) project is an extension of the PostgreSQL database that provides graph database functionality. AGE is a powerful tool for performing graph-based queries and operations, and in this blog post, we'll explore how the AGE Python driver enables developers to harness this power using the popular Python programming language. We'll cover the driver's dependencies, how it works, and its functionality.

Dependencies

  1. Python 3.6+: The AGE Python driver requires Python 3.6 or newer versions, so make sure your Python installation meets this requirement.
brew install python3 
# or if you already have python installed
brew upgrade python3
Enter fullscreen mode Exit fullscreen mode
  1. psycopg2: This PostgreSQL adapter for Python is essential for connecting to the PostgreSQL database and executing SQL commands. You can install it using pip:
pip install psycopg2
Enter fullscreen mode Exit fullscreen mode
  1. Apache AGE: To use the AGE Python driver, you'll need to have Apache AGE installed and set up within your PostgreSQL database. For detailed instructions on how to do this, consult the official documentation. To use the python driver functionalities, you'll need to install the apache age package for python using:
pip install apache-age-python
Enter fullscreen mode Exit fullscreen mode

How the AGE Python Driver Works

The AGE Python driver works by leveraging the psycopg2 adapter to connect to a PostgreSQL database with the AGE extension installed. The driver facilitates the execution of AGE-specific graph query languages like openCypher and PGQL, enabling developers to perform graph operations using familiar Python syntax.

Functionality

Here are some of the core functionalities offered by the AGE Python driver:

  1. Connecting to the database: The driver allows you to establish a connection to your PostgreSQL database, enabling you to execute graph queries.
import age
conn = age.connect(host="localhost", port="5432", dbname="your_database", user="your_user", password="your_password")
Enter fullscreen mode Exit fullscreen mode

Voila! Now you are utilise Apache Age's functionalities in Python.
You can choose to execute cypher queries on an existing graph or else you can choose to create a new graph using:

age.create_graph(conn, "your_graph_name")
Enter fullscreen mode Exit fullscreen mode

Here's an example of how to execute cypher queries in python and handle their output:

with conn.cursor() as cursor:
    try :
         cursor.execute("""SELECT * from cypher(%s, $$ CREATE (n:Person {name: 'Joe', title: 'Developer'}) $$) as (v agtype); """, (GRAPH_NAME,) )
         cursor.execute("""SELECT * from cypher(%s, $$ CREATE (n:Person {name: 'Smith', title: 'Developer'}) $$) as (v agtype); """, (GRAPH_NAME,))
         cursor.execute("""SELECT * from cypher(%s, $$ 
             CREATE (n:Person {name: 'Tom', title: 'Manager'}) 
             RETURN n
             $$) as (v agtype); """, (GRAPH_NAME,))
         for row in cursor:
             print("CREATED::", row[0])

Enter fullscreen mode Exit fullscreen mode

Make sure to commit your changes when data is inserted or updated.

conn.commit()
Enter fullscreen mode Exit fullscreen mode

And if an exception occurs while executing the cypher queries, you must rollback your changes.

except Exception as ex:
    print(type(ex), ex)
    conn.rollback()
Enter fullscreen mode Exit fullscreen mode

Happy Coding!

Top comments (0)