apoc.cypher.runFileReadOnly

Procedure APOC Full

apoc.cypher.runFileReadOnly(file or url,[{statistics:true,timeout:10,parameters:{}}]) - runs each READ statement in the file, all semicolon separated - currently no schema operations

Signature

apoc.cypher.runFileReadOnly(file :: STRING?, config = {} :: MAP?) :: (row :: INTEGER?, result :: MAP?, fileName :: STRING?)

Input parameters

Name Type Default

file

STRING?

null

config

MAP?

{}

Config parameters

The procedure support the following config parameters:

Table 1. Config parameters
name type default description

reportError

boolean

false

Returns a entry row with key error and value the error occurred, if any.

statistics

boolean

true

Returns an additional row with the query statistics, leveraging the org.neo4j.graphdb.QueryStatistics api

timeout

long

10

The single query timeout (in seconds)

queueCapacity

long

100

The capacity of the java.util.concurrent.BlockingQueue used to aggregate the results.

parameters

Map<String, Object>

Empty map

Optional parameter map to be used with the apoc.schema.runFile and apoc.schema.runFiles procedures.

Output parameters

Name Type

row

INTEGER?

result

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

Given this dataset:

CREATE (:ReturnQuery {id:1}), (:ReturnQuery {id:2}), (:ReturnQuery {id:3});
CREATE (n:Other {id: 4});

and this file, located in .$NEO4J_HOME/import:

match.cypher
MATCH (n:ReturnQuery) RETURN n;
MATCH (n:Other) RETURN n;

we can run the Cypher commands in match.cypher, by running the following query:

CALL apoc.cypher.runFileReadOnly("match.cypher");
Table 2. Results
row fileName

result

0

match.cypher

{"n":{"identity":1,"labels":["ReturnQuery"],"properties":{"id":1},"elementId":"…​"}}

1

match.cypher

{"n":{"identity":2,"labels":["ReturnQuery"],"properties":{"id":2},"elementId":"…​"}}

2

match.cypher

{"n":{"identity":3,"labels":["ReturnQuery"],"properties":{"id":3},"elementId":"…​"}}

0

match.cypher