Exercise 16

Exercise 16: Importing data (Preparations)


If you are using Neo4j Desktop:

You will start with a new database named importcsv. Execute these commands in Neo4j Browser to create the database.

:USE system
CREATE DATABASE importcsv
:USE importcsv

If you are using Neo4j Sandbox or Neo4j Aura, you will need to:

  1. Drop all indexes and constraints in the graph.

  2. Execute MATCH (n) DETACH DELETE n

Exercise 16: Importing data (Overview)


In this exercise you write Cypher statements to import data into your graph.

  • Exercise 16.1: Write the Cypher statement to read the actor data from a file.

  • Exercise 16.2: Read the data and return it, ensuring that the data returned is properly formatted.

  • Exercise 16.3: Load the data into your graph.

  • Exercise 16.4: Write the Cypher statement to read the movie data from a file.

  • Exercise 16.5: Read the data and return it, ensuring that the data returned is properly formatted.

  • Exercise 16.6: Load the data into your graph.

  • Exercise 16.7: Write the Cypher statement to read the relationship data from a file.

  • Exercise 16.8: Read the data and return it, ensuring that the data returned is properly formatted.

  • Exercise 16.9: Load the data into your graph.

Go to the next page to start this exercise.

Exercise 16.1: Write the Cypher statement to read the actor data from a file (Instructions)


You are given the name of a file, http://data.neo4j.com/v4.0-intro-neo4j/actors.csv that you must load into your graph.

Write the Cypher statement to read the actor data from this file.

Exercise 16.1: Write the Cypher statement to read the actor data from a file (Solution)


You are given the name of a file, http://data.neo4j.com/v4.0-intro-neo4j/actors.csv that you must load into your graph.

Write the Cypher statement to read the actor data from this file.

LOAD CSV WITH HEADERS
FROM 'http://data.neo4j.com/v4.0-intro-neo4j/actors.csv'
AS line
RETURN line.id, line.name, line.birthYear

The result should be:

LoadActorsToView

Note: If you have previously downloaded the files for this course and are using Neo4j Desktop, you can place them into the import folder:

  1. In Neo4j Desktop Click Manage > Open Folder for the Neo4j instance you are using.

  2. Copy the CSV files you previously downloaded to the import folder.

  3. Adapt the code above to read from the filesystem:

LOAD CSV WITH HEADERS
FROM 'file:///actors.csv'
AS line
RETURN line.id, line.name, line.birthYear

Note: If you are using Neo4j Sandbox or Neo4j Aura, you must use the URL for the file at data.neo4j.com.

Exercise 16.2: Read the data and return it, ensuring that the data returned is properly formatted (Instructions)


Read the data and return it, ensuring that the data returned is properly formatted.

Hint: You want to use birthYear as a number, not a string.

Exercise 16.2: Read the data and return it, ensuring that the data returned is properly formatted (Solution)


Read the data and return it, ensuring that the data returned is properly formatted.

Hint: You want to use birthYear as a number, not a string.

LOAD CSV WITH HEADERS
FROM 'http://data.neo4j.com/v4.0-intro-neo4j/actors.csv'
AS line
RETURN line.id, line.name, toInteger(trim(line.birthYear))

The result should be:

LoadAndFormatActorData

Exercise 16.3: Load the data into your graph (Instructions)


Load the data into your graph where you will create Person nodes with the properties: name, born, and actorId.

Exercise 16.3: Load the data into your graph (Solution)


Load the data into your graph where you will create Person nodes with the properties: name, born, and actorId.

LOAD CSV WITH HEADERS
FROM 'http://data.neo4j.com/v4.0-intro-neo4j/actors.csv'
AS line
MERGE (actor:Person {name: line.name})
  ON CREATE SET actor.born = toInteger(trim(line.birthYear)), actor.actorId = line.id
  ON MATCH SET actor.actorId = line.id

The result returned should be:

LoadedActors

Exercise 16.4: Write the Cypher statement to read the movie data from a file (Instructions)


You are given the name of a file, http://data.neo4j.com/v4.0-intro-neo4j/movies.csv that you must load into your graph.

Write the Cypher statement to read the movie data from this file.

Exercise 16.4: Write the Cypher statement to read the movie data from a file (Solution)


