Skip to main content
Version: 2.2.1

LOAD CSV Cypher clause

Related - Reference Guide

The LOAD CSV Cypher clause enables you to load and use data from a CSV file of your choosing in a row-based manner within a query. Memgraph supports the Excel CSV dialect, as it's the most common one. For the clause syntax, 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.

info

For more detailed information about the LOAD CSV Cypher clause, check our Reference guide.

Transferring CSV files into a Docker container

When working with the LOAD CSV clause, Memgraph needs to have access to the CSV files you are using. If you are working with Docker, you need to transfer those files into the container.

Transfer CSV files into a Docker container
If you are using Docker to run Memgraph, you will need to copy the files from your local directory into the Docker container where Memgraph can access them.

1. Start your Memgraph instance using Docker.

2. Open a new terminal and find the CONTAINER ID of the Memgraph Docker container:

docker ps

3. Copy a file from your current directory to the container with the command:

docker cp ./file_to_copy.csv <CONTAINER ID>:/file_to_copy.csv

The file is now inside your Docker container, and you can import it using the LOAD CSV clause.

Examples

Below, you can find two examples of how to use the LOAD CSV clause depending on the complexity of your data:

One type of nodes and relationships

Let's import a simple dataset.

If the CSV file has a header, set the HEADER option to WITH. Each row will be parsed as a map, and you can use the property lookup syntax (e.g. id: row.id) to access a certain field.

If the CSV file doesn't have a header, set the HEADER option to NO. Each row will be parsed as a list, and you can access elements by defining the position of the element in the list.

Download the people_nodes.csv file with the following content:

id,name
100,Daniel
101,Alex
102,Sarah
103,Mia
104,Lucy

Download the people_relationships.csv file with the following content:

id_from,id_to
100,101
100,102
100,103
101,103
102,104

If you are using Docker, transfer the CSV files into a Docker container.

The following query will load row by row from the CSV 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 (p:Person {id: row.id, name: row.name});

If successful, you should receive an Empty set (0.014 sec) message.

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);
This is how the graph should look like in Memgraph after the import:
Run the following query:
MATCH p=()-[]-() RETURN p;


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 say we want to create a graph like this:

We will create that graph by using LOAD CSV clause to import four CSV files.

Download the people_nodes.csv file, content of which is:

id,name,age,city
100,Daniel,30,London
101,Alex,15,Paris
102,Sarah,17,London
103,Mia,25,Zagreb
104,Lucy,21,Paris

If you are using Docker, transfer the CSV files into a Docker container.

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});
This is how the graph should look like in Memgraph after the import:
Run the following query:
MATCH (p) RETURN p;

Now move on to the people_relationships.csv file.