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 calledlibrary
using thecreate_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 graphlibrary
. We assigned this to a variable calledv
, returned this v variable using theRETURN
clause and got the output below.
- 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 variablesa
andb
respectively, with theWHERE
clause and theAND
clause we assessed the properties we are looking for using thea
andb
variables, then finally we used theCREATE
clause to create a relationship(Edge) between these two, We returned our newly created Edge which has the labelWrittenBy
using the variablee
.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.
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.
- 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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.