Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the SpringBatch batch Framework

Shulou Source: shulou.com Published: 2022-06-03 11:00:01 11月15日 Update

This article mainly introduces how to use SpringBatch batch framework, the article is very detailed, has a certain reference value, interested friends must read it!

Using Spring Batch as a batch processing framework, it is possible to perform off-line calculations with regular data volumes that are not particularly large.

Now write a simple starter example.

By default, you have mastered the basic knowledge of Spring Batch. The example is just for quick practice.

Goal 1: The program randomly generates a string. After Spring Batch, add "---PROCESSED" after the string and output it.

Goal 2: The program reads the txt file, and after Spring Batch, uniformly adds the above fields and outputs them.

Spring Batch Process

Read Data---itemReader Process Data---itemProcess Data Write---itemWrite

Analyzing the targets shows that the input data sources of the two targets are different, the processing methods are basically the same, and the writing rules after data completion are the same.

Thus, the code can be completed in stages

itemReader

goal one

Spring Batch's centralized reader is not used here, so the reader for randomly generated strings is customized.

Here the code is not perfect, the reader will generate random strings wirelessly, but it does not affect the purpose of this study.

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

goal two

Because it is to read the contents of the file, so do not use custom reader implementation, you can directly use FlatFileItemReader, in the batch config configuration can be

@Bean public ItemReader textReader(){ FlatFileItemReader reader=new FlatFileItemReader(); File file = new File("D:\\FTP\\ttest.txt"); reader.setResource(new FileSystemResource(file)); reader.setLineMapper(new LineMapper() { @Override public String mapLine(String line, int lineNumber) throws Exception { return line; } }); return reader; }

itemProcess

The same treatment can be used here

public class MyItemProcessor implements ItemProcessor { @Override public String process(String s) throws Exception { return s+"---------PROCESSED"; }}

itemWriter

The same can be used.

public class MyItemWriter implements ItemWriter { @Override public void write(List

Tags: Data targets processing characters strings frames content generation consistency code files methods knowledge examples programs articles unification output configuration differences Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Technology vpn Shulou Information macOS