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 …


This content originally appeared on DEV Community and was authored by Chidera Stella Onumajuru

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 demo     #Create a database cluster called demo
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
  • 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 them in our SQL queries.
CREATE EXTENSION age;
LOAD 'age';
SET search_path = ag_catalog, "$user", public;

  • The syntax of the cypher query is like this.
SELECT * FROM cypher('library', $$ 
                     /* cypher_query here */ 
                     () $$) AS (a agtype);
  • 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);
  • 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);
  • 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);
  • 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);
  • 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);
  • 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
  • Here is what my view looks like.

Image description

  • Stop the server.
bin/pg_ctl -D test -l logfile stop

References
Official Apache AGE Documentation


This content originally appeared on DEV Community and was authored by Chidera Stella Onumajuru


Print Share Comment Cite Upload Translate Updates
APA

Chidera Stella Onumajuru | Sciencx (2023-05-03T21:09:59+00:00) Creating your First Graph Database.. Retrieved from https://www.scien.cx/2023/05/03/creating-your-first-graph-database/

MLA
" » Creating your First Graph Database.." Chidera Stella Onumajuru | Sciencx - Wednesday May 3, 2023, https://www.scien.cx/2023/05/03/creating-your-first-graph-database/
HARVARD
Chidera Stella Onumajuru | Sciencx Wednesday May 3, 2023 » Creating your First Graph Database.., viewed ,<https://www.scien.cx/2023/05/03/creating-your-first-graph-database/>
VANCOUVER
Chidera Stella Onumajuru | Sciencx - » Creating your First Graph Database.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/03/creating-your-first-graph-database/
CHICAGO
" » Creating your First Graph Database.." Chidera Stella Onumajuru | Sciencx - Accessed . https://www.scien.cx/2023/05/03/creating-your-first-graph-database/
IEEE
" » Creating your First Graph Database.." Chidera Stella Onumajuru | Sciencx [Online]. Available: https://www.scien.cx/2023/05/03/creating-your-first-graph-database/. [Accessed: ]
rf:citation
» Creating your First Graph Database. | Chidera Stella Onumajuru | Sciencx | https://www.scien.cx/2023/05/03/creating-your-first-graph-database/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.