How to Import Data to Neo4J from CSV files

Vivek Maskara
Jun 26, 2020 · 3 min read
Image for post
Image for post
Neo4J Database visualization.

In this post, we will see how to import data to the Neo4J database from CSV files.

Recently, I worked with Graph databases for the first time and was really amazed by the capabilities it offers. Initially, I struggled to find a good resource that guides beginners to import CSV data to the Neo4J database. So I thought it might be useful to share the steps using a real-world example.

Prerequisite

Download and install Neo4J on your machine.

Next, start Neo4J and create a new “Local Graph” named “user_sample”.

Image for post
Image for post

Preparing the CSV

Let us assume that you have the following CSV file with User data. I have chosen an example with different types of data values to explore various common scenarios.

Image for post
Image for post
Sample User data

If your data is stored in an Excel sheet saved on Google Drive then you can directly import the data by first publishing the file on the web. You can go to File > Publish to Web to get a link.

Image for post
Image for post
Use publish to web option in Google Docs

Note: If you try to use the doc link directly, it won’t work.

  • If you have a link to a CSV hosted elsewhere, you can use that directly.
  • If you have a locally stored CSV file, you can follow the instructions here to copy the CSV to the appropriate path and then use the file path.

Let’s see how can you import this data to Neo4J.

Importing Data from CSV

You can choose to structure your nodes and relationships in any way that suits your application but for this example, I have decided to create separate nodes for address, phone number, email, and user.

Let us start off by defining uniqueness constrains for these nodes.

This will ensure that a duplicate node is not created for the same data.

Finally, let us import the CSV data. We will use the LOAD CSV Cypher statement to import data from CSV. Start the Neo4J graph DB and execute the query in the Cypher query window.

Let us discuss a few things about the above query:

  • LOAD CSV WITH HEADERS FROM <link> WITH line reads the CSV file line by line taking the first line as the header.
  • created_at is imported as a date data type. See docs for more date-time formatting options.
  • We are using MERGE instead of CREATE so that if a node already exists then it's updated instead of creating a new node.
  • We are using the SPLIT function to split the wallet IDs to wallets variable.
  • Finally, we use FOREACH to iterate all wallets and create Wallet node using it.

Now, that you have imported the data, you can visualize it in Neo4J.

Image for post
Image for post

That’s it for this post. I hope you would find this post useful.

Quick Code

Find the best tutorials and courses for the web, mobile…

By Quick Code

Receive weekly updates about new posts on programming, development, data science, web development and more Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Vivek Maskara

Written by

Grad Student at ASU | Student Researcher at The Luminosity Lab | Ex Senior Software Engineer, Zeta | Volunteer, Wikimedia Foundation

Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Vivek Maskara

Written by

Grad Student at ASU | Student Researcher at The Luminosity Lab | Ex Senior Software Engineer, Zeta | Volunteer, Wikimedia Foundation

Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium