In this lesson, you will:
-
Learn how to create relationships based on data in a CSV file
-
Create
ACTED_IN
relationships between thePerson
and Movie nodes.
Creating relationships
The acted_in.csv
file contains the following data:
-
movieId - the
movieId
property of theMovie
node -
person_tmdbId - the
tmbdId
property of thePerson
node -
role - the role the person played in the movie
To create the ACTED_IN
relationship, you will need to:
-
LOAD CSV
data from theacted_in.csv
file. -
MATCH
the relevantPerson
andMovie
nodes. -
Use
MERGE
to create theACTED_IN
relationship between the matchedPerson
andMovie
nodes.
Review the Cypher statement below, which creates the ACTED_IN
relationships between the Person
and Movie
nodes.
LOAD CSV WITH HEADERS
FROM 'https://data.neo4j.com/importing-cypher/acted_in.csv' AS row
MATCH (p:Person {tmdbId: toInteger(row.person_tmdbId)})
MATCH (m:Movie {movieId: toInteger(row.movieId)})
MERGE (p)-[r:ACTED_IN]->(m)
SET r.role = row.role
Try to identify:
-
The 2
MATCH
clauses that find the appropriatePerson
andMovie
nodes using themovieId
andperson_tmdbId
properties. -
The
MERGE
clause that creates theACTED_IN
relationship between the matchedp
andm
nodes -
The
SET
clause that sets therole
property of theACTED_IN
relationshipr
.
This approach of `MATCH`ing 2 nodes and `MERGE`ing a relationship between them is a typical Cypher pattern.
ACTED_IN relationship
Run the Cypher statement to create the ACTED_IN
relationships.
LOAD CSV WITH HEADERS
FROM 'https://data.neo4j.com/importing-cypher/acted_in.csv' AS row
MATCH (p:Person {tmdbId: toInteger(row.person_tmdbId)})
MATCH (m:Movie {movieId: toInteger(row.movieId)})
MERGE (p)-[r:ACTED_IN]->(m)
SET r.role = row.role
To verify that you created the ACTED_IN
relationships successfully, run the following Cypher statement to find people who acted in movies:
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie) RETURN p, r, m LIMIT 25
Check Your Understanding
Creating relationships
Which Cypher clause would you use to create a relationship?
-
❏
LOAD
-
❏
MATCH
-
✓
MERGE
-
❏
SET
Hint
Relationships are created in the same way as creating nodes.
Solution
You would use MERGE
to create a relationship. For example:
MERGE (startNode)-[:RELATIONSHIP]->(endNode)
Summary
In this lesson, you learned how to create relationships between nodes from data in a CSV file.
In the next lesson, you will create the DIRECTED
relationship between the Person
and Movie nodes