Skip to content

arjuntherajeev/neo4j_starter_lesson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

Getting Started with Neo4j

  • Author: Arjun Rajeev Nedungadi (@arjuntherajeev)
  • Field: Graph Databases
  • Topic: Installing & Performing CRUD (Create-Read-Update-Delete) operations on Neo4j - a Graph Database

Why are we doing this?

Typically, when we talk about databases (or persistant storage), the first thing that pops in my head is MySQL - the infamous RDBMS. However, the dynamic is changing and for certain problems, it is much easier and intuitive to represent data using graphs. Neo4j is a Graph Database which helps us represent and visualize data intuitively. It uses a query language called Cypher. In this lesson, we will learn how to represent our data and use Cypher. By the end of this lesson, we will be able to comfortably use Neo4j and perform CRUD (Create-Read-Update-Delete) operations on our data.

Getting Set Up

The first we need to do is to download Neo4j. Run the executable and install it on your machine. A restart might be required.

After installation, there should be a Neo4j shortcut on the Desktop/Start Menu. Run it!

1. Starting Neo4j

Firstly, we see a pop-up window saying Neo4j Community Edition and the Status saying Choose a graph database directory, then start the sever. The most important thing here is to select a Database Location for our Neo4j files. Let us create a new folder on the Desktop called lessongraph. Now, click on the Choose... button and select this folder.

For example, my Database Location is C:\Users\Arjun\Desktop\lessongraph. Next, click Start!

2. Web Interface

After Neo4j is ready, the Status will say Neo4j is ready. Browse to http://localhost:7474/. This means that Neo4j has been started and its Web Interface can be accessed on localhost or 127.0.0.1 on port 7474. We are going to be greeted with a Log in screen. Log in with username neo4j and password neo4j. We will now be prompted to enter a new password. Let us change the password!

The Web Interface has a friendly menu on the left which contains database information, favorites and documentation amongst other items. Here, the Database Information tab is crucial for us where we can see:

  • Node labels
  • Relationship types
  • Property keys

3. Graph Databases

Before we start writing Cypher code, we need to understand the parts of a Graph. A Graph in Neo4j has Nodes, Relationships and Properties. To understand these terms, let us take an example:

"Thomas likes rock music"

  • Thomas is the name of a Person
  • rock is the genre of music
  • likes is the relationship between Thomas and rock music

To make it clearer,

  • Person is a Node with the Property name
  • music is a Node with the Property genre
  • likes is a Relationship between Nodes - Person and music

4. Cypher

Great! We've reached this far! Now, all we need to do is learn Cypher - the Query language of Neo4j. Cypher is pretty straightforward and with a little practice, we can easily master it!

On the Neo4j Web Interface, there is an empty input field on the top of the screen. This is where we need to type in our Cypher queries.

Tip: Type in the Cypher query and press [ENTER] instead of clicking on the Submit button all the time!

A typical Cypher query looks like:

MATCH (p:Person {name:"Thomas"}) RETURN (p)

  • MATCH is a keyword indicating that we want to search (similar to SELECT in SQL).
  • (p:Person) means that we want a Node p with Person Label.
  • {name:"Thomas"} means that we are checking if the property name is "Thomas".
  • RETURN (p) means that we want to return the entire Node (including its properties). RETURN is a keyword!

5. CREATE Operation

Sweet! Now that we have learnt about Graph Databases and Cypher, let's get started with the CREATE operation!

We are trying to replicate the sentence - "Thomas likes rock music" on Neo4j using Cypher.

Nodes Properties
Person name : Thomas
music genre : rock
Relationships Properties
Person likes music None

In Cypher, to understand the flow of Relationships, we use Arrows. The above Relationship is represented as: (Person)-[likes]->(music) with the Arrow indicating that a Person likes music and NOT vice versa!

Tip: Like Nodes, even Relationships can have Properties!

For example, a Property for likes could be since (the year which Thomas began liking rock music)

To construct this Query using Cypher, we need to use the CREATE keyword. Using the concepts mentioned above, the Query will look like:

CREATE (p:Person {name:"Thomas"})-[r:likes]->(m:Music {genre:"Rock"})