You are given the name of a file, http://data.neo4j.com/v4.0-intro-neo4j/movies.csv that you must load into your graph.

Write the Cypher statement to read the movie data from this file.

LOAD CSV WITH HEADERS
FROM 'http://data.neo4j.com/v4.0-intro-neo4j/movies.csv'
AS line
RETURN line.id, line.title, line.year, line.tagLine

The result returned should be:

LoadMoviesToView

Exercise 16.5: Read the data and return it, ensuring that the data returned is properly formatted (Instructions)


Read the data and return it, ensuring that the data returned is properly formatted.

Hint: The year should be interpreted as a number and the tagline data should have no leading or trailing spaces.

Exercise 16.5: Read the data and return it, ensuring that the data returned is properly formatted (Solution)


Read the data and return it, ensuring that the data returned is properly formatted.

Hint: The year should be interpreted as a number and the tagline data should have no leading or trailing spaces.

LOAD CSV WITH HEADERS
FROM 'http://data.neo4j.com/v4.0-intro-neo4j/movies.csv'
AS line
RETURN line.id, line.title, toInteger(line.year), trim(line.tagLine)

The result returned should be:

LoadAndFormatMovies

Exercise 16.6: Load the data into your graph (Instructions)


Load the data into your graph where you will create Movie nodes with the properties: title, released, tagline, and movieId.

Exercise 16.6: Load the data into your graph (Solution)


Load the data into your graph where you will create Movie nodes with the properties: title, released, tagline, and movieId.

LOAD CSV WITH HEADERS
FROM 'http://data.neo4j.com/v4.0-intro-neo4j/movies.csv'
AS line
MERGE (m:Movie {title: line.title})
ON CREATE
  SET m.released = toInteger(trim(line.year)),
      m.movieId = line.id,
      m.tagline = line.tagLine

The result returned should be:

LoadedMovies

Exercise 16.7: Write the Cypher statement to read the relationship data from a file (Instructions)


You are given the name of a file, http://data.neo4j.com/v4.0-intro-neo4j/actor-roles.csv that you must load into your graph.

Write the Cypher statement to read the relationship data from this file.

Exercise 16.7: Write the Cypher statement to read the relationship data from a file (Solution)


You are given the name of a file, http://data.neo4j.com/v4.0-intro-neo4j/actor-roles.csv that you must load into your graph.

Write the Cypher statement to read the relationship data from this file.

LOAD CSV WITH HEADERS
FROM 'http://data.neo4j.com/v4.0-intro-neo4j/actor-roles.csv'
AS line FIELDTERMINATOR ';'
RETURN line.personId, line.movieId, line.Role

The result returned should be:

LoadRolesToView

Exercise 16.8: Read the data and return it, ensuring that the data returned is properly formatted (Instructions)


Read the data and return it, ensuring that the data returned is properly formatted.

Hint: Use split() to create the list of roles for a line.

Exercise 16.8: Read the data and return it, ensuring that the data returned is properly formatted (Solution)


Read the data and return it, ensuring that the data returned is properly formatted.

Hint: Use split() to create the list of roles for a line.

LOAD CSV WITH HEADERS
FROM 'http://data.neo4j.com/v4.0-intro-neo4j/actor-roles.csv'
AS line FIELDTERMINATOR ';'
RETURN line.personId, line.movieId, split(line.Role,',')

The result returned should be:

LoadAndFormatRoles

Exercise 16.9: Load the data into your graph (Instructions)


Load the relationship data into your graph.

Hint: You will need to use the properties actorId and movieId to find the nodes in the graph.

Exercise 16.9: Load the data into your graph (Solution)


Load the relationship data into your graph.

Hint: You will need to use the properties actorId and movieId to find the nodes in the graph.

LOAD CSV WITH HEADERS
FROM 'http://data.neo4j.com/v4.0-intro-neo4j/actor-roles.csv'
AS line FIELDTERMINATOR ';'
MATCH (movie:Movie { movieId: line.movieId })
MATCH (person:Person { actorId: line.personId })
MERGE (person)-[:ACTED_IN { roles: split(line.Role,',')}]->(movie)

The result returned should be:

LoadedRoles

Exercise 16: Importing data (Summary)


In this exercise you wrote Cypher statements to load data that is properly formatted into the graph.