使用Spring Batch合并多個文件可以按照以下步驟進行:
@EnableBatchProcessing
注解開啟Batch Processing的支持。在配置類中定義一個StepBean,并設置其讀取器、處理器及寫入器。@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private ItemReader<File> fileReader;
@Autowired
private ItemProcessor<File, String> fileProcessor;
@Autowired
private ItemWriter<String> fileWriter;
@Bean
public Step mergeFilesStep() {
return stepBuilderFactory.get("mergeFilesStep")
.<File, String>chunk(10)
.reader(fileReader)
.processor(fileProcessor)
.writer(fileWriter)
.build();
}
@Bean
public Job mergeFilesJob() {
return jobBuilderFactory.get("mergeFilesJob")
.start(mergeFilesStep())
.build();
}
}
MultiResourceItemReader
來讀取多個文件,并將它們合并為一個列表。@Bean
public MultiResourceItemReader<File> fileReader() {
MultiResourceItemReader<File> reader = new MultiResourceItemReader<>();
reader.setResources(new Resource[] {
new FileSystemResource("file1.csv"),
new FileSystemResource("file2.csv"),
new FileSystemResource("file3.csv")
});
reader.setDelegate(new FlatFileItemReaderBuilder<File>()
.name("fileItemReader")
.delimited()
.names("column1", "column2", "column3")
.targetType(File.class)
.build());
return reader;
}
@Bean
public ItemProcessor<File, String> fileProcessor() {
return file -> {
// 處理文件邏輯,返回處理后的字符串
return file.getName();
};
}
FlatFileItemWriter
來將結果寫入一個文件。@Bean
public ItemWriter<String> fileWriter() {
FlatFileItemWriter<String> writer = new FlatFileItemWriter<>();
writer.setResource(new FileSystemResource("mergedFile.csv"));
writer.setLineAggregator(new PassThroughLineAggregator<>());
return writer;
}
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job mergeFilesJob;
public void runBatchJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
JobParameters jobParameters = new JobParametersBuilder()
.addLong("time", System.currentTimeMillis())
.toJobParameters();
jobLauncher.run(mergeFilesJob, jobParameters);
}
以上就是使用Spring Batch合并多個文件的基本步驟。根據實際需求可以進行適當調整和擴展。