Type or Copy-Paste this Query to the input field on the Neo4j Web Interface and hit [ENTER]! If the query successfully executed, we should see that 2 Node labels and 1 Relationship type have been created in the database. Verify this by opening the Database Information tab on the Side Menu of the Web Interface!

Tip: Sometimes, it's nice to get a holistic view of the Graph. Type MATCH (n) RETURN (n) to view the whole Graph!

6. READ Operation

Now that we have learnt to perform the CREATE operation in Neo4j, it's time to READ data from our Graph!

To construct the Read Query using Cypher, we need to use the MATCH keyword (as seen above).

As previously described, the query MATCH (p:Person {name:"Thomas"}) RETURN (p) will return:

╒══════════════╕
│p             │
╞══════════════╡
│{name: Thomas}│
└──────────────┘

Here, it is crucial to understand that RETURN (p) implies that we want ALL information about our Node p. Since we only have 1 Property (name), we see that as the Output. However, we can be specific:

To ONLY return the name: MATCH (p:Person {name:"Thomas"}) RETURN (p.name)

Let's do something a bit more cooler! How about we find what kind of relationship Thomas has with rock music?

MATCH (p:Person {name:"Thomas"})-[r]->(m:Music {genre:"Rock"}) RETURN type(r)

Here, we specified only [r] because we want to find the label of the relationship. We use the type() function to achieve this! It will return:

╒═══════╕
│type(r)│
╞═══════╡
│likes  │
└───────┘

7. UPDATE Operation

Great! The question always arises - What if we need to edit/modify something? What if Thomas decides to like Pop music instead?

To construct the Update Query using Cypher, we need to use the SET keyword. The SET keyword is typically used in conjunction with the MATCH keyword. Let's take an example! For the above modification (Editing Rock to Pop music), the Query will look like:

MATCH (p:Person {name:"Thomas"})-[r:likes]->(m:Music {genre:"Rock"}) SET m.genre="Pop" RETURN m

Now, Thomas likes Pop music!

It is interesting to know that the SET keyword can also be used to add new properties to the node. For example:

MATCH (p:Person {name:"Thomas"})-[r:likes]->(m:Music {genre:"Pop"}) SET p.age="20" RETURN p

As we can see, age is the new Property that we want to add to the Person node. Using the SET keyword, we are assigning a value of 20 to it. The output will be:

╒═══════════════════════╕
│p                      │
╞═══════════════════════╡
│{name: Thomas, age: 20}│
└───────────────────────┘

8. DELETE Operation

We've reached pretty far! Good job! The Delete operation requires the use of the DELETE keyword. The DELETE keyword is often used in conjunction with the MATCH keyword.

Let's try to delete the Music node from our database. To do this, the Query will look like:

MATCH (m:Music) DELETE m - However, This will NOT work! The reason is that there are still Relationships and those need to be deleted first!

  1. To delete Relationships:

    • The first step is to use the DELETE keyword and delete the Relationships. In our example, we only have 1 relationship - likes. To delete this, we will use the Query: MATCH (p:Person)-[r:likes]-(m:Music) DELETE r.
  2. To delete Nodes:

    • Now that we have deleted the Relationship, we can simply delete the Music Node using the Query: MATCH (m:Music) DELETE m.

Now, there should only be 1 Person Node in the database!

It's been a long journey...

There you have it! That's Neo4j in a nutshell. I genuinely hope that this lesson has inspired you to use Neo4j as your database for your next project! The developer community is just brilliant and there are lots of resources available online for Cypher and Neo4j.

For anyone with experience with SQL, what do you think of Neo4j? Is not having to worry about Foreign Keys and whatnots fun? I shall leave you think about it!

Summary

In this lesson, we got acquainted with Neo4j - a Graph Database. We completed the installation and Set-Up stages of Neo4j and progressed to understanding Graph Databases and Cypher (the Query language of Neo4j). Further, we learnt to perform the basic CRUD (Create-Read-Update-Delete) operations on our data in the database.

References

What's Next?

If you enjoyed this lesson and want to take it to the next level, I would strongly advice trying to access Neo4j using a programming language of your choice. Neo4j can be accessed by languages such as Python, Java, JavaScript, etc. using Drivers. You can read more about this here!

Releases

No releases published

Packages

No packages published