DEV Community

Cover image for Create Our First Graph in AgensGraph
Muhammad Farooq
Muhammad Farooq

Posted on

Create Our First Graph in AgensGraph

to create your first Graph in AgensGraph you should know the basics of Graph Database and cypher.

If you don't know you can learn from this blog post:Understanding Graph Database and Cypher

Introduction

AgensGraph is a multi-model database, which supports the relational and graph data model at the same time that enables developers to integrate the legacy relational data model and the flexible graph data model in one database. AgensGraph supports ANSI-SQL and openCypher

Creating the project on AGCloud

To create a new project follow these steps

  • Go toAGCloud Website
  • Sign Up if you don't already have the account otherwise sign in
  • after sign in the main page will appear
  • NowCreate a new Project
  • Select "Understanding Graph Database and Cypher" option
  • The Project will be created as shown below

Image showing created project

Understand AGViewer

to create a graph, you need to open AGViewer

  • Click on "Launch AGViewer" from the project you have created as shown in above image

AGViewer will be opened in new tab and the screen will appear like this.

AGViewer image

in query editor you can write or paste queries and can execute queries usingtriangle Iconas shown in above image.

Create Graph

Here is the code that creates our small social network.

CREATE
(a:Male {Name: 'Adam', Age: 25}),
(b:Male {Name: 'Bert', Age: 26, Eyes: 'Brown'}),
(c:Male {Name: 'Carl', Age: 27}),
(d:Female {Name: 'Dee', Age: 25}),
(e:Female {Name: 'Eve', Age: 25}),
(f:Female {Name: 'Fia', Age: 28}),
(a)-[:follows { Duration: 5}]->(b),
(b)-[:follows]->(a),
(b)-[:follows]->(c),
(b)-[:follows]->(d),
(d)-[:follows]->(a),
(c)-[:follows]->(d),
(c)-[:follows]->(e),
(c)-[:follows]->(f),
(f)-[:follows]->(d),
(e)-[:follows]->(b);

Explanation.

TheCREATEstatement createsnodesandedges,and gives themlabelsandproperties.The objects to create are separated by,comma and the whole block ends with a;semi-colon.
Extra spaces and line feeds are ignored and everything is case insensitive (except string values - so 'Ava' is different from 'ava')
You can see the first node to be created is**(a:Male {Name: 'Adam', Age: 25})**.This is a node with Label:Male and with 2properties, Name: and Age:with values as shown.
Theais a variable name to refer to that node while inside that code block as a convenient way to refer to that node later.
All other nodes are created similarly before we create the relationships.
TheCREATE (a)-[:follows { Duration: 5}]->(b)creates an edge with Label:followsfrom the node referred to as** a (Adam's node),to the node referred to asb (Bert's node).**

You should now copy that code block, and paste into the query box of the viewer, and hit the play button on the right hand side.

(Note - sometime paste Ctrl-V or CMD-V on mac does not work in Query Editor and you may have to right click and choose Paste option.)

You should see the message'Successfully ran the query!'

If something goes wrong and you want to start from scratch in AG Viewer with an empty graph you can enter the following command

Comments in AgensGraph

/*
This is a multi-line comment
To delete the graph if you need to start again you can enter the following command...
*/
-- WARNING Deletes entire graph. Only do it if you want to re-start
; MATCH (n) DETACH DELETE n; -- This is also a comment, from double dash to the end of line.

Displaying the Graph

to display the all nodes and edges run the following query

MATCH my_path = ()-[]->() RETURN my_path;

The above query matches every path in the graph and will show the entire graph.
The paths are given the name my_path and the RETURN part of the query will display the paths.
Copy the query and paste it into the query editor in AGViewer. You should get something like this

to show only nodes run the following query

MATCH (n) return n;

I'll show you more examples in my upcoming Blog

Visit Apache AGE

Top comments(0)