I want to configure spring-batch
to read all csv
files inside a specific folder sequentially.
The following does not work because the deleg
Use PathMatchingResourcePatternResolver like this.
@Bean
public ItemReader<String> reader() {
Resource[] resources = null;
ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
try {
resources = patternResolver.getResources("/myfolder/*.csv");
} catch (IOException e) {
e.printStackTrace();
}
MultiResourceItemReader<String> reader = new MultiResourceItemReader<>();
reader.setResources(resources);
reader.setDelegate(new FlatFileItemReader<>(..));
return reader;
}
Alternatively @Value annotation can be used to let ResourceArrayPropertyEditor convert string to Resource[].
@Bean
@StepScope
public ItemReader<String> reader(
@Value("file:/myfolder/*.csv") Resource[] resources
) {
MultiResourceItemReader<String> reader = new MultiResourceItemReader<>();
reader.setResources(resources);
reader.setDelegate(new FlatFileItemReader<>(..));
return reader;
}
Read multiple files from Absolute file path:
@Bean
public MultiResourceItemReader<POJO> multiResourceItemReader() {
MultiResourceItemReader<POJO> resourceItemReader = new MultiResourceItemReader<POJO>();
ClassLoader cl = this.getClass().getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
Resource[] resources = resolver.getResources("file:" + filePath );
resourceItemReader.setResources(resources);
resourceItemReader.setDelegate(reader());
return resourceItemReader;
}
@Bean
public FlatFileItemReader<POJO> reader() {
FlatFileItemReader<POJO> reader = new FlatFileItemReader<POJO>();
reader.setLineMapper(new DefaultLineMapper() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[]{"id1", "POJO"});
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<POJO>() {{
setTargetType(POJO.class);
}});
}});
return reader;
}
I think you should use a PathMatchingResourcePatternResolver.