Neo4j: Loading CSV data to Neo4j database
- Krishnaraj Rajagopal
- Feb 15, 2018
- 1 min read
Use the following steps to load data in a csv file to Neo4j. Ensure the CSV file has an header to each column to be imported and the header name is does not have special characters or spaces.
Steps
1. Copy the CSV file to import folder in neo4j installation directory
2. Execute the following Cypher query to upload the data to Neo4j
LOAD CSV WITH HEADERS FROM "file:///filename.csv" AS variable <Cypher Query- Use variable.header-name to refer the field in the CSV file>
Example 1:
The following example will load node and properties from CSV data.
LOAD CSV WITH HEADERS FROM "file:///TestFile.csv" AS csvLine
MERGE (system:System {name:csvLine.SystemName, sysID:csvLine.SystemID});
TestFile.csv is the CSV file
csvLine is the lines from the CSV file
csvLine.SystemName is the field from CSV file with header as SystemName
csvLine.SystemID is the field from CSV file with header as SystemID
Example 2:
The following example will verify existence of nodes in the database with the data in CSV and load the relationships
LOAD CSV WITH HEADERS FROM "file:///TestFile2.csv" AS csvLine MATCH (system:System {name:csvLine.SystemName}), (function:Function {name:csvLine.FunctionName}) MERGE (system)-[:REALIZES {source: csvLine.Source}]->(function);
Comments