In this article, we will talk briefly about graph databases and what Neo4j is. Then we will give a step-by-step guide on how to install and get started with Neo4j on Windows operating system.
This article is the second in the NoSQL databases series, where we will explain different NoSQL database technologies, and how to integrate them with SQL Server databases.
What is a graph database?
It is a NoSQL database used to store network graphs. They are used to model relationships between objects, not only data. While traditional SQL is based on logic and set theory, graph databases are based on graph theory.
In this type of NoSQL database, data is represented as node (entities) and edges (relations) where each node or edge can have one or many properties (attributes) assigned. Since it is a NoSQL database, a graph database doesn’t require a pre-defined model to store data. As an example, two different persons may have different properties:
- Employee (name: Mike Rose, Age: 25)
- Employee (name: Tim Moshin, DOB: 1980-01-01)
This database is more efficient than relational databases in performing analyzing networks, especially when handling large datasets where JOINS operations are expensive. Graphs are used for artificial intelligence, fraud detection, social network analysis, and other promising domains.
What is Neo4j?
Neo4j is the most popular graph database. It is very performant and guarantees high scalability, making it used by many leading companies such as Microsoft, HP, IBM, Adobe, CISCO, and eBay. Neo4j is an open-source database project that was first released in February 2010. The latest stable version is 4.2.1, which was released last month (2020-11-17).
Cypher is the language used to query and administer Neo4j graphs.
Neo4j Installation on Windows
This section is a step-by-step guide to download and install the latest version of Neo4j Desktop.
To download the latest version of Neo4j desktop, you should open your browser and navigate to the Neo4j download page.
Figure 1 – Neo4j download page
Click on the download button to start downloading the Neo4j desktop installation file. After clicking the download button, you are asked to fill in some information before getting started.
Figure 2 – Download required information
After filling in the information and clicking on the “Download Desktop” button, the file download process starts. And you are redirected to a page that includes the activation key you will need later.
Figure 3 – Activation key
After the download is complete, open the installation file. Neo4j installation is straightforward; you must only specify if you need to install it on the current user or all user accounts.
Figure 4 – Choosing Installation option
The next step is to select the installation path.
Figure 5 – Selecting the installation path
Figure 6 – Installation progress
Figure 7 – Installation completion dialog
After installation is complete, open the Neo4j desktop. After reading and accepting the user license agreement, you are asked to paste the activation code generated previously (when downloading Neo4j).
Figure 8 – License agreement
Figure 9 – Pasting the activation key
Once the activation key is validated, the sample databases are installed, and the Neo4j main screen appears.
Figure 10 – Main screen
You can refer to the Neo4j official documentation to learn more about the main screen’s navigation option.
Starting the Movie graph database
If you want to start learning from scratch, you can create a new project by clicking on the “New” button on the screen’s top left.
Figure 11 – Adding a new project
This tutorial will be using the Neo4j primer project, which contains a sample database called the “Movie database” that contains information about movies and related people.
First, we should start the Movie graph database by clicking on the “Start” button shown in the image below.
Figure 12 – Starting movie database
Once the movie database is active, click on the “Open” button to open it using the Neo4j browser.
Figure 13 – Opening the movie database
Figure 14 – Neo4j browser
Movie database contains built-in examples. You can create nodes and edges, add properties, and explore your data using interactive slideshows. To get started, enter the following command in the console:
1 |
:play movie-graph |
This command will open the movie database example interactive slideshow.
Figure 15 – Movie database example interactive slideshows
Explaining the Movie database tutorial
In this section, we will explain the main commands that appear in the movie database example.
Creating and switching the current database
To create a new database, you should use the following command:
1 |
create database <database name> |
To switch to the current database, you should use the following command:
1 |
:use <database name> |
To switch to the system database, you should use:
1 |
:use system |
Creating Nodes and Edges
The next step is to add Nodes (entities). If we look at the code shown in the second slide. We can see that there are two types of commands:
- Creating Nodes
- Creating Edges
The following lines of code are an example of creating nodes:
1 2 3 |
CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'}) CREATE (Keanu:Person {name:'Keanu Reeves', born:1964}) CREATE (Carrie:Person {name:'Carrie-Anne Moss', born:1967}) |
In the previous code lines, the first line is to add a movie node, and the others are for persons. From those lines, we can see that the node creation command syntax is:
1 |
CREATE (<Node name>:<Node type> {<Property 1 name>:<property 1 value>, <Property 2 name>:<Property 2 value>, …}) |
The following lines of codes are an example of creating edges (relations):
1 2 3 4 5 6 7 8 |
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix), (Carrie)-[:ACTED_IN {roles:['Trinity']}]->(TheMatrix), (Laurence)-[:ACTED_IN {roles:['Morpheus']}]->(TheMatrix), (Hugo)-[:ACTED_IN {roles:['Agent Smith']}]->(TheMatrix), (LillyW)-[:DIRECTED]->(TheMatrix), (LanaW)-[:DIRECTED]->(TheMatrix), (JoelS)-[:PRODUCED]->(TheMatrix) |
From those lines, we can see that the node creation command syntax is:
1 |
CREATE (<Node 1 label>)-[:<Edge label> {<Edge property 1>:<Edge property 1 value>, …, <Edge property n>:<Edge property n value>}]->(<Node 2 label) |
After running this command we the created graph will be visualized as shown below:
Figure 16 – Visualized graph
Querying graphs
The rest of the Movie database tutorial contains different examples of data retrieval operations using GraphQL. Note that the MATCH command is used to query data. As an example, to get all movies that Tom Hanks acted in:
1 2 |
MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies) RETURN tom,tomHanksMovies |
Note that the result can be visualized as a graph, text, table, or code:
Figure 17 – Query result
Conclusion
In this article, we have explained what graph databases are. Then we talked briefly about the leading graph database called Neo4j. We showed how to download and install it on the Windows operating system and start creating and querying graphs. This was just a brief introduction, we will go deeper in the next articles. You can also refer to this documentation, Neo4j official developer resources to get more details.
Table of contents
- An overview of SQL Server monitoring tools - December 12, 2023
- Different methods for monitoring MongoDB databases - June 14, 2023
- Learn SQL: Insert multiple rows commands - March 6, 2023