数据处理——ItemProcessor接口

spring batch中需要实现ItemProcessor接口来进行批量处理读入的数据。下面以批量把大小字母转化为小写字母为例

首先批量读字母,然后进行处理,最后打印出来,整个job作业配置如下

import java.util.Arrays;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@EnableBatchProcessing
@Configuration
public class BatchConfig {

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Value("${spring.batch.chunk.size:3}")
    private int chunkSize; //5

    /*1、创建一个Job作业*/
    @Bean
    public Job itemProcessJob(){
        return jobBuilderFactory.get("ItemProcessJob")
        .start(chunkStep())
        .build();
    }

    //2、创建一个step*/
    @Bean
    public Step chunkStep(){
        return stepBuilderFactory.get("chunkStep")
                .chunk(chunkSize)   //表示输入是String类型,输出是String类型,不加会报错类型不匹配                           
                .reader(listReader())
                .processor(itemProcess())
                .writer(list -> list.forEach(System.out::println))
                .allowStartIfComplete(true)
                .build();
    }

    //读
    @Bean
    public ItemReader listReader(){
        return new ListItemReader<>(Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H", "I",
                "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"));
    }

    //处理
    @Bean
    public ItemProcessor itemProcess(){
        return new LowerCaseItemProcess();
    }

    /*处理程序中,输入的是String类型, 输出的是String类型*/
    private class LowerCaseItemProcess implements ItemProcessor{
        @Override
        /*item表示输入的值;output表示处理后返回的值,都是String类型的*/
        public String process(String item) throws Exception {
            String output = item.toLowerCase(); //把大写字母转化为小写字母
            return output;
        }

    }

}

运行该作业,输出内容如下:

……省略
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t

从数据库中观察执行情况如下
数据处理——ItemProcessor接口_第1张图片
最后一条就是执行信息,commit了5次,共读取20条信息,写入20条信息。由于chunkSize设置的是5,共20条数据,4次就读完20条数据了,但是spring batch不知道后面是否还有没有数据,不会终止,第5次读取下一条数据时,为空,然后再commit一次,终止。最后一次空也会commit一次,所以总共commit了5次。

你可能感兴趣的:(spring,batch)