Spring Batch批處理框架使用解析
這篇文章主要介紹了Spring Batch批處理框架使用解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
使用Spring Batch做為批處理框架,可以完成常規(guī)的數(shù)據(jù)量不是特別大的離線計算。
現(xiàn)在寫一個簡單的入門版示例。
這里默認大家已經(jīng)掌握了Spring Batch的基本知識,示例只是為了快速上手實踐
目標1:程序隨機生成字符串,經(jīng)過Spring Batch后,統(tǒng)一在字符串后加入“----PROCESSED”,并輸出
目標2:程序讀取txt文件,經(jīng)過Spring Batch后,統(tǒng)一加入如上字段,并輸出
Spring Batch的流程
- 讀取數(shù)據(jù)----itemReader
- 處理數(shù)據(jù)----itemProcess
- 數(shù)據(jù)寫入----itemWrite
分析目標可知,兩個目標的輸入數(shù)據(jù)源不同,處理方式基本一致,數(shù)據(jù)完成后的寫入規(guī)則一致
由此可以分段完成代碼
itemReader
目標一
這里沒有使用Spring Batch自帶的集中reader,所以自定義了隨機生成字符串的reader
這里代碼并不完善,reader會無線循環(huán)生成隨機字符串,但不影響本次學習的目的
public class MyItemReader implements ItemReader<String> {
@Override
public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
return RandomStringUtils.randomAlphabetic(10);
}
}
目標二
由于是讀取文件中的內(nèi)容,所以不用自定義reader實現(xiàn),可直接使用FlatFileItemReader,在Batch的config中配置即可
@Bean
public ItemReader<String> textReader(){
FlatFileItemReader<String> reader=new FlatFileItemReader<>();
File file = new File("D:\\FTP\\ttest.txt");
reader.setResource(new FileSystemResource(file));
reader.setLineMapper(new LineMapper<String>() {
@Override
public String mapLine(String line, int lineNumber) throws Exception {
return line;
}
});
return reader;
}
itemProcess
這里采用同一種處理方式即可
public class MyItemProcessor implements ItemProcessor<String,String> {
@Override
public String process(String s) throws Exception {
return s+"---------PROCESSED";
}
}
itemWriter
也采用同一種即可
public class MyItemWriter implements ItemWriter<String> {
@Override
public void write(List<? extends String> items) throws Exception {
for (String item : items) {
System.out.println(item);
}
}
}
配置完成Batch Config
@Configuration
@EnableBatchProcessing
public class BatchConfiguration extends DefaultBatchConfigurer {
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Bean
public MyItemProcessor processor(){
return new MyItemProcessor();
}
@Bean
public ItemWriter<String> writer(){
return new MyItemWriter();
}
@Bean
public ItemReader<String> textReader(){
FlatFileItemReader<String> reader=new FlatFileItemReader<>();
File file = new File("D:\\FTP\\ttest.txt");
reader.setResource(new FileSystemResource(file));
reader.setLineMapper(new LineMapper<String>() {
@Override
public String mapLine(String line, int lineNumber) throws Exception {
return line;
}
});
return reader;
}
@Bean
public ItemReader<String> stringReader(){
return new MyItemReader();
}
@Override
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
}
@Bean
public Step myStep(){
return stepBuilderFactory
.get("step1")
//這個chunk size是最后調(diào)用寫入的時候,一次性寫入多少條已處理的數(shù)據(jù)
.<String,String>chunk(10)
// .reader(textReader())
.reader(stringReader())
.processor(processor())
.writer(writer())
.build();
}
@Bean
public Job MyJob(){
return jobBuilderFactory
.get("MyJOB")
.listener(new JobExecutionListenerSupport(){
//所有處理結束后調(diào)用
@Override
public void afterJob(JobExecution jobExecution) {
if(jobExecution.getStatus() == BatchStatus.COMPLETED){
System.out.println("OK");
}
}
})
.flow(myStep())
.end()
.build();
}
}
結束
最后直接運行spring boot程序即可
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java中HashMap與String字符串互轉(zhuǎn)的問題解決
本文介紹了Java中HashMap與String字符串互轉(zhuǎn)的問題解決,當我們有需求將HashMap轉(zhuǎn)為Json格式的String時,需要使用FastJson/Gson將HashMap轉(zhuǎn)為String,感興趣的可以了解一下2022-03-03
SpringBoot單元測試使用@Test沒有run方法的解決方案
這篇文章主要介紹了SpringBoot單元測試使用@Test沒有run方法的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

