DEV Community

Tito Osadebey
Tito Osadebey

Posted on • Edited on

Getting Started with Apache AGE: A Comprehensive Guide (Part 2)

In the previous part of this series, we looked at the introduction to Apache AGE as a graph analysis tool and followed up with steps to installing and setting it up. In continuation, we’ll be creating a graph, and querying it using Cypher queries.

Creating a graph in AGE
Following up with the steps in part 1, we can create a graph by running the statement:

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

To check if the graph was created successfully, run the statement that shows a table of graphs created in the database:

SELECT * FROM ag_graph;
Enter fullscreen mode Exit fullscreen mode

After the graph is created, we need to add nodes and/or edges (otherwise called relationships) to the graph using cypher queries. For a brief introduction to graphs and its analysis, click here.

SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Tito", bornin: "Warri", tribe: "Igbo"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Tobe", bornin: "Warri", tribe: "Igbo"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Tuoyo", bornin: "Warri", tribe: "Itsekiri"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Austen", bornin: "Windsor"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "David", bornin: "Benin", tribe: "Bini"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Ikechukwu", bornin: "Benin", tribe: "Igbo"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Mariela", bornin: "Mexico City"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Ayomide", bornin: "Benin", tribe: "Yoruba"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Dotun", bornin: "Windsor", tribe: "Yoruba"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode

9 nodes were created with the statements above. These nodes have a label Person and varying properties for each node such as name, bornin and tribe.
Furthermore, we can add more nodes with label City to the graph to make it robust.

SELECT * FROM cypher('test',  $$ CREATE (n:City {name: "Warri"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:City {name: "Benin"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:City {name: "Windsor"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:City {name: "Mexico City"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode

We can also add nodes with label Tribe:

SELECT * FROM cypher('test',  $$ CREATE (n:Tribe {name: "Igbo"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:Tribe {name: "Bini"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:Tribe {name: "Yoruba"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM cypher('test',  $$ CREATE (n:Tribe {name: "Itsekiri"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode

Now let’s create some edges (relationships) between the nodes.
Create an edge between nodes with label Person and City:

SELECT * FROM cypher('test',  $$ MATCH (a:Person), (b:City) WHERE a.bornIn = b.name CREATE (a)-[r:BORN_IN]->(b) RETURN r $$) AS (r agtype);
Enter fullscreen mode Exit fullscreen mode

For an edge between nodes with label Person and Tribe:

SELECT * FROM cypher('test',  $$ MATCH (a:Person), (b:Tribe) WHERE a.tribe = b.name CREATE (a)-[r:IS]->(b) RETURN r $$) AS (r agtype);
Enter fullscreen mode Exit fullscreen mode

Querying the graph

To find out persons born in certain cities, we can query the graph with the statement:

SELECT * FROM cypher('test',  $$ MATCH (a:Person)-[r]- (b:Tribe) WHERE a.tribe = b.name RETURN a, r, b $$) AS (a agtype, r agtype, b agtype);
Enter fullscreen mode Exit fullscreen mode

This will return all Person nodes whose property tribe matches the name in Tribe nodes.

SELECT * FROM cypher('test',  $$ MATCH (a:Person)-[r]- (b:City) WHERE a.bornIn = b.name RETURN a, r, b $$) AS (a agtype, r agtype, b agtype);
Enter fullscreen mode Exit fullscreen mode

This returns all Person nodes whose property bornIn matches the name in City nodes.

Conclusion
In this post, we created a graph using AGE, added nodes and edges to the graph and executed cypher queries to extract important data from the graph. This is just a simple illustration of how AGE can be effectively utilized for data acquisition and extraction in a network. Next up, we would be looking at how to visualize graphs and the results from queries using AGE Viewer.

For more information on Apache AGE, visit:
AGE website
AGE GitHub

Top comments (0)