简体   繁体   English

如何在Spring Batch中使用不同的文件名多次调用FlatFileItemReader?

[英]How do I call FlatFileItemReader multiple times with different file names in Spring Batch?

Goal: Have one file reader that can be called from 1 to N times, with different file names in Spring batch without have to close/reopen files every read. 目标:拥有一个可以被调用1到N次的文件阅读器,在Spring批处理中具有不同的文件名,而不必每次读取都关闭/重新打开文件。

What I have so far: 到目前为止,我有:

I roughed out the program using the following bean for reading the CSV files: 我使用以下bean来粗略读取该程序,以读取CSV文件:

@Bean
    public ItemReader<DataLoadRecordClass> dataLoadFileItemReader() {
        this.inputRecords = new DataLoadRecordListClass();
        FlatFileItemReader<DataLoadRecordClass> reader = new FlatFileItemReader<DataLoadRecordClass>();
        reader.setEncoding("UTF-8");
        reader.setLinesToSkip(1); //skip header line
        reader.setResource(new FileSystemResource(String.format(this.fileSystemBasePath,storeName) + this.fileBasePath + this.inFileName));
        LineMapper<DataLoadRecordClass> dataFileLineMapper = buildLineMapper();
        reader.setLineMapper(dataFileLineMapper);
        return reader;
    }

StoreName and inFileName will be specified at run time, and there will normally be at least 4 stores this will need to be run for, but the requirements are for it to run for 1 to N stores. StoreName和inFileName将在运行时指定,通常至少需要运行4个商店,但要求运行1到N个商店。

Obviously, as the code is written it will only work for 1 store and 1 file. 显然,在编写代码时,它仅适用于1个存储区和1个文件。 How can I scale this so that it can run for 1 to N stores with 1 to N files in each store, and in parallel? 我如何缩放它,使其可以在1到N个商店中运行,每个商店中有1到N个文件并可以并行运行? I do not want to create beans for each possible store and file obviously since the only difference in them will be the file name and path. 我不想为每个可能的存储和文件创建bean,因为它们之间的唯一区别是文件名和路径。

Thanks! 谢谢!

This allows me to pass in variables 这使我可以传递变量

@Bean
@StepScope
public FlatFileItemReader<DataLoadRecordClass> dataLoadFileItemReader(@Value("#{jobParameters['myParam']}" String storeNameEntry) {  
..etc..
}

The big catch was you can not use ItemReader for this, you have to return the FlatFileItemReader or you will get an error that it can not open the reader. 最大的麻烦是您不能为此使用ItemReader,必须返回FlatFileItemReader,否则将收到一个错误消息,指出它无法打开阅读器。 This only happened with StepScope. 这仅发生在StepScope中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Batch:如何使用FlatFileItemReader读取CSV文件的页脚和验证 - Spring Batch : How to read footer of CSV file and validation using FlatFileItemReader Spring Batch-用于不同分隔文件的FlatFileItemReader - Spring Batch - FlatFileItemReader for different delimited files 在Spring Batch中更新FlatFileItemReader之后的文件 - Update file after FlatFileItemReader in Spring Batch Spring 使用 FlatFileItemReader 批量空文件 xML - Spring Batch empty file using FlatFileItemReader xML 春季批处理中如何使用FlatFileItemReader处理以空格分隔的项目(单行中有多个记录)? - How to use FlatFileItemReader in spring batch for items separated by space (multiple records on single line)? Spring Batch-写入时将读取行FlatFileItemReader拆分为多行 - Spring Batch - Split a read line FlatFileItemReader into multiple lines while writing 如何使用不同的作业参数多次运行spring batch作业? - How to run spring batch job multiple times with different job parameters? 读取 CSV 中的换行符,这些换行符在 spring 批处理的 FlatfileItemReader 中的文件中引用 - Reading line breaks in CSV which are quoted in the file in FlatfileItemReader of spring batch Spring 批处理 FlatFileItemReader 令牌错误 - Spring batch FlatFileItemReader token error 如何在Spring批处理中使用FlatFileItemReader忽略CSV中不需要的列 - How to ignore unwanted columns in CSV using FlatFileItemReader in spring batch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM