In this lesson, you will:
-
Learn how to create relationships based on data in a CSV file
-
Create
ACTED_INrelationships between thePersonand Movie nodes.
Creating relationships
The acted_in.csv file contains the following data:
-
movieId - the
movieIdproperty of theMovienode -
person_tmdbId - the
tmbdIdproperty of thePersonnode -
role - the role the person played in the movie
To create the ACTED_IN relationship, you will need to:
-
LOAD CSVdata from theacted_in.csvfile. -
MATCHthe relevantPersonandMovienodes. -
Use
MERGEto create theACTED_INrelationship between the matchedPersonandMovienodes.
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.roleTry to identify:
-
The 2
MATCHclauses that find the appropriatePersonandMovienodes using themovieIdandperson_tmdbIdproperties. -
The
MERGEclause that creates theACTED_INrelationship between the matchedpandmnodes -
The
SETclause that sets theroleproperty of theACTED_INrelationshipr.
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.roleTo 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 25Check Your Understanding
Find or create relationships
Which Cypher clause would you use to find or 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