DEV Community

Ahmed Mohamed
Ahmed Mohamed

Posted on

Get more familiar with Apache AGE and AGE-Viewer with an Example

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

$ bin/createdb demodb
Enter fullscreen mode Exit fullscreen mode

then initialize it through the following command

$ bin/psql demodb
Enter fullscreen mode Exit fullscreen mode

Load AGE extension

make sure to load the extension before using it

$ CREATE EXTENSION age;
$ LOAD 'age';
$ SET search_path = ag_catalog, "$user", public;
Enter fullscreen mode Exit fullscreen mode

Create the graph

using the following command

SELECT * FROM ag_catalog.create_graph('trans_network');
Enter fullscreen mode Exit fullscreen mode

here we created a graph with name trans_network

Output:

NOTICE:  graph "trans_network" has been created
 create_graph 
--------------

(1 row)
Enter fullscreen mode Exit fullscreen mode

Create Nodes

Now we will create cities (nodes) with adding some properties for them (i.g. name)
using the following

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);
Enter fullscreen mode Exit fullscreen mode

Output:

city 
------
(0 rows)

 city 
------
(0 rows)

 city 
------
(0 rows)

 city 
------
(0 rows)

Enter fullscreen mode Exit fullscreen mode

then review the names of cities that we have created

SELECT * FROM cypher('trans_network',$$
        MATCH (node)
        RETURN node.name

$$) as (Name agtype);
Enter fullscreen mode Exit fullscreen mode

Output:

   name   
----------
 "City_A"
 "City_B"
 "City_C"
 "City_D"
(4 rows)

Enter fullscreen mode Exit fullscreen mode

Relations between Cities

Now we add some relations between the cities

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);
Enter fullscreen mode Exit fullscreen mode

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

Start AGE-Viewer

# move to age-viewer directory
$ cd age-viewer
$ npm run start
Enter fullscreen mode Exit fullscreen mode

then go to localhost:3000

write queries

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

$$) as (V1 agtype, R agtype, V2 agtype);
Enter fullscreen mode Exit fullscreen mode

graph query

Result:

graph visualization

Ref:
install ApacheAGE and AGE-Viewer

Apache AGE’s documentation

Top comments (0)