neo4j
  1. neo4j-imort-data-from-csv

Import Data from CSV in Neo4j CQL

Importing data from CSV (Comma Separated Values) files is a common requirement for many applications. Neo4j CQL provides a simple and efficient way to import data from CSV files into a Neo4j database.

Syntax

The syntax for importing data from CSV in Neo4j CQL is as follows:

LOAD CSV WITH HEADERS FROM "file:///path/to/file.csv" AS row

Example

Consider the following CSV file employees.csv:

id,name,email,department
1,John Doe,johndoe@example.com,IT
2,Jane Smith,janesmith@example.com,HR
3,Bob Johnson,bobjohnson@example.com,Sales

To import this data into a Neo4j database, the following Neo4j CQL command can be used:

LOAD CSV WITH HEADERS FROM "file:///employees.csv" AS row
CREATE (:Employee {id: toInteger(row.id), name: row.name, email: row.email, department: row.department})

Output

The above example will create three Employee nodes in the Neo4j database, with the properties id, name, email, and department set to the values in the CSV file.

Explanation

The LOAD CSV WITH HEADERS clause is used to load the CSV file and create a virtual table containing the contents of the file. The FROM keyword is used to specify the location of the CSV file on the system.

The AS row clause assigns the alias row to each row in the virtual table. This allows us to access the values of each column in the CSV file using the row.column_name syntax.

The CREATE clause is used to create a new Employee node in the Neo4j database with the properties set to the values in the CSV file.

In the CREATE clause, the toInteger function is used to convert the id value from a string to an integer.

Use

Importing data from CSV files is a common requirement for many applications. Neo4j CQL provides a simple and efficient way to import data from CSV files into a Neo4j database. This allows data to be loaded into the database quickly and easily, without the need for manual entry.

Important Points

  • The CSV file must have a header row containing the column names.
  • The location of the CSV file must be specified using the file:/// prefix.
  • The toInteger function can be used to convert string values to integers.

Summary

Importing data from CSV files into Neo4j is a common requirement for many applications. Neo4j CQL provides a simple and efficient way to import data from CSV files into a Neo4j database. The LOAD CSV WITH HEADERS clause is used to load the CSV file and create a virtual table containing the contents of the file. The AS row clause assigns the alias row to each row in the virtual table, allowing us to access the values of each column in the CSV file using the row.column_name syntax. The CREATE clause is used to create new nodes in the Neo4j database with the properties set to the values in the CSV file.

Published on: