0% found this document useful (0 votes)
81 views

Introduction To Neo4j

ajhhajhsdasdjkhahkhkhdasd

Uploaded by

Mantily Holmes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Introduction To Neo4j

ajhhajhsdasdjkhahkhkhdasd

Uploaded by

Mantily Holmes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Introduction to Neo4j

W. H. Bell

November 8, 2023

1 Introduction

The purpose of this document is to introduce common features of graph databases, using the Neo4j
database. Graph databases are used to rapidly join several entities together, allowing analysis across
data samples. A graph database approach is much faster than using a relational database with many
table joins and is syntactically simpler. Graph databases are used within machine learning models, for
fraud detection, and other applications that include relationships between many entities.

2 Typesetting and spaces

A fixed width font is used for commands or source code. To aid the reader, a small bucket character
is used to indicate spaces in commands or source code. This character should be replaced with
a space when the command or source code is entered. An example of the bucket that is used to
indicate spaces is demonstrated in Listing 1, where there is a single space between the command and
argument.

Listing 1: Demonstrating the bucket character used to indicate spaces in source code and commands.
command ␣ argument

3 Graphs and Cypher

An example relationship between a students and a course is given in Figure 1. This diagram includes
two nodes and a relationship between them. Each node comprises a label and a property. In this
example, the labels are :Student and :Course and the properties are two names Joan MacNain and
MSc Software Development. A data sample might comprise many students, who are studying many
different degree programs. Students can be selected by the course that they are following, using the
Cypher query language as shown in Listing 2. Concerning this Cypher query:

Introduction to Neo4j Page 1 of 8


• MATCH searches for a relationship between two nodes.
• RETURN is used to specify what values should be returned. It is needed when reading information
from the graph database.
• WHERE is used to filter the results and is optional.

Figure 1: An example graph relationship between a student and a course.

Listing 2: Searching for a student, who is following a specific course.


MATCH ␣ ( s : Student ) -[: FOLLOWING ] - >( c : Course )
WHERE ␣ c . name ␣ = ␣ " MSc ␣ Software ␣ Development " ␣ RETURN ␣ s . name

4 Assumptions

It is assumed that these exercises are being run on a Linux PC in the Computer and Information
Sciences labs. However, these exercises can be run on another computer, provided that Neo4j desktop
has been installed. If Neo4j desktop is used on another computer, it can be started from the application
menu rather than the command line.

This document includes Bash Linux commands to clone a software repository and change directory. A
separate sheet of Bash Linux commands is provided to Computer and Information Sciences students.
It is assumed that the reader is either familiar with these commands or has access to the reference
document.

The commands that are given in this document have been tested with Neo4j desktop version 5.13.0.
They may work with other recent versions of Neo4j.

5 Exercises

For each Cypher script provided, view it in the Neoj4 desktop browser before running it.

1. Open a terminal window.


2. Clone the exercises repository by typing the command that is given in Listing 3 on one line.

Listing 3: Cloning the MySQL exercises.


git ␣ clone ␣ https :// gitlab . cis . strath . ac . uk / gxb20157 / introduction - to -
neo4j . git

Introduction to Neo4j Page 2 of 8


3. Start Neo4j desktop by clicking on it in the application menu or by typing Listing 4 in the terminal
window.

Listing 4: Starting Neo4j desktop.


neo4j - desktop

When Neo4j desktop first starts, the license agreement should be accepted by clicking on “I
Agree”. Then click on “Register later”. When working on the CIS lab PCs, ignore update requests
by clicking on “Later” as needed.

Neo4j desktop files are stored in a temporary area on the CIS lab PCs, which is on the
hard disk of the physical lab PC. This implies that any databases that are created will be lost
when the temporary area is cleaned up by IT management processes.
4. Use the mouse to hover over “Movie DBMS” and click on “Stop”.
5. Create a new project by clicking on “New”, which is next to “Projects”.
6. Change the project name to “Sales” by hovering over the project name and clicking on it.
7. Click on “Add” to the right of the project name “Sales”. Then select “Local DBMS”.
8. Rename the database to “Sales DBMS”.
9. Set a password of “password”. This is important if the optional Python programs are used.
10. Leave the “Version” set at its default value.
11. Click on “Create”.
12. Use the mouse to hover over the “Sales DBMS” and click on “Start”.
13. Click on “Reveal files in File Explorer”.
14. Use the file explorer to copy the files ending in .cypher from introduction-to-neo4j/src/ into
the project folder that is associated with the database.
15. Use the mouse to hover over create-customers.cypher in the Neo4j Desktop and click on
“Open”. This opens a separate window.
16. Click on the triangle to the right of the file contents to run the script.

