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.

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

filename

'file:/students.csv'

labels

set of labels

['Person', 'Student']

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

filename

'file:/works_at.csv'

type

relationship type

'WORKS_AT'

The <config> parameter is a map containing optional configurations.

The procedure support the following config parameters:

Table 1. Config parameters
name type default description import tool counterpart

delimiter

STRING

,

delimiter character between columns

--delimiter=,

arrayDelimiter

STRING

;

delimiter character in arrays

--array-delimiter=;

ignoreDuplicateNodes

BOOLEAN

false

for duplicate nodes, only load the first one and skip the rest (true) or fail the import (false)

--ignore-duplicate-nodes=false

quotationCharacter

STRING

"

quotation character

--quote='"'

stringIds

BOOLEAN

true

treat ids as strings

--id-type=STRING

skipLines

INTEGER

1

lines to skip (incl. header)

N/A

ignoreBlankString

BOOLEAN

false

if true ignore properties with a blank string

N/A

ignoreEmptyCellArray

BOOLEAN

false

if true ignore array properties containing a single empty string, like the import tool

N/A

compression

Enum[NONE, BYTES, GZIP, BZIP2, DEFLATE, BLOCK_LZ4, FRAMED_SNAPPY]

null

Allow taking binary data, either not compressed (value: NONE) or compressed (other values) .

N/A

charset

STRING

'UTF-8'

name of the character extending java.nio.Charset in the currently used JDK. E.g.: US-ASCII, ISO-8859-1, UTF-8, UTF-16

--input-encoding

batchSize

INTEGER

2000

commits and continues after the defined number of rows have been processed

N/A

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:

persons.csv
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.

persons.csv
:ID|name:STRING|speaks:STRING[]
1|John|en,fr
2|Jane|en,de
knows.csv
: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.