apoc.load.csv

Procedure APOC Full

apoc.load.csv('url',{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(url :: STRING?, config = {} :: MAP?) :: (lineNo :: INTEGER?, list :: LIST? OF ANY?, strings :: LIST? OF STRING?, map :: MAP?, stringMap :: MAP?)

Input parameters

Name Type Default

url

STRING?

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

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

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"}