Search code examples
neo4jcypher

iterating on all files of a directory from neo4j


How can I access the filesystem from a Neo4j script? I need to call apoc.load.json on all the files contained in a directory ...

so the pseudo code should be

get list of files in  directory
UNWIND list of files AS filename
call apoc.json.load (filename) YIELD value
WITH value
blah blah
RETURN

Solution

  • There is now (at least since neo4j 4.1) an apoc.load.directory function that can return all the file names that conform to a pattern under a directory. In neo4j 5.x, it is found in the extended apoc plugin.

    For example, to get all the .json files directly under the configured import directory (read the docs to understand how directories are handled):

    CALL apoc.load.directory('*.json', '', {recursive: false}) YIELD value AS filepaths
    UNWIND filepaths AS filepath
    CALL apoc.json.load (filepath) YIELD value AS row
    // Process each row
    ...
    RETURN