Import CSV
CSV files that comply with the Neo4j import tool’s header format can be imported using the apoc.import.csv
procedure.
This procedure can be used to load small- to medium-sized data sets in an online database.
For importing larger data sets, it is recommended to perform a batch import using the import tool, which loads data in bulk to an offline (initially empty) database.
This procedure should not be confused with the apoc.load.csv
procedure which loads the lines of the CSV file to lists/maps (to be used to create the entities in the database).
Usage
The parameters of the apoc.import.csv(<nodes>, <relationships>, <config>)
procedure are as follows.
The <nodes>
parameter is a list, where each element is a map defining a source file (fileName
) to be loaded with a set of labels (labels
):
name | description | example |
---|---|---|
|
filename |
|
|
set of labels |
|
The <relationships>
parameter is also a list, where each element is a map defining a source file (fileName
) to be loaded with a given relationship type (type
):
name | description | example |
---|---|---|
|
filename |
|
|
relationship type |
|
The <config>
parameter is a map
name | description | default | import tool counterpart |
---|---|---|---|
|
delimiter character between columns |
|
|
|
delimiter character in arrays |
|
|
|
for duplicate nodes, only load the first one and skip the rest (true) or fail the import (false) |
|
|
|
quotation character |
|
|
|
treat ids as strings |
|
|
|
lines to skip (incl. header) |
|
|
Examples for apoc.import.csv
Loading nodes
Given the following CSV file and procedure call, the database loads two Person
nodes with their name
properties set:
name:STRING John Jane
CALL apoc.import.csv([{fileName: 'file:/persons.csv', labels: ['Person']}], [], {})
Loading nodes and relationships
Given the following CSV files and procedure call, the database loads two Person
nodes and a KNOWS
relationship between them (with the value of the since
property set). Note that both the field terminators and the array delimiters are changed from the default value, and the CSVs use numeric ids.
:ID|name:STRING|speaks:STRING[] 1|John|en,fr 2|Jane|en,de
:START_ID|:END_ID|since:INT 1|2|2016
CALL apoc.import.csv(
[{fileName: 'file:/persons.csv', labels: ['Person']}],
[{fileName: 'file:/knows.csv', type: 'KNOWS'}],
{delimiter: '|', arrayDelimiter: ',', stringIds: false}
)
The loader supports advanced features of the import tool:
-
ID spaces are supported, using the import tool’s syntax.
-
Node labels can be specified with the
:LABEL
field. -
Relationship types can be specified with the
:TYPE
field.