The contents of create-customers.cypher is given in Listing 5. Concerning this Listing:

• Line 1: This is a comment, which is ignored by Neo4j when the script is run.
• Line 2: A customer is created using CREATE, followed by (). The text within the () describes
one node. The name barry is a variable, which is accessible when the script is running.
The :Customer is label, where the label name can be chosen as needed. The name: is
a property name, which is followed by an associated value. Each node can have several
properties, which are separated by commas.
• Line 3-6: Another four customers are created.

Introduction to Neo4j Page 3 of 8


Listing 5: The create-customers.cypher file.
1 // ␣ Create ␣ several ␣ customers .
2 CREATE ␣ ( barry : Customer ␣ { name : ’ Barry ␣ Rayner ’})
3 CREATE ␣ ( zaynab : Customer ␣ { name : ’ Zaynab ␣ Rowley ’})
4 CREATE ␣ ( asiya : Customer ␣ { name : ’ Asiya ␣ Sheridan ’})
5 CREATE ␣ ( kynan : Customer ␣ { name : ’ Kynan ␣ Payne ’})
6 CREATE ␣ ( jak : Customer ␣ { name : ’ Jak ␣ May ’})

17. Run select-all.cypher to select all of the nodes.

• In the results display, view the “Graph”, “Table”, and “Text” output.
• In the “Graph” results, click on one of the notes to display information about it. Notice that
Neo4j has created an id value for each customer.

The contents of select-all.cypher is given in Listing 6. The command MATCH can be used
with or without an associated WHERE. If no constrains are provided, MATCH returns all nodes. In
this case, RETURN is followed by n. This causes a node to be returned, rather than a specified
property of a node.

Listing 6: The select-all.cypher file.


1 MATCH ␣ ( n ) ␣ RETURN ␣ n

18. delete-customers.cypher to delete the customers.

The contents of delete-customers.cypher is given in Listing 7. The WHERE condition is used to


select only Customer nodes. Each of the selected nodes is deleted with DETACH DELETE n.

Listing 7: The delete-customers.cypher file.


1 // ␣ Delete ␣ all ␣ of ␣ the ␣ customers .
2 MATCH ␣ ( n ) ␣ WHERE ␣ ( n : Customer ) ␣ DETACH ␣ DELETE ␣ n

19. Run create-purchases.cypher to create, purchases, customers and relationships from


customers to purchases. Then view the relationships with “Graph” results.

The contents of create-purchases.cypher is given in Listing 8. Concerning this Listing:

• Line 2-6: Five purchases are created.


• Line 9-13: Five customers are created. In this case, the customers have two properties:
name and age.
• Line 16-26: Relationships are created from the customers to the purchases, using the
relationship name BOUGHT. Relationship names can be created as needed, but must be
consistently used. The relationships are formed by using the variables that were declared
when the nodes were created.

Introduction to Neo4j Page 4 of 8


