Neo4j – How to Read CSV file

Neo4j CSV import create node Neo4j How to Read CSV file

Today in this article, we will see how to read a CSV file into Neo4j,.We will use the LOAD CSV Cypher clause.

One common task in data management is importing data from external sources, such as CSV or Excel files, into Neo4j.

In this article, we’ll explore how to efficiently read CSV files into Neo4j, complete with practical examples to guide you through the process.

LOAD CSV clause in the Neo4j browser allows you to import data from CSV files directly into your Neo4j graph database.

Neo4j, being a graph database, organizes data into nodes, relationships, and properties.

On the other hand, CSV (Comma-Separated Values) files are a popular format for storing tabular data.

When importing CSV data into Neo4j, each row typically corresponds to a node, with columns representing node properties or relationships.

Example CSV Data

We will use below sample CSV file to import

Cypher Import Commands

Open Neo4j database management interface (Neo4j Browser or Neo4j Desktop).

Please use below Cypher, Neo4j’s query language, to import the CSV data.

LOAD CSV WITH HEADERS FROM 'file:///your_file.csv' AS row return line 

The above logic loads the specified file into a memory object called row which we are returning line by line just for validation purposes.

load all the columns from a csv into neo4j nodes
image 1 Neo4j How to Read CSV file

LOAD CSV WITH HEADERS FROM 'file:///your_file.csv' AS row
CREATE (:Movie{id: toInteger(row.id), title: row.title, released: row.released,  tagline: row.tagline}))

Please see below details on the command used

Here’s a breakdown of what this query does:

  • LOAD CSV WITH HEADERS:
    • This clause loads the CSV file and assumes the first row contains headers that define the column names.

  • FROM 'file:///data.csv':
    • Specifies the path to the CSV file. In this example, the file is assumed to be in the import directory of the Neo4j database.

  • AS row:
    • Defines an alias row that represents each row in the CSV file.

  • LOAD CSV WITH HEADERS FROM ‘file:///data.csv’ AS row CREATE (:Movie{id: toInteger(row.id), title: row.title, released: row.released, tagline: row.tagline}))
    • Creates a new node for each row in the CSV file with the label Movie and properties id, title, and released date and tagline details that are used to convert the id and age values from strings to integers.

That’s all! Happy coding!

Does this help you fix your issue?

Do you have any better solutions or suggestions? Please sound off your comments below.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

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