DEV Community

Cover image for Getting started with Graph database using Neo4j
Vishnu Sivan
Vishnu Sivan

Posted on

Getting started with Graph database using Neo4j

A graph database stores information as relationships between entities and represents this data using nodes and edges. It allows you to easily store data and analyze the relationships among them.

Neo4j is a highly scalable, schema free graph database management system. It stores and displays data in the form of graph and represents data using nodes and edges and relationships. A major feature of Neo4j is creating interconnection between various entities (nodes). It provides a declarative query language known as Cypher (CQL) for querying data.

In this session, we will learn Neo4j basics and try basic clauses such as CREATE, MATCH, WHERE, RETURN, SET, DELETE etc.

Getting Started

Relational database stores data in the form rows and columns and relations are mentioned using foreign key constraint. This becomes complicated when it involves a lot of indirect relationships between the data. The connections between the data becomes equally important as the data itself.

neo4j graph
Image credits: neo4j.com

Graph database uses graph structures with nodes, relationships and properties to store and visualize data.

The fundamental components of a graph database are:

  • Nodes — the entities in a graph.
  • Relationships — the connections between the entities, similar to foreign keys in a relational database.
  • Labels — attributes that group similar nodes together.
  • Properties — key/value pairs stored within nodes or relationships.

neo4j
neo4j
Image credits: neo4j.com

In relational databases, we can store relationships as tables and navigate them using JOIN operations or cross-lookups which is time consuming, not cost-effective method. Whereas in graph databases, relationships are stored as data elements (the nodes) in a much more flexible format. This enables the user to navigate deep hierarchies, find hidden connections between distant nodes, and discover inter-relationships between nodes.

neo4j

Neo4j

Neo4j is a schema free graph database management system. It is a NoSQL based ACID complaint transactional database. It uses Cypher Query Language (CQL) for creating, modifying, interconnecting and deleting data (nodes).

Features

  • Flexible data model − Neo4j provides a flexible, simple and powerful data model, which can be easily changed according to the requirements.
  • ACID properties − Neo4j supports ACID (Atomicity, Consistency, Isolation, and Durability) rules unlike other databases.
  • Scalability and reliability − You can scale the database by increasing the number of reads/writes, and the volume without affecting the query processing speed or data integrity.
  • Cypher Query Language − Neo4j provides a declarative query language known as Cypher. It can be used to create and retrieve relations between data without using complex queries like JOINS.
  • Built-in web application − Neo4j provides a built-in Neo4j Browser web application to create and query your graph data.
  • Drivers − Neo4j supports REST API to work with programming languages and Java Script to work with UI MVC frameworks.

Installation

Install Neo4j in your machine using the following steps:

  • Download and install Java JDK 1.8 from the official site.
  • Download the installer from the official site. For which, open the link Neo4j Download Center — Neo4j Graph Data Platform and click on the Download Neo4j Desktop button. download1
  • Provide the necessary information in the next page and click on the Download Desktop button. download2
  • Copy the Neo4j activation key from the final page, it is required in the software registration step. download3
  • Double click on the installer and select all users option from the window. Provide the destination folder path and click on the Install button to start the installation process.

download4
download5

  • Run the Neo4j Desktop application. Accept the terms and conditions, provide the data path (keep as defaults), provide name, email and software key (neo4j activation key) in the next window to complete the neo4j initial configuration.
    installation

  • You can see the following window after the successful configuration.
    config

Understand the IDE

The following is the screenshot of the Neo4j Desktop in the first use. It provides an Example Project and the basic CQL commands on the about-movies.neo4j-browser-guide file to introduce the CQL commands.
understand the ide

Let’s understand the basic Neo4j Desktop components,

  • Project Hierarchy Window — It shows the project names, the user can switch to another project by clicking on the project folder name. The user can delete the project if required.
  • Active DBMS — It shows the currently running DBMS name, option to open the Neo4j browser and Stop button to terminate the DBMS.
  • Project Info Window — It shows the project related information such as project name and DBMS info. It provides an Add button to add new DBMS locally, via remote connection and a file, the Stop button can be used to terminate the DB, the Open button to open the CQL window and the Refresh button to refresh the database.
  • Detail Window — It provides information such as DBMS name, version, HTTP port, Bolt port and so on.
  • File Window — It shows the File list which contains the files associated with the project.

Explore Example Project

As a new user, you will get an example project with movie DBMS containing details of Movies and Actors. You can click on the Start button to start the database if it is not automatically started.
explore

  • Click on the Open button located with the browser guide file in the File window. It will open a new neo4j browser like below,
    neo4j

  • Click on the Next arrow button to see more information on the Movie database. You can also execute the CQL commands listed in the description. For which, click on the command. It will paste the command into the input box.
    neo4j

  • Click on the Play button to execute the command. You can use the http://localhost:7474/browser/ link to open neo4j browser in a browser to execute the commands.
    neo4j

  • You can click on the node to expand and view the properties. You can visualize the CQL query result as Graph, Table, Text and Code.
    output