Listing 8: The create-purchases.cypher file.
1 // ␣ Create ␣ several ␣ purchases .
2 CREATE ␣ ( socks : Purchase ␣ { name : ’ Socks ’})
3 CREATE ␣ ( computer : Purchase ␣ { name : ’ Computer ’})
4 CREATE ␣ ( coding_book : Purchase ␣ { name : ’ Coding ␣ book ’})
5 CREATE ␣ ( french_fries : Purchase ␣ { name : ’ French ␣ fries ’})
6 CREATE ␣ ( icecream : Purchase ␣ { name : ’ Ice - cream ’})
7
8 // ␣ Create ␣ several ␣ customers .
9 CREATE ␣ ( barry : Customer ␣ { name : ’ Barry ␣ Rayner ’ , ␣ age : ␣ 25})
10 CREATE ␣ ( zaynab : Customer ␣ { name : ’ Zaynab ␣ Rowley ’ , ␣ age : ␣ 29})
11 CREATE ␣ ( asiya : Customer ␣ { name : ’ Asiya ␣ Sheridan ’ , ␣ age : ␣ 30})
12 CREATE ␣ ( kynan : Customer ␣ { name : ’ Kynan ␣ Payne ’ , ␣ age : ␣ 27})
13 CREATE ␣ ( jak : Customer ␣ { name : ’ Jak ␣ May ’ , ␣ age : ␣ 28})
14
15 // ␣ Create ␣ relationships ␣ between ␣ customers ␣ and ␣ purchases .
16 CREATE ␣ ( barry ) -[: BOUGHT ] - >( socks )
17 CREATE ␣ ( barry ) -[: BOUGHT ] - >( computer )
18 CREATE ␣ ( barry ) -[: BOUGHT ] - >( coding_book )
19 CREATE ␣ ( zaynab ) -[: BOUGHT ] - >( french_fries )
20 CREATE ␣ ( zaynab ) -[: BOUGHT ] - >( icecream )
21 CREATE ␣ ( asiya ) -[: BOUGHT ] - >( computer )
22 CREATE ␣ ( asiya ) -[: BOUGHT ] - >( coding_book )
23 CREATE ␣ ( kynan ) -[: BOUGHT ] - >( socks )
24 CREATE ␣ ( kynan ) -[: BOUGHT ] - >( icecream )
25 CREATE ␣ ( jak ) -[: BOUGHT ] - >( socks )
26 CREATE ␣ ( jak ) -[: BOUGHT ] - >( computer )

20. Run find-purchases.cypher to search for purchases for a selected customer.

The contents of find-purchases.cypher is given in Listing 9. The command finds all Customer
nodes that BOUGHT a purchase, where the name of the Customer must be “Barry Rayner”. The
RETURN is followed by p.name to specify that only the name of the purchase should be returned.

Listing 9: The find-purchases.cypher file.


1 // ␣ Find ␣ purchases ␣ for ␣ selected ␣ customer .
2 MATCH ␣ ( c : Customer ) -[: BOUGHT ] - >( p : Purchase )
3 WHERE ␣ c . name ␣ = ␣ ’ Barry ␣ Rayner ’ ␣ RETURN ␣ p . name

21. Run find-customers.cypher to search for customers for a selected purchase.

The contents of find-customers.cypher is given in Listing 10. The command returns the names
of the Customer nodes who have bought ice-cream.

Introduction to Neo4j Page 5 of 8


Listing 10: The find-customers.cypher file.
1 // ␣ Find ␣ customers ␣ for ␣ selected ␣ purchase .
2 MATCH ␣ ( c : Customer ) -[: BOUGHT ] - >( p : Purchase )
3 WHERE ␣ p . name ␣ = ␣ ’Ice - cream ’ ␣ RETURN ␣ c . name

22. Run find-customers-2.cypher to search for customers for two selected purchase.

The contents of find-customers-2.cypher is given in Listing 11. The command returns the
Customer nodes who have bought either ice-cream or french fries. Due to the data and query
specified, the query would return multiple instances of the same node without DISTINCT.

Listing 11: The find-customers-2.cypher file.


1 // ␣ Find ␣ customers ␣ for ␣ selected ␣ purchases .
2 MATCH ␣ ( c : Customer ) -[: BOUGHT ] - >( p : Purchase )
3 WHERE ␣ p . name ␣ IN ␣ [ ’ Ice - cream ’ , ’ French ␣ fries ’]
4 RETURN ␣ DISTINCT ␣ c

23. Edit the find-customers-2.cypher command and remove DISTINCT. Then re-run the command
to check what happens. Look at the data to understand the results of the query.
24. Run delete-all.cypher to remove all of the nodes.

The contents of delete-all.cypher is given in Listing 12. Since no selection conditions are
included, the command deletes all nodes.

Listing 12: The delete-all.cypher file.


1 // ␣ Delete ␣ all ␣ nodes .
2 MATCH ␣ ( n ) ␣ DETACH ␣ DELETE ␣ n

25. Run create-network.cypher to create a series of houses, a postcode and electrical connections.
Then use select-all.cypher to view the nodes that are created.

The contents of create-network.cypher is given in Listing 13. Concerning this Listing:

• Line 3-27: Several nodes and relationships are created using one CREATE, where each
creation request is given within () that is separated from the next () by a comma.
• Line 3-5: A Postcode is created, which has three properties.
• Line 6-10: Five House nodes are created. These nodes have two labels Node and House.
This implies that either label could be used within a query.
• Line 11-15: The House nodes are associated with the postcode.
• Line 16-18: Three Connection nodes are created, which are also given the Node label.
• Line 19: A Splitter node is created, which is also given the Node label.
• Line 20-27: Connected relationships between the House, Connection and Splitter nodes
are defined.

Introduction to Neo4j Page 6 of 8


Listing 13: The create-network.cypher file.
1 // ␣ Create ␣ a ␣ postcode , ␣ series ␣ of ␣ houses , ␣ and
2 // ␣ some ␣ electrical ␣ connections .
3 CREATE ␣ ( pc : Postcode ␣ { name : ’ CR0 ␣ 3 RN ’ ,
4 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ latitude : ␣ 51.376398 ,
5 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ ␣ longitude : ␣ -0.108977}) ,
6 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h1 : Node : House ␣ { name : ’2 ␣ Theobald ␣ Road ’}) ,
7 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h2 : Node : House ␣ { name : ’3 ␣ Theobald ␣ Road ’}) ,
8 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h3 : Node : House ␣ { name : ’4 ␣ Theobald ␣ Road ’}) ,
9 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h4 : Node : House ␣ { name : ’5 ␣ Theobald ␣ Road ’}) ,
10 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h5 : Node : House ␣ { name : ’6 ␣ Theobald ␣ Road ’}) ,
11 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h1 ) -[: HAS ] - >( pc ) ,
12 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h2 ) -[: HAS ] - >( pc ) ,
13 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h3 ) -[: HAS ] - >( pc ) ,
14 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h4 ) -[: HAS ] - >( pc ) ,
15 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h5 ) -[: HAS ] - >( pc ) ,
16 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( c1 : Node : Connection ␣ { name : ’ Phase ␣ 1 ’}) ,
17 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( c2 : Node : Connection ␣ { name : ’ Phase ␣ 2 ’}) ,
18 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( c3 : Node : Connection ␣ { name : ’ Phase ␣ 3 ’}) ,
19 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( s : Node : Splitter ␣ { name : ’ Substation ’}) ,
20 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h1 ) -[: CONNECTED ] - >( c1 ) ,
21 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h2 ) -[: CONNECTED ] - >( c1 ) ,
22 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h3 ) -[: CONNECTED ] - >( c1 ) ,
23 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h4 ) -[: CONNECTED ] - >( c2 ) ,
24 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( h5 ) -[: CONNECTED ] - >( c3 ) ,
25 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( c1 ) -[: CONNECTED ] - >( s ) ,
26 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( c2 ) -[: CONNECTED ] - >( s ) ,
27 ␣ ␣ ␣ ␣ ␣ ␣ ␣ ( c3 ) -[: CONNECTED ] - >( s )

26. Run count-connections.cypher to count the number of electrical connections for each
connection point.

The contents of count-connections.cypher is given in Listing 14. The command returns the
name of the Connection nodes and the number of houses that are connected to them.

Listing 14: The count-connections.cypher file.


1 // ␣ Count ␣ the ␣ number ␣ of ␣ houses ␣ connected ␣ to ␣ a
2 // ␣ network ␣ connection .
3 MATCH ␣ ( h : House ) -[: CONNECTED ] - >( c : Connection )
4 RETURN ␣ c . name , ␣ count ( h )

27. Select all of the houses that are connected to the electrical connection point with the
most connections. This can be achieved by using a Cypher command that is similar to
find-customers.cypher.

Introduction to Neo4j Page 7 of 8


28. Run delete-all.cypher to remove all of the nodes.
29. Close the Neo4j desktop window. Then click on “Stop DBMS, then quit”.

6 Further reading

The examples presented in this document demonstrate some of the features of a graph
database. More information on Cypher is given at https://neo4j.com/developer/cypher/ and
https://www.tutorialspoint.com/neo4j/.

Introduction to Neo4j Page 8 of 8

You might also like