Getting Started with Apache AGE: A Comprehensive Guide (Part 2)
Tags: apacheage, postgres, database
Published: 2023-04-27 22:06:03

<div>
In the [previous part of this series](https://dev.to/titoausten/getting-started-with-apache-age-a-comprehensive-guide-part-1-39nj), 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.
</div>
**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');
```
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;
```
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](https://dev.to/titoausten/the-graph-analytics-revolution-understanding-the-dynamic-tool-driving-modern-technology-5ff9).
```
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Tito", bornin: "Warri", tribe: "Igbo"}) $$) AS (a agtype);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Tobe", bornin: "Warri", tribe: "Igbo"}) $$) AS (a agtype);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Tuoyo", bornin: "Warri", tribe: "Itsekiri"}) $$) AS (a agtype);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Austen", bornin: "Windsor"}) $$) AS (a agtype);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "David", bornin: "Benin", tribe: "Bini"}) $$) AS (a agtype);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Ikechukwu", bornin: "Benin", tribe: "Igbo"}) $$) AS (a agtype);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Mariela", bornin: "Mexico City"}) $$) AS (a agtype);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Ayomide", bornin: "Benin", tribe: "Yoruba"}) $$) AS (a agtype);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:Person {name: "Dotun", bornin: "Windsor", tribe: "Yoruba"}) $$) AS (a agtype);
```
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);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:City {name: "Benin"}) $$) AS (a agtype);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:City {name: "Windsor"}) $$) AS (a agtype);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:City {name: "Mexico City"}) $$) AS (a agtype);
```
We can also add nodes with label `Tribe`:
```
SELECT * FROM cypher('test',  $$ CREATE (n:Tribe {name: "Igbo"}) $$) AS (a agtype);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:Tribe {name: "Bini"}) $$) AS (a agtype);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:Tribe {name: "Yoruba"}) $$) AS (a agtype);
```
```
SELECT * FROM cypher('test',  $$ CREATE (n:Tribe {name: "Itsekiri"}) $$) AS (a agtype);
```
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);
``` 
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);
```
**Querying the graph**
<div>
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);
```
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);
```
This returns all `Person` nodes whose property `bornIn` matches the `name` in `City` nodes.
</div>
**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](https://age.apache.org)
[AGE GitHub](https://github.com/apache/age)