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

后端 未结 4 457
眼角桃花
眼角桃花 2020-12-29 10:37

I want to configure spring-batch to read all csv files inside a specific folder sequentially.

The following does not work because the deleg

相关标签:
4条回答
  • 2020-12-29 10:40

    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;
    

    }

    0 讨论(0)
  • 2020-12-29 10:43

    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;
    }
    
    0 讨论(0)
  • 2020-12-29 10:49

    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;
          }
    
    0 讨论(0)
  • 2020-12-29 10:56

    I think you should use a PathMatchingResourcePatternResolver.

    0 讨论(0)
提交回复
热议问题