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

Oren

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;
    }
    
Mahmoud Ben Hassine

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Delete all files from a folder and its sub folders

Batch. Mass renaming files in the folder and all sub-folders

How to delete all files inside public_ftp folder—and its sub folders—which are older than X days using cron job?

How to read all files in a folder with spring-batch and MultiResourceItemReader?

How to give read write permission to a folder and its sub folders and files?

Copy all files and folders from one folder Batch

Set namespace on all files in folder and sub folders

list all files in the folder and also sub folders

List all files in a folder and sub folders into a table

How to read all files in a folder using C

How to read all files in a folder from Java?

How to read all files from a folder randomly

How can we read all json files from all sub directory?

read all files in sub folder with pandas

How to read files from the sub folder of resource folder with using toURI

Disable backup of documents folder and all its sub folders on icloud?

How to read all .txt files in a folder and append its contents into one .txt file, using Python?

How to iterate through all sub-folders and their sub-sub-folders and so on using vba to check a folder if it exist?

Python read JSON files from all sub-directories

Xampp htdocs folder, sub folders and all files permission

Command to list all files in a folder as well as sub-folders in windows

Move all files inside sub folders to parent folder

Renaming all the files in a folder is renaming the sub folders too

How to list all the folders and its files in php

How to continuously read files from a folder with spring-batch?

How to delete all files in particular folder present in current directory sub-folders excluding a certain file?

How to lock cells for all files in a folder which has sub-folders

Copy all files and folders inside specific folder in batch file

ExifTool - How to remove all metadata from all files possible inside a folder and all its subfolders?