DEV Community

Muhammad Zahid
Muhammad Zahid

Posted on

Starting Out With Apache AGE

Apache AGE is an extension for PostgreSQL that enables users to leverage a graph database on top of the existing relational databases. AGE is an acronym for A Graph Extension and is inspired by Bitnine's AgensGraph, a multi-model database fork of PostgreSQL. The basic principle of the project is to create a single storage that handles both the relational and graph data model so that the users can use the standard ANSI SQL along with openCypher, one of the most popular graph query languages today.

You can find more info on Apache AGE on their Website or Github Repository

Lets Start by Creating a New Graph named family_tree.

SELECT create_graph('family_tree');
Enter fullscreen mode Exit fullscreen mode

With this statement we have created an empty graph named family tree. Now lets populate our graph.

SELECT * FROM cypher('family_tree', $$ 
CREATE(:Person {
name:'Andrew James',
year_born:1987,
year_died:2020 })
$$) as (person agtype);
Enter fullscreen mode Exit fullscreen mode

In our graph family tree we have added a person with the properites: name, year_born and year_died

Now we would add another person:

SELECT * FROM cypher('family_tree', $$ 
CREATE(:Person {
name:'Hannah James',
year_born:1987,
year_died:2020 })
$$) as (person agtype);
Enter fullscreen mode Exit fullscreen mode

In our graph family tree we have added a person with the properites: name, year_born and year_died

Now to Display our data we use the command:

SELECT * FROM cypher('family_tree', $$ 
MATCH(v) 
RETURN properties(v) 
$$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode

Image description
Now Lets add a relationship between these two persons:

 SELECT * from cypher(
'family_tree',
$$
    MATCH (a:Person), (b:Person) WHERE a.name='Andrew James' AND b.name='Hannah James' CREATE (a)-[e:Married_To { from :2010, to:2013}]->(b) Return e
$$
) as (e agtype);
Enter fullscreen mode Exit fullscreen mode

In the above command we are adding a relationship between the two persons named Andrew James and Hannah James with named Married_To and with properties: From, To.

Now to Display the relationship we use the following command:

SELECT * from cypher(
'family_tree',
$$
    MATCH (a:Person)-[e:Married_To]-(b:Person) Return a.name,label(e),b.name
$$
) as (a agtype, e agtype, b agtype);
Enter fullscreen mode Exit fullscreen mode

Image description

This was a simple example to adding vertices and an edge between those vertices.

Top comments (0)