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
then initialize it through the following command
$ bin/psql demodb
Load AGE extension
make sure to load the extension before using it
$ CREATE EXTENSION age;
$ LOAD 'age';
$ SET search_path = ag_catalog, "$user", public;
Create the graph
using the following command
SELECT * FROM ag_catalog.create_graph('trans_network');
here we created a graph with name trans_network
Output:
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
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:
city
------
(0 rows)
city
------
(0 rows)
city
------
(0 rows)
city
------
(0 rows)
then review the names of cities that we have created
SELECT * FROM cypher('trans_network',$$
MATCH (node)
RETURN node.name
$$) as (Name agtype);
Output:
name
----------
"City_A"
"City_B"
"City_C"
"City_D"
(4 rows)
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);
we finished creating our graph now, let's visualize it.
Start AGE-Viewer
# move to age-viewer directory
$ cd age-viewer
$ npm run start
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);
Result:
Top comments (0)