Loading CSV files

In this lesson, you’ll learn how to use the LOAD CSV Cypher clause, including:

  • The LOAD CSV syntax

  • How to load headers within a CSV file

  • Options for accessing a file

LOAD CSV

The LOAD CSV Cypher clause reads data from a CSV file and returns the rows in the file.

LOAD CSV has the following syntax:

cypher
LOAD CSV [WITH HEADERS] FROM url [AS alias] [FIELDTERMINATOR char]

You can specify whether the file has a header row, the file location and the field terminator

You are going to load a CSV file that contains people data:

csv
personId,name,birthYear
23945,Gerard Pires,1942
553509,Helen Reddy,1941
113934,Susan Flannery,1939

In the Sandbox run the following Cypher statement to load the people.csv file:

cypher
LOAD CSV WITH HEADERS
FROM 'https://data.neo4j.com/importing-cypher/people.csv'
as row
RETURN row

Review the data returned. Note the number of rows returned and the header names match those in the CSV file.

json
{
  "birthYear": "1942",
  "name": "Gerard Pires",
  "personId": "23945"
}

The FIELDTERMINATOR wasn’t specified in the LOAD CSV clause because the default value is a comma. If the field terminator was a pipe character, the LOAD CSV clause would need to include the field terminator:

cypher
LOAD CSV WITH HEADERS
FROM 'https://data.neo4j.com/importing-cypher/people.csv'
as row FIELDTERMINATOR '|'
RETURN row

Counting rows

A simple check you can do to ensure all rows are loaded is to count the number of rows in the CSV file and compare it to the number of rows returned by the LOAD CSV clause.

You can return the number of rows in the CSV file using the COUNT function.

cypher
LOAD CSV WITH HEADERS
FROM 'https://data.neo4j.com/importing-cypher/people.csv'
as row
RETURN count(row)

Accessing files

LOAD CSV can access files on a Neo4j server’s file system or a remote machine.

The Sandbox in this course and Aura DB cloud service only allow access to remote files.

You can find more information in the Reading CSV Files section of the Neo4j documentation.

Check Your Understanding

Which of the following are valid LOAD CSV statements?

  • LOAD CSV FROM 'file:///data.csv' AS line

  • LOAD CSV INCLUDING HEADERS FROM 'file:///data.csv' AS line

  • LOAD CSV FROM 'file:///data.csv' AS line FIELDTERMINATOR '$'

Hint

You can specify the file location, the field terminator, and whether the file has a header row.

Solution

LOAD CSV INCLUDING HEADERS FROM 'file:///data.csv' AS line the correct syntax is LOAD CSV WITH HEADERS FROM 'file:///data.csv' AS line

Summary

In this lesson, you learned:

  • The LOAD CSV Cypher syntax.

  • How to use LOAD CSV to load a CSV file.

  • How to change the field terminator.

In the next lesson, you can take the optional challenge of loading your own CSV file.