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.
Download and install Neo4J on your machine.
Neo4j Desktop Download - Launch and Manage Neo4j Databases
Create and manage local Neo4j databases and connect to remote graphs. Includes Neo4j Enterprise features, APOC, Neo4j…
Next, start Neo4J and create a new “Local Graph” named “user_sample”.
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.
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.
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 linereads the CSV file line by line taking the first line as the header.
created_atis imported as a date data type. See docs for more date-time formatting options.
- We are using
CREATEso that if a node already exists then it's updated instead of creating a new node.
- We are using the
SPLITfunction to split the wallet IDs to
- Finally, we use
FOREACHto iterate all
Walletnode using it.
Now, that you have imported the data, you can visualize it in Neo4J.
That’s it for this post. I hope you would find this post useful.