LOAD CSV Cypher clause
The LOAD CSV
clause enables you to load and use data from a CSV file of your
choosing in a row-based manner, within a query. We support the Excel CSV
dialect, as it's the most commonly used one. For the syntax of the clause,
please check the Cypher manual.
The clause reads row by row from a CSV file and binds the contents of the parsed row to the variable you specified.
our Reference guide.
To work with the LOAD CSV clause, we need to have access to our files. If working with Docker, check our Docker guide on how to access files from your local filesystem:
Below, you can find two examples of how to use the CSV Import Tool depending on the complexity of your data:
Examples
One type of nodes and relationships
- With CSV header
- Without CSV header
Depending on how you set the HEADER
option (WITH
or NO
), a row will be
parsed as either a map or a list.
To access a given field, you can use the property lookup syntax. Let's assume
that the contents of people_nodes.csv
are as follows:
id,name
100,Daniel
101,Alex
102,Sarah
103,Mia
104,Lucy
The contents of people_relationships.csv
are the following:
id_from,id_to
100,101
100,102
100,103
101,103
102,104
The following query will load row by row from the file, and create a new node for each row with properties based on the parsed row values:
LOAD CSV FROM "/path-to/people_nodes.csv" WITH HEADER AS row
CREATE (n:Person {id: row.id, name: row.name});
With the initial nodes in place, you can now create relationships between them:
LOAD CSV FROM "/path-to/people_relationships.csv" WITH HEADER AS row
MATCH (p1:Person {id: row.id_from}), (p2:Person {id: row.id_to})
CREATE (p1)-[:IS_FRIENDS_WITH]->(p2)
Let's assume that the contents of people_nodes.csv
are as follows:
100,Daniel
101,Alex
102,Sarah
103,Mia
104,Lucy
The contents of people_relationships.csv
are the following:
100,101
100,102
100,103
101,103
102,104
The following query will load row by row from the file, and create a new node for each row with properties based on the parsed row values:
LOAD CSV FROM "/path-to/people_nodes.csv" NO HEADER AS row
CREATE (n:Person {id: row[0], name: row[1]}) ;
With the initial nodes in place, you can now create relationships between them:
LOAD CSV FROM "/path-to/people_relationships.csv" NO HEADER AS row
MATCH (p1:Person {id: row[0]}), (p2:Person {id: row[1]})
CREATE (p1)-[:IS_FRIENDS_WITH]->(p2)
Multiple types of nodes and relationships
In the case of a more complex graph, we have to deal with multiple node and relationship types. Let's assume we have the following example:
- 1. people_nodes.csv
- 2. people_relationships.csv
- 3. restaurants_nodes.csv
- 4. restaurants_relationships.csv
Add the following to the file people_nodes.csv
:
id,name,age,city
100,Daniel,30,London
101,Alex,15,Paris
102,Sarah,17,London
103,Mia,25,Zagreb
104,Lucy,21,Paris
The following query will load row by row from the file, and create a new node for each row with properties based on the parsed row values:
LOAD CSV FROM "/path-to/people_nodes.csv" WITH HEADER AS row
CREATE (n:Person {id: row.id, name: row.name, age: ToInteger(row.age), city: row.city});
Each person from people_relationships.csv
is connected to another person they are
friends with. This is represented with the following example:
first_person,second_person,met_in
100,102,2014
103,101,2021
102,103,2005
101,104,2005
104,100,2018
101,102,2017
100,103,2001
The following query will create relationships between the people nodes:
LOAD CSV FROM "/path-to/people_relationships.csv" WITH HEADER AS row
MATCH (p1:Person {id: row.first_person})
MATCH (p2:Person {id: row.second_person})
CREATE (p1)-[f:IS_FRIENDS_WITH]->(p2)
SET f.met_in = row.met_in;
We have a list of restaurants people ate at in the file restaurants_nodes.csv
:
id,name,menu
200, Mc Donalds, Fries;BigMac;McChicken;Apple Pie
201, KFC, Fried Chicken;Fries;Chicken Bucket
202, Subway, Ham Sandwich;Turkey Sandwich;Foot-long
203, Dominos, Pepperoni Pizza;Double Dish Pizza;Cheese filled Crust
The following query will create new nodes for each restaurant:
LOAD CSV FROM "/path-to/restaurants_nodes.csv" WITH HEADER AS row
CREATE (n:Restaurant {id: row.id, name: row.name, menu: row.menu});
We file restaurants_relationships.csv
contains a list of people and the
restaurants they visited:
PERSON_ID,REST_ID,liked
100,200,true
103,201,false
104,200,true
101,202,false
101,203,false
101,200,true
102,201,true
The following query will create relationships between people and restaurants where they ate:
LOAD CSV FROM "/path-to/restaurants_relationships.csv" WITH HEADER AS row
MATCH (p1:Person {id: row.PERSON_ID})
MATCH (re:Restaurant {id: row.REST_ID})
CREATE (p1)-[ate:ATE_AT]->(re)
SET ate.liked = ToBoolean(row.liked);