Understand the Neo4j CQL Clauses

Cypher is a graph query language (CQL) used in Neo4j that enables you to create, modify, interconnect, delete and retrieve data from the graph. It provides a way to visualize the data in the form of nodes and relationships. It uses an ASCII-art type of syntax where (nodes)-[:ARE_CONNECTED_TO]->(otherNodes) using rounded brackets for circular (nodes), and -[:ARROWS]-> for relationships.

Read clauses:
read

Write clauses:
write

General Clauses:
general

Create your first Neo4j project

In this section, we will create a Neo4j project for managing person related information. Let’s create the below graph using Neo4j.

project image

Create new project

Follow the steps to create a new project in Neo4j.

  • Click New button and select Create project option from the project window.
    project1

  • Click the edit icon to provide a valid project name.
    project2

  • Click Add button and select Local DBMS option.
    project3

Provide name, password and version of the DBMS. Click the Create button to create the project.
project4

  • Start the project by clicking on the Start button in the DBMS list.
    project5

  • Click the Open button to switch to the Neo4j browser window.
    project6

Write CQL commands

Let’s begin with the person node creation.

  • Execute the following statements to create 4 person nodes.
CREATE (a:Person {name:'Ajith', born: 'Apr 10, 1996', linkedin:'@ajith'}) RETURN a
CREATE (a:Person {name:'Ishani', born: 'Mar 8, 1998', linkedin:'@ishani'}) RETURN a
CREATE (a:Person {name:'Gowri', born: 'Oct 16, 1997', linkedin:'@gowri'}) RETURN a
CREATE (a:Person {name:'Vishnu', born: 'Dec 12, 1994', linkedin:'@codemaker2015'}) RETURN a
Enter fullscreen mode Exit fullscreen mode

cql commands

  • Execute the following statements to create 2 game nodes.
CREATE (a:Sports {name:'Chess'}) RETURN a
CREATE (a:Sports {name:'Cricket'}) RETURN a
Enter fullscreen mode Exit fullscreen mode

cql commands
cql commands

  • Execute the following statement to create a company node.
CREATE (a:Company {name:'ABC Company', location: "Ernakulam"}) RETURN a
Enter fullscreen mode Exit fullscreen mode

company

  • Execute the following statement to create an institution node.
CREATE (a:Institution {name:'XYZ College', location: "Kochi"}) RETURN a
Enter fullscreen mode Exit fullscreen mode

institution

Let’s list the node details using the MATCH clause.

MATCH (n) RETURN n LIMIT 25
Enter fullscreen mode Exit fullscreen mode

match

Get the node details of the person node, ‘Vishnu’, by clicking on the node.

MATCH (a {name: 'Vishnu'}) RETURN a
Enter fullscreen mode Exit fullscreen mode

match

Now, we can create relationship between the nodes as shown in the figure (fig: person management).

  • Execute the following statement to create a PLAYS relationship between person nodes and game nodes.
MATCH (a:Person), (b:Sports) WHERE (a.name = 'Vishnu' OR a.name = 'Ajith') AND b.name = 'Cricket' CREATE (a)-[r:PLAYS]->(b)
MATCH (a:Person), (b:Sports) WHERE (a.name = 'Vishnu' OR a.name = 'Gowri' OR a.name = 'Ishani') AND b.name = 'Chess' CREATE (a)-[r:PLAYS]->(b)
Enter fullscreen mode Exit fullscreen mode

cql commands

  • Create a relationship between institution and person nodes using the following statement.
MATCH (a:Person), (b:Institution) WHERE (a.name = 'Vishnu' OR a.name = 'Ishani') AND b.name = 'XYZ College' CREATE (a)-[r:ACADEMICS]->(b)
Enter fullscreen mode Exit fullscreen mode

relationship

  • Create a relationship between company and person nodes using the following statement.
MATCH (a:Person), (b:Company) WHERE (a.name = 'Vishnu' OR a.name = 'Gowri') AND b.name = 'ABC Company' CREATE (a)-[r:WORKS]->(b)

Enter fullscreen mode Exit fullscreen mode

relationships

  • View the nodes and relationships using the following statement.
MATCH (n) RETURN n
Enter fullscreen mode Exit fullscreen mode

match

  • Delete the PLAYS relationship between Ajith and Cricket using the following command.
MATCH (n {name: 'Ajith'})-[r:PLAYS]->() DELETE r
Enter fullscreen mode Exit fullscreen mode

delete1
delete2

  • Delete a node with property name as Ajith.
MATCH (n {name: 'Ajith'}) DELETE n
Enter fullscreen mode Exit fullscreen mode

delete3
delete4

Thanks for reading this article.

Thanks Gowri M Bhatt for reviewing the content.

If you enjoyed this article, please click on the heart button ♥ and share to help others find it!

The article is also available on Medium.

The full source code for this tutorial can be found here,

GitHub - codemaker2015/neo4j-examples: Neo4j example project codes

Useful Links:

Top comments (0)