apoc.load.csv

Procedure APOC Full

apoc.load.csv('urlOrBinary',{config}) YIELD lineNo, list, map - load CSV from URL as stream of values, config contains any of: {skip:1,limit:5,header:false,sep:'TAB',ignore:['tmp'],nullValues:['na'],arraySep:';',mapping:{years:{type:'int',arraySep:'-',array:false,name:'age',ignore:false}}

Signature

apoc.load.csv(urlOrBinary :: ANY?, config = {} :: MAP?) :: (lineNo :: INTEGER?, list :: LIST? OF ANY?, strings :: LIST? OF STRING?, map :: MAP?, stringMap :: MAP?)

Input parameters

Name Type Default

urlOrBinary

ANY?

null

config

MAP?

{}

Config parameters

The procedure support the following config parameters:

Table 1. Config parameters
name type default description

skip

boolean

none

skip result rows

limit

Long

none

limit result rows

header

booelan

true

indicates if file has a header

sep

String

','

separator character or 'TAB'

quoteChar

String

'"'

the char to use for quoted elements

escapeChar

String

'\'

the char to use for escape elements. Use "NONE" if you don’t want to use any characters

arraySep

String

';'

array separator

ignore

List<String>

[]

which columns to ignore

nullValues

List<String>

[]

which values to treat as null, e.g. ['na',false]

mapping

Map

{}

per field mapping, entry key is field name, .e.g {years:{…​.} see below

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) See the Binary file example

Output parameters

Name Type

lineNo

INTEGER?

list

LIST? OF ANY?

strings

LIST? OF STRING?

map

MAP?

stringMap

MAP?

Reading from a file

By default importing from the file system is disabled. We can enable it by setting the following property in apoc.conf:

apoc.conf
apoc.import.file.enabled=true

If we try to use any of the import procedures without having first set this property, we’ll get the following error message:

Failed to invoke procedure: Caused by: java.lang.RuntimeException: Import from files not enabled, please set apoc.import.file.enabled=true in your apoc.conf

Import files are read from the import directory, which is defined by the dbms.directories.import property. This means that any file path that we provide is relative to this directory. If we try to read from an absolute path, such as /tmp/filename, we’ll get an error message similar to the following one:

Failed to invoke procedure: Caused by: java.lang.RuntimeException: Can’t read url or key file:/path/to/neo4j/import/tmp/filename as json: /path/to/neo4j//import/tmp/filename (No such file or directory)

We can enable reading files from anywhere on the file system by setting the following property in apoc.conf:

apoc.conf
apoc.import.file.use_neo4j_config=false

Neo4j will now be able to read from anywhere on the file system, so be sure that this is your intention before setting this property.

Usage Examples

test.csv contains people and their properties:

test.csv
name,age,beverage
Selma,9,Soda
Rana,12,Tea;Milk
Selina,19,Cola

We’ll place this file into the import directory of our Neo4j instance.

We can load this file and return the contents by running the following query:

CALL apoc.load.csv('test.csv')
YIELD lineNo, map, list
RETURN *;
Table 2. Results
lineNo list map

0

["Selma", "9", "Soda"]

{name: "Selma", age: "9", beverage: "Soda"}

1

["Rana", "12", "Tea;Milk"]

{name: "Rana", age: "12", beverage: "Tea;Milk"}

2

["Selina", "19", "Cola"]

{name: "Selina", age: "19", beverage: "Cola"}

Binary file

You can also import a file from a binary byte[] (not compressed) or a compressed file (allowed compression algos are: GZIP, BZIP2, DEFLATE, BLOCK_LZ4, FRAMED_SNAPPY).

CALL apoc.load.csv(`binaryGzipByteArray`, {compression: 'GZIP'})

or:

CALL apoc.load.csv(`binaryFileNotCompressed`, {compression: 'NONE'})

For example, this one works well with apoc.util.compress function:

WITH apoc.util.compress("name,age\nSelma,8\nRana,11\nSelina,18", {compression: 'DEFLATE'}) as csvCompressed
CALL apoc.load.csv(csvCompressed, {compression: 'DEFLATE'})
YIELD lineNo, map, list
RETURN lineNo, map, list
Table 3. Results
lineNo list map

0

["Selma", "8"]

{name: "Selma", age: "8"}

1

["Rana", "11"]

{name: "Rana", age: "11"}

2

["Selina", "18"]

{name: "Selina", age: "18"}