How to load a local CSV file in Neo4j in Mac, Linux or Windows

Neo4j is pretty efficient loading a large amount of data. Whether you are loading a raw data or data that you extracted as CSV from another database, you can easily load those to Neo4j Graph Database and create Nodes and Relationships as the data is loaded.

In this tutorial, I am uploading a simple CSV file with headers, few lines from that CSV files are as listed below, for your reference.

Municipality,District
Kathmandu,Kathmandu
Pokhara,Kaski
Lalitpur,Lalitpur
Biratnagar,Morang

Here is a command I can run on the Neo4j console to load this file and then create relationships.

LOAD CSV WITH HEADERS FROM 'file:///Users/kushal/Desktop/Nepal Data/Municipalities_of_nepal.csv' AS line
MERGE(d:District{name:line.District})
CREATE (m:Municipality { name: line.Municipality})
CREATE (d-[:MUNICIPALITY]->(m)
return d,m;

If you are using windows, then you would have to provide the URL as, for example:

file://C:/Users/kushal/Desktop/Nepal Data/Municipalities_of_nepal.csv

The good thing is Neo4j also supports loading data from URLs. So let’s say the CSV file was on the internet, I could load it using the following Cipher.

LOAD CSV WITH HEADERS FROM 'http://www.icodejava.com/data/doesnotexist/Municipalities_of_nepal.csv' AS line
MERGE(d:District{name:line.District})
CREATE (m:Municipality { name: line.Municipality})
CREATE (d-[:MUNICIPALITY]->(m)
return d,m;
Tagged , , , , , . Bookmark the permalink.

One Response to Neo4j Cheat Sheet – How to do certain things in Neo4j Graph Database

  1. adam says:

    I tried almost every combination to load my data sample from csv to neo4j but it doesnt work. can you help me ? i am trying to load from my local machine and it is giving understandable errors.

Leave a Reply

Your email address will not be published. Required fields are marked *