DEV Community

Chidera Stella Onumajuru
Chidera Stella Onumajuru

Posted on • Updated on

Creating your First Graph Database.

I assume you have PostgreSQL installed from source code, Apache AGE, installed as well AGE viewer installed. If not, use this link, and this link to install PostgreSQL and AGE.

  • First, let's create a Database Cluster using the initdb command.
cd postgresql-11.18   #Enter into the postgres directory
bin/initdb test     #Create a database cluster called test
bin/pg_ctl -D test -l logfile start   #Start the server
bin/createdb testdb   #Create a postgres database
bin/psql testdb   #Enter into the database
Enter fullscreen mode Exit fullscreen mode
  • Having done all that, let us load the extension AGE into our current PostgreSQL session and set the search_path for the current session. This search path determines the order in which PostgreSQL looks for objects (such as tables, functions, and extensions) when they are being referenced in our SQL queries.
CREATE EXTENSION age;
LOAD 'age';
SET search_path = ag_catalog, "$user", public;

Enter fullscreen mode Exit fullscreen mode
  • The syntax of the cypher query is like this.
SELECT * FROM cypher('library', $$ 
                     /* cypher_query here */ 
                     () $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
  • Every query in AGE must be enclosed within the $$ symbol. Let us create a graph, and a vertex without any properties or labels.
SELECT create_graph('library');
SELECT * FROM cypher('library', $$ CREATE (n) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
  • Now let's break down the above queries, To create a vertex I used the CREATE clause, In the above query, I created a graph called library using the create_graph function, and a vertex without any labels or properties.

  • Having done that, let's create a Vertex, with a label and properties.

SELECT * FROM cypher('library', $$ CREATE (n:Book {name : "Purple Hibiscus"}) $$) AS (a agtype);
SELECT * FROM cypher('library', $$ CREATE (n:Book {name : "A Time to Kill"}) $$) AS (a agtype);
SELECT * FROM cypher('library', $$ CREATE (n:Book {name : "If Tomorrow Comes"}) $$) AS (a agtype);
SELECT * FROM cypher('library', $$ CREATE (n:Author {name : "Chimamanda Adichie"}) $$) AS (a agtype);
SELECT * FROM cypher('library', $$ CREATE (n:Author {name : "Sidney Sheldon"}) $$) AS (a agtype);
SELECT * FROM cypher('library', $$ CREATE (n:Author {name : "John Grisham"}) $$) AS (a agtype);
Enter fullscreen mode Exit fullscreen mode
  • In the code above, I created six different Vertices above, and gave them labels of Book and Author respectively, I also gave them different properties as can be seen with the different names they have in the key value pair.

To view all the Vertex in our graph, run the command below.

SELECT * FROM cypher('library', $$
MATCH (v)
RETURN v
$$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode
  • In the query above, we are using the MATCH clause to get all the vertices in our graph library. We assigned this to a variable called v, returned this v variable using the RETURN clause and got the output below.

Image description

  • Now that that is taken care of, Let us create an Edge.
SELECT * FROM cypher('library', $$ MATCH (a:Book), (b:Author)
WHERE a.name = 'Purple Hibiscus' AND b.name = 'Chimamanda Adichie' CREATE (a)-[e:WrittenBy]->(b)RETURN e $$) as (e agtype);
SELECT * FROM cypher('library', $$ MATCH (a:Book), (b:Author)
WHERE a.name = 'A Time to Kill' AND b.name = 'John Grisham'
CREATE (a)-[e:WrittenBy]->(b) RETURN e $$) as (e agtype);
SELECT * FROM cypher('library', $$ MATCH (a:Book), (b:Author)
WHERE a.name = 'If Tomorrow Comes' AND b.name = 'Sidney Sheldon' CREATE (a)-[e:WrittenBy]->(b) RETURN e $$) as (e agtype);
Enter fullscreen mode Exit fullscreen mode
  • Again we are using the MATCH clause to search out the Vertex Book and Author, we assigned them the variables a and b respectively, with theWHERE clause and the AND clause we assessed the properties we are looking for using the a andb variables, then finally we used the CREATE clause to create a relationship(Edge) between these two, We returned our newly created Edge which has the label WrittenBy using the variable e.

  • To view all created Edges run the code below.

SELECT * FROM cypher('library', $$ MATCH p = (v)-[*]->(b) RETURN relationships(p) $$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode
  • This will give the output below.

Image description

To visualize all we have done, start the Apache AGE viewer. I assume you have installed it using this tutorial, and have done the necessary database connections as explained in that tutorial, remember not to stop the server.

npm run start
Enter fullscreen mode Exit fullscreen mode
  • Here is what my view looks like.

Image description

  • Stop the server.
bin/pg_ctl -D test -l logfile stop
Enter fullscreen mode Exit fullscreen mode

References
Official Apache AGE Documentation

Top comments (0)