溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBatch批處理框架怎么用

發(fā)布時(shí)間:2021-09-23 14:45:29 來源:億速云 閱讀:157 作者:小新 欄目:編程語言

這篇文章主要介紹SpringBatch批處理框架怎么用,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

使用Spring Batch做為批處理框架,可以完成常規(guī)的數(shù)據(jù)量不是特別大的離線計(jì)算。

現(xiàn)在寫一個(gè)簡(jiǎn)單的入門版示例。

這里默認(rèn)大家已經(jīng)掌握了Spring Batch的基本知識(shí),示例只是為了快速上手實(shí)踐

目標(biāo)1:程序隨機(jī)生成字符串,經(jīng)過Spring Batch后,統(tǒng)一在字符串后加入“----PROCESSED”,并輸出

目標(biāo)2:程序讀取txt文件,經(jīng)過Spring Batch后,統(tǒng)一加入如上字段,并輸出

Spring Batch的流程

讀取數(shù)據(jù)----itemReader  處理數(shù)據(jù)----itemProcess  數(shù)據(jù)寫入----itemWrite

分析目標(biāo)可知,兩個(gè)目標(biāo)的輸入數(shù)據(jù)源不同,處理方式基本一致,數(shù)據(jù)完成后的寫入規(guī)則一致

由此可以分段完成代碼

itemReader

目標(biāo)一

這里沒有使用Spring Batch自帶的集中reader,所以自定義了隨機(jī)生成字符串的reader

這里代碼并不完善,reader會(huì)無線循環(huán)生成隨機(jī)字符串,但不影響本次學(xué)習(xí)的目的

public class MyItemReader implements ItemReader<String> {  @Override  public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {    return RandomStringUtils.randomAlphabetic(10);  }}

目標(biāo)二

由于是讀取文件中的內(nèi)容,所以不用自定義reader實(shí)現(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@EnableBatchProcessingpublic 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")        //這個(gè)chunk size是最后調(diào)用寫入的時(shí)候,一次性寫入多少條已處理的數(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(){          //所有處理結(jié)束后調(diào)用          @Override          public void afterJob(JobExecution jobExecution) {            if(jobExecution.getStatus() == BatchStatus.COMPLETED){              System.out.println("OK");            }          }        })        .flow(myStep())        .end()        .build();  }}

以上是“SpringBatch批處理框架怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI