我目前正在学习春季批处理,我受到了文件归档任务的挑战。基本上我需要读取单独的CSV文件并将它们放在一个新的存档文件夹中,原始文件名附加当前日期。我想知道的是我如何从multiResourceItemReader获取原始文件名,在FlatfileItemWriter中使用它作为文件名+日期,然后删除原始文件。
这是我目前的代码:
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Value("classpath*:/data/CSV_FOR_ARCHIVING*.csv")
private Resource inputFiles[];
@Bean
public MultiResourceItemReader<Person> multiResourceItemReader() {
MultiResourceItemReader<Person> itemReader = new MultiResourceItemReader<>();
itemReader.setDelegate(personItemReader());
itemReader.setResources(inputFiles);
return itemReader;
}
@Bean
public FlatFileItemReader<Person> personItemReader() {
FlatFileItemReader<Person> itemReader = new FlatFileItemReader<>();
itemReader.setLinesToSkip(1);
DefaultLineMapper<Person> personLineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
tokenizer.setNames(new String[] { "id", "firstName", "lastName", "email", "gender" });
personLineMapper.setLineTokenizer(tokenizer);
personLineMapper.setFieldSetMapper(new PersonFieldSetMapper());
personLineMapper.afterPropertiesSet();
itemReader.setLineMapper(personLineMapper);
return itemReader;
}
@Bean
public FlatFileItemWriter<Person> jsonWriter() throws Exception {
FlatFileItemWriter<Person> personWriter = new FlatFileItemWriter<>();
personWriter.setLineAggregator(new PersonLineAggregator());
File outputPath = File.createTempFile("PersonOutput", ".csv", new File("C:/Users/default/Documents"));
System.out.println("Output Path: " + outputPath.getAbsolutePath());
personWriter.setResource(new FileSystemResource(outputPath));
personWriter.afterPropertiesSet();
return personWriter;
}
@Bean
public Step step1() throws Exception {
return stepBuilderFactory.get("step1").<Person, Person>chunk(10)
.listener(new ItemReaderListener())
.reader(multiResourceItemReader())
.writer(jsonWriter())
.build();
}
@Bean
public Job job() throws Exception {
return jobBuilderFactory.get("job")
.start(step1())
.build();
}
CSV files to be readed and archived separately
编辑1: 感谢@Niraj的回答,我已经实现了ResourceAware,我已经可以获得文件名了。现在我的新问题是,每当资源发生变化时,我如何告诉我的FlatFileItemWriter创建和写入新文件?
答案 0 :(得分:2)
使用ResourceAware接口获取当前文件名。和ResourceSuffixCreator接口根据需要创建后缀
http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/ResourceAware.html http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/file/ResourceSuffixCreator.html
答案 1 :(得分:0)
您可以从reader中将值放在executionContext中,然后在您的编写器中读取。像这样的东西
@Value("#{jobExecution.executionContext}")
private ExecutionContext executionContext;
public void somePlaceInYourReader(){
this.executionContext.putInt("fileName", fileName);
}
使用executionContext.getInt("fileName")
以相同的方式阅读。您可能还必须使用此@JobScope