Oren
Oren

Reputation: 437

Spring Batch how to read all json files from a folder and all its sub-folders using PathMatchingResourcePatternResolver

I'm using Spring Batch MultiResourceItemReader in order to read multipule files. these files are located at a parent directory and it's sub-directories.

Already tried:

  1. Read the files by my own customized code and create the Resource array manually.

  2. Use PathMatchingResourcePatternResolver as can be seen in the code example ( inspired by this Finding Resources with PathMatchingResourcePatternResolver and URLClassloader in JARs

    @Bean
    public MultiResourceItemReader<List<SingleJsonRowInput>> 
    multiResourceItemReader() {
    PathMatchingResourcePatternResolver  patternResolver = new 
    PathMatchingResourcePatternResolver();
    Resource resources[] = null;;
    try {
        resources = 
        patternResolver.getResources("file:C:\\inputFolder\\**\\*.json");
    } catch (IOException e) {
        e.printStackTrace();
    }
    MultiResourceItemReader<List<SingleJsonRowInput>> 
    multiResourceItemReader = new MultiResourceItemReader<>();
    multiResourceItemReader.setResources(resources);
    multiResourceItemReader.setDelegate(new 
    ItemReaderForMulti(fileManager));
    return multiResourceItemReader;
    }
    

Upvotes: 1

Views: 2918

Answers (2)

Oren
Oren

Reputation: 437

Instead of using windows backslashes - the solution is to use Unix\Linux like syntax:

Didn't work: resources = patternResolver.getResources("file:C:\\inputFolder\\**\\*.json");

Works well: resources = patternResolver.getResources("file:C:/inputFolder/**/*.json");

Upvotes: 3

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31640

You can use the following snippet:

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("file:/root/folder/**/*.json");

The **/* will return the files recursively from the root/folder. Then you pass the resources array to the MultiResourceItemReader.

Upvotes: 4

Related Questions