1. CS331 - Database Management Systems
Dr. Lilia Sfaxi
17 novembre 2019
Neo4J
1. Graph-based Databases : Neo4J
1.1. Graphs
Formally, a graph is a collection of nodes and arcs that connect them. The
graphs represent the entities in the form of nodes, and the ways in which these
entities are connected in the form of arcs. This structure inspired the graph-
oriented databases, because they make it possible to model all types of scenarios.
For example, the relationships between Twitter users can be represented in a
simpli
fi
ed way by the following graph:
A graph can represent any type of data.
- A node is the equivalent of a record
- A relation makes it possible to connect nodes, and can be typed
- Nodes and relationships can have properties, representing named
attributes
- A label is a name allowing to organize the nodes in groups
NEO4J DR. LILIA SFAXI 1
MedTech
2. The database we will use in this lab is Neo4j . This database uses the Cypher
1
language to represent queries.
1.2.Example
The example we will show in this manuscript is taken from the tutorial provided
by Neo4j in their site on this link: http://neo4j.com/graphacademy/online-
course/
The data we process represents:
- Movies with a label : Movie and the properties title, released and tagline
- People with a label : Person and the properties name and born
- An ACTED_IN relation from an actor to a movie, with a roles property
containing the list of character names
- A DIRECTED relationship from a director to a
fi
lm.
Activity 1
Launch Neo4J on your browser. The default login/password is neo4j/neo4j (they will
ask you to change after
fi
rst authentication).
Create the Movies database given as an example by the tool by following these steps:
- Once authenticated and the default password replaced, choose: "Jump Into Code" to
start developing.
- Click on "Create a Graph" in the "Movie Graph" section as follows:
Neo4J: http://neo4j.com/
1
NEO4J DR. LILIA SFAXI 2
3. - In the window that appears, click on the graph creation code to load it into the
editor. Then run it.
1.3.The Cipher Language
Nodes
(a) Actors
(m) Films
( ) Anonymous Node
Relations
-[r]-> A relation called r
(a) –[r]-> (m) Actors having a relation r with movies
-[:ACTED_IN]-> The relation is of type ACTED_IN
(a)-[:ACTED_IN]->(m) Actors who play in a movie
(d)-[:DIRECTED]->(m) The directors of a
fi
lm
Properties of Nodes
(m {title : "The Matrix"}) Movie with a property title
(a {name: "Keanu Reeves", born : 1964}) Actor with properties name and born
NEO4J DR. LILIA SFAXI 3
4. Properties of Relations
(a)-[ :ACTED_IN {roles :[« Neo »]}]->(m) Relation ACTED_IN with the
propperty roles, which is an array of
names
Labels
Allow to recognize different types of nodes.
(a:Person)-[:ACTED_IN]->(m:Movie) A person who has played in a movie
1.4. Cipher queries
1.4.1. Simple read requests
Here are some examples of Cipher queries:
The
fi
rst statement returns the value of the property property for all nodes.
The second one, returns the values of propertyA and propertyB of each pair
of nodes node2 and node1 which are connected by a relation (whatever its
nature).
To return only nodes connected by a relation of type REL_TYPE:
NEO4J DR. LILIA SFAXI 4
5. If the relation in question contains properties that one wishes to read, give
it a name (here rel):
To indicate a particular type of nodes, use the labels:
It is possible to assign an identi
fi
er to a relation, then return the type of the
relation, or its properties:
To support by an example, consider the following graph:
The query to
fi
nd all the characters in The Matrix movie is:
NEO4J DR. LILIA SFAXI 5
6. Another way is also possible to
fi
lter the data (instead of the traditional
WHERE), by replacing the previous query by:
Activity 2. Run the following queries in your editor:
- Give all movies where Tom Hanks played
- Give the list of all Matrix characters
- Give each
fi
lm its release date
1.4.2. Paths
A path is a set of connected nodes and relationships, which can be a
pattern. For example :
(a) -> (b) -> (c)
(a) -> (b) <- (c)
Example: To show all actors and directors of all movies:
It is also possible to divide the query into several paths, as follows:
Or:
NEO4J DR. LILIA SFAXI 6
7. The results of previous queries are tables.
It is possible to return the entire path, with all the properties of all nodes
and relationships involved:
If this is too much information, you can just select the nodes of the path:
... or only relationships:
Activity 3. Execute the following queries in your editor:
- Which directors have played in their own movies?
- Give the movies where Keanu Reeves played the role of Neo.
- Which actors have already played in the same movie with Tom Hanks,
and in which
fi
lm?
- Which actors played in the same movie with Tom Hanks, and with
Keanu Reeves?
- Who are the characters in the movie "The Matrix"?
1.4.3. Edit a graph with Cipher
To add a node to the graph:
To add (or modify) properties to an existing node:
NEO4J DR. LILIA SFAXI 7
8. To add a relationship between two existing nodes:
Activity 4. Run the following queries in your editor:
- Create a movie called Mystic River
- Add Kevin Bacon as an actor in the movie "Mystic River" with the role
"Sean"
- Change the role to become "Sean Devine"
NEO4J DR. LILIA SFAXI 8