Neo4j is a popular graph database that allows you to store data in nodes and relationships. It provides a simple way to import data from CSV files into the database using the LOAD CSV command.
Prerequisites
Before importing a CSV file into Neo4j, you need:
- A running Neo4j database instance
- The CSV file containing the data to be imported in a specific format
- Optional headers in the first row of the CSV file for naming nodes/relationships
- Access to Neo4j Browser or Neo4j Desktop to run Cypher queries
CSV File Format
The CSV file should contain data in a specific format for Neo4j to correctly import it:
- The first row should contain headers for each column
- Subsequent rows contain node or relationship data
- For nodes – the first column specifies labels and other columns are properties
- For relationships – first two columns specify start node, end node, third column specifies type and remaining are properties
Here is an example CSV file containing node data:
Name | Age | Gender |
---|---|---|
John | 28 | Male |
Lisa | 31 | Female |
And here is an example CSV file containing relationship data:
Start Node | End Node | Type | Since |
---|---|---|---|
John | Lisa | FRIEND | 2015 |
Import using LOAD CSV
Once the CSV file is formatted properly, importing it into Neo4j is simple using the LOAD CSV command.
The syntax is:
LOAD CSV WITH HEADERS FROM "file.csv" AS row CREATE (n:Label {props})
For example, to import the nodes CSV file:
LOAD CSV WITH HEADERS FROM "nodes.csv" AS row CREATE (:Person {name: row.Name, age: toInteger(row.Age), gender: row.Gender})
And to import the relationships CSV file:
LOAD CSV WITH HEADERS FROM "relationships.csv" AS row MATCH (a:Person {name: row.Start Node}),(b:Person {name: row.End Node}) CREATE (a)-[r:FRIEND {since: row.Since}]->(b)
The WITH HEADERS option indicates the first row contains headers. The AS row maps each row to a variable named row. Then CREATE clause uses row to access each column and create nodes/relationships.
Import CSV via Neo4j Browser
You can easily import a CSV file via the Neo4j Browser:
- Click “Manage” in the left menu
- Click “Open Folder” under “Import”
- Copy your CSV file into the import folder
- Click “Update” to refresh the view
- Click “Import” for the CSV file
- Check headers option if CSV contains headers
- Click “Import”
This will open up a query to load the CSV file, which you can then run.
Import CSV with Neo4j Desktop
If you use Neo4j Desktop, importing a CSV is also straightforward:
- Click “Manage” in the top menu
- Click “Open Folder” under “Import”
- Copy the CSV file to import folder
- In the database view, click “Manage”
- Click “Import”
- Select the CSV file and click “Import”
- Check headers option if required
- Click “Import”
This will also construct the LOAD CSV query and run it to import the data into Neo4j.
Import CSV Programmatically
You can also import a CSV file into Neo4j programmatically from languages like Python, Java, etc. Here is some sample Python code to load a CSV file:
import csv from neo4j import GraphDatabase uri = "bolt://localhost:7687" driver = GraphDatabase.driver(uri, auth=("neo4j", "password")) def load_nodes(tx, file): with open(file) as csvfile: reader = csv.reader(csvfile) next(reader) # Skip header row for row in reader: tx.run("CREATE (:Person {name: $name, age: $age, gender: $gender})", name=row[0], age=row[1], gender=row[2]) def load_rels(tx, file): with open(file) as csvfile: reader = csv.reader(csvfile) next(reader) for row in reader: tx.run("MATCH (a:Person {name: $start}),(b:Person {name: $end}) " "CREATE (a)-[r:FRIEND {since: $since}]->(b)", start=row[0], end=row[1], since=row[2]) with driver.session() as session: session.write_transaction(load_nodes, "nodes.csv") session.write_transaction(load_rels, "relationships.csv")
This shows opening the CSV files, parsing them using the CSV library, and using Cypher within write transactions to import the data.
Conclusion
Importing CSV files provides a simple way to get data into Neo4j. By following the steps above you can quickly load nodes and relationships into your Neo4j database from CSV files:
- Format CSV file properly with headers, node data and relationship data
- Use LOAD CSV command with headers and row variable to match CSV columns
- Import via Browser or Desktop by copying CSV to import folder
- Write code to load CSV data programmatically from languages like Python
With Neo4j’s flexible Cypher query language, you can then start analyzing and querying your graph data in powerful ways.