数据处理——ItemProcessor接口

本文介绍如何使用 Spring Batch 的 ItemProcessor 接口将一批大写字母转换为小写字母。通过具体示例展示了 Job 和 Step 的配置过程,以及如何自定义 ItemProcessor 来处理数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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")
                .<String, String>chunk(chunkSize)   //<String, String>表示输入是String类型,输出是String类型,不加会报错类型不匹配                           
                .reader(listReader())
                .processor(itemProcess())
                .writer(list -> list.forEach(System.out::println))
                .allowStartIfComplete(true)
                .build();
    }

    //读
    @Bean
    public ItemReader<String> 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<String, String> itemProcess(){
        return new LowerCaseItemProcess();
    }

    /*处理程序中,输入的是String类型, 输出的是String类型*/
    private class LowerCaseItemProcess implements ItemProcessor<String, String>{
        @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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值