How to Import a CSV File In Neo4j Desktop
In this article, you will learn how to import data from CSV files to a Neo4j graph database using the Neo4j Desktop application.
This tutorial is designed for you to follow along and step through the process.
Step 1: Creating and starting the Neo4j instance
When you open Neo4j Desktop for the first time you will see a Neo4j Primer Project with the Movie Database already started as shown here:
Next, you will create a new project in Neo4j Desktop. You can only have a single DBMS running, so you must first stop the DBMS that is started by clicking the Stop button:
You should now see that there is no active DBMS:
Add a new project by clicking New at the top of the sidebar:
This creates a project named Project:
You can change the name of a project by hovering over the area to the right of the project name and selecting the Edit button:
Then type a name for the project and select the “Check” button to save it
In a Neo4j Desktop project, you can create one or more DBMSs. Next, you will create a local DBMS in a project and start it.
In the project where you want to add the DBMS, click the Add button, and then select Local DBMS:
This opens a dialog where you will specify the details of the DBMS.
You can use the default name for the DBMS, which is Graph DBMS, but you should name it something that helps to identify the use case for the DBMS.
You must also specify a password for the DBMS so enter a password that you will remember and click create
And here is what you should see after the DBMS is successfully created:
You cannot have more than one DBMS started. If you have no other DBMSs started in Neo4j Desktop, you can start your newly-created DBMS by hovering to the right of the DBMS name and clicking the Start button:
The DBMS will take a few seconds to start. After it is started, you should see something like this:
After the DBMS is started, you can access it with clients running on your system such as Neo4j Browser, Neo4j Bloom, etc. In Neo4j Desktop, the DBMS is an Enterprise Server, but it can only be accessed locally.
Step 2: Putting CSV files in the import folder
First, download this zip file. Uncompress/unzip this file which should yield three CSV files for products, orders, and order details. You will put these files in the import folder that Neo4j expects for imports.
Open a finder window on your system by hovering over the three dots to the right started DBMS and selecting Open folder, and then Import:
This will open the import folder. Copy or move the three CSV files into this import folder on your system:
Now that your files are in the import folder, you can import the data into the database managed by the DBMS. We will use the current table and column format in the CSV files and translate it into nodes and relationships.
This can be done a few different ways, but we will use Cypher’s LOAD CSV command in this tutorial.
Step 3: LOAD CSV Files
LOAD CSV is a built-in command in Cypher that allows you to read CSV files and append regular Cypher statements to create or update the data as a graph. You can also use LOAD CSV without creating the graph to output samples, counts, or distributions. This helps to detect incorrect header column counts, delimiters, quotes, escapes, or spelling of header names before the data is written and stored.
To open Cypher Shell, Click the drop-down menu to the right of the Open button and select Terminal.
First, you have to check how many lines are in the CSV files to ensure they didn’t get corrupted or cut off from a potential export process. For the files with headers, you simply add the WITH HEADERS clause after LOAD CSV, so that it excludes the header row in the count and only counts the rows of data.
The Cypher query for it will be:
//count data rows in products.csv (no headers) LOAD CSV FROM 'file:///products.csv' AS row RETURN count(row);
//count data rows in orders.csv (headers) LOAD CSV WITH HEADERS FROM 'file:///orders.csv' AS row RETURN count(row);
//count data rows in order-details.csv (headers) LOAD CSV WITH HEADERS FROM 'file:///order-details.csv' AS row RETURN count(row);
After running these statements you should get the following counts:
- 77 rows for products.csv
- 830 rows for orders.csv
- 2155 rows for order-details.csv