Get more familiar with Apache AGE and AGE-Viewer with an Example
Tags: apacheage, postgres, postgressql
Published: 2023-05-22 12:28:43

In a previous article we had a walk-through how to install ApacheAGE and AGE-Viewer in a detailed steps.

now, let's take an example how to use ApacheAGE extension in postgres to build a graph and then visualize it using AGE-viewer

## Create a database
create a database called `demodb` using postgres 
```sh
$ bin/createdb demodb
```

then initialize it through the following command
```sh
$ bin/psql demodb
```

## Load AGE extension
make sure to load the extension before using it
```sh
$ CREATE EXTENSION age;
$ LOAD 'age';
$ SET search_path = ag_catalog, "$user", public;
```
## Create the graph
using the following command 
```sh
SELECT * FROM ag_catalog.create_graph('trans_network');
```
here we created a graph with name `trans_network`

Output:
```sh
NOTICE:  graph "trans_network" has been created
 create_graph 
--------------
 
(1 row)
```
## Create Nodes
Now we will create cities (nodes) with adding some properties for them (i.g. name)
using the following
```sh
SELECT * FROM cypher('trans_network',$$
	Create(:City{
		name: 'City_A'
	
	})

$$) as (city agtype);

SELECT * FROM cypher('trans_network',$$
	Create(:City{
		name: 'City_B'
	
	})

$$) as (city agtype);


SELECT * FROM cypher('trans_network',$$
	Create(:City{
		name: 'City_C'
	
	})

$$) as (city agtype);

SELECT * FROM cypher('trans_network',$$
	Create(:City{
		name: 'City_D'
	
	})

$$) as (city agtype);
```
Output:
```sh
city 
------
(0 rows)

 city 
------
(0 rows)

 city 
------
(0 rows)

 city 
------
(0 rows)

```

then review the names of cities that we have created 
```sh
SELECT * FROM cypher('trans_network',$$
        MATCH (node)
        RETURN node.name
        
$$) as (Name agtype);
```
Output:
```sh
   name   
----------
 "City_A"
 "City_B"
 "City_C"
 "City_D"
(4 rows)

```

## Relations between Cities
Now we add some relations between the cities

```sh
SELECT * FROM cypher('trans_network',$$
	MATCH (a:City), (b:City)
	WHERE a.name = 'City_A' AND b.name = 'City_B'
	CREATE (a)-[e:Cost_20]->(b)
	RETURN e

$$) as (rel agtype);

SELECT * FROM cypher('trans_network',$$
	MATCH (a:City), (b:City)
	WHERE a.name = 'City_A' AND b.name = 'City_C'
	CREATE (a)-[e:Cost_15]->(b)
	RETURN e

$$) as (rel agtype);

SELECT * FROM cypher('trans_network',$$
	MATCH (a:City), (b:City)
	WHERE a.name = 'City_B' AND b.name = 'City_C'
	CREATE (a)-[e:Cost_20]->(b)
	RETURN e

$$) as (rel agtype);

SELECT * FROM cypher('trans_network',$$
	MATCH (a:City), (b:City)
	WHERE a.name = 'City_C' AND b.name = 'City_D'
	CREATE (a)-[e:Cost_10]->(b)
	RETURN e

$$) as (rel agtype);

SELECT * FROM cypher('trans_network',$$
	MATCH (a:City), (b:City)
	WHERE a.name = 'City_D' AND b.name = 'City_A'
	CREATE (a)-[e:Cost_40]->(b)
	RETURN e

$$) as (rel agtype);
```

we finished creating our graph now, let's visualize it.

## Start AGE-Viewer
```sh
# move to age-viewer directory
$ cd age-viewer
$ npm run start
```
then go to `localhost:3000`

### write queries

```sh
SELECT * FROM cypher('trans_network',$$
	MATCH (V1)-[R]->(V2)
	RETURN V1, R, V2

$$) as (V1 agtype, R agtype, V2 agtype);
```
![graph query](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rweqg70yy5a052ffg02.png)


Result:

![graph visualization](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mc5m3k2uxp48m5zykfe4.png)   


Ref:
[install ApacheAGE and AGE-Viewer](https://dev.to/ahmedmohamed/install-postgresql-apache-age-from-source-23e0)

[Apache AGE’s documentation](https://age.apache.org/age-manual/master/index.html)