前面的所有文件的读取基本上是对单文件执行的,在实际应用中,我们经常操作批量的文件。
Spring Batch框架提供了现有的组件MultiResourceItemReader支持对多文件的读取,通过MultiResourceItemReader读取批量文件非常简单。MultiResourceItemReader通过代理的ItemReader来读取问津。
MultiResourceItemReader关键属性:
MultiResourceItemReader属性 | 类型 | 说明 |
---|---|---|
delegate | ResourceAwareItemReaderItemStream | IteamReader的代理,将resources中定义的文件代理给当前指定的ItemReader进行处理 |
resources | Resource[] | 需要读取的资源文件列表 |
strick | boolean | 定义读取文件不存在时候的策略,如果为true则抛出异常,如果为false表示不抛出异常。 默认值为true |
saveState | boolean | 保存状态标识,读取资源时候是否保存当前读取的文件及当前文件是否读取条目记录的状态。 默认值为true |
BatchMain.java:
package com.xj.demo25;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author : xjfu
* @Date : 2021/10/26 20:01
* @Description : demo25 读多文件
*/
public class BatchMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("demo25/job/demo25-job.xml");
//Spring Batch的作业启动器,
JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
//在batch.xml中配置的一个作业
Job job = (Job)context.getBean("billJob");
try{
//开始执行这个作业,获得处理结果(要运行的job,job参数对象)
JobExecution result = launcher.run(job, new JobParameters());
System.out.println(result.toString());
}catch (Exception e){
e.printStackTrace();
}
}
}
CreditBill.java:
package com.xj.demo25;
/**
* @Author : xjfu
* @Date : 2021/10/26 19:27
* @Description :
*/
public class CreditBill {
//银行卡账户ID
private String accountID = "";
//持卡人姓名
private String name = "";
//消费金额
private double amount = 0;
//消费日期
private String date = "";
//消费场所
private String address = "";
public String getAccountID() {
return accountID;
}
public void setAccountID(String accountID) {
this.accountID = accountID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return this.accountID + "," + this.name + "," + this.amount + "," + this.date + "," + this.address;
}
}
CreditBillFieldSetMapper.java:
package com.xj.demo25;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
/**
* @Author : xjfu
* @Date : 2023/07/13 11:32
* @Description : 将FieldSet对象转为CreditBill对象
*/
public class CreditBillFieldSetMapper implements FieldSetMapper {
public CreditBill mapFieldSet(FieldSet fieldSet) throws BindException {
CreditBill result = new CreditBill();
result.setAccountID(fieldSet.readString("accountID"));
result.setName(fieldSet.readString("name"));
result.setAmount(fieldSet.readDouble("amount"));
result.setDate(fieldSet.readString("date"));
result.setAddress(fieldSet.readString("address"));
return result;
}
}
demo25-job.xml:
demo25-jobContext.xml:
credit-card-bill-201303.csv:
4047390012345678,tom,100.00,2013-2-2 12:00:08,Lu Jia Zui road
4047390012345678,tom,320.00,2013-2-3 10:35:21,Lu Jia Zui road
credit-card-bill-201304.csv:
4047390012345678,tom,674.70,2013-2-6 16:26:49,South Linyi road
4047390012345678,tom,793.20,2013-2-9 15:15:37,Longyang road
credit-card-bill-201305.csv:
4047390012345678,tom,360.00,2013-2-11 11:12:38,Longyang road
4047390012345678,tom,893.00,2013-2-28 20:34:19,Hunan road