一、读多文件
前面的所有文件的读取基本上是对单文件执行的,在实际应用中,我们经常操作批量的文件。
Spring Batch框架提供了现有的组件MultiResourceItemReader支持对多文件的读取,通过MultiResourceItemReader读取批量文件非常简单。MultiResourceItemReader通过代理的ItemReader来读取问津。
MultiResourceItemReader关键属性:
MultiResourceItemReader属性 | 类型 | 说明 |
---|---|---|
delegate | ResourceAwareItemReaderItemStream | IteamReader的代理,将resources中定义的文件代理给当前指定的ItemReader进行处理 |
resources | Resource[] | 需要读取的资源文件列表 |
strick | boolean | 定义读取文件不存在时候的策略,如果为true则抛出异常,如果为false表示不抛出异常。 默认值为true |
saveState | boolean | 保存状态标识,读取资源时候是否保存当前读取的文件及当前文件是否读取条目记录的状态。 默认值为true |
二、项目实例
1.项目框架
2.代码实现
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 {//银行卡账户IDprivate 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;}@Overridepublic 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<CreditBill> {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:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd"><!--导入文件--><import resource="classpath:demo25/job/demo25-jobContext.xml"/><!--定义名字为billJob的作业--><batch:job id="billJob"><!--定义名字为billStep的作业步--><batch:step id="billStep"><batch:tasklet transaction-manager="transactionManager"><!--定义读、处理、写操作,规定每处理两条数据,进行一次写入操作,这样可以提高写的效率--><batch:chunk reader="multiResourceReader" writer="csvItemWriter" commit-interval="2"></batch:chunk></batch:tasklet></batch:step></batch:job>
</beans>
demo25-jobContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><!--定义作业仓库 Job执行期间的元数据存储在内存中--><bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/><!--定义作业调度器,用来启动job--><bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"><!--注入jobRepository--><property name="jobRepository" ref="jobRepository"/></bean><!--定义事务管理器,用于Spring Batch框架中对数据操作提供事务能力--><bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/><!--读多文件使用multiResourceItemReader--><bean id="multiResourceReader" class="org.springframework.batch.item.file.MultiResourceItemReader"><!--需要读取的文件集合--><property name="resources" value="classpath:demo25/data/credit-card-bill-*.csv"/><!--配置具体的文件读取ItemReader--><property name="delegate" ref="flatFileItemReader"/></bean><bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step"><!--将一行文件记录转换为Java对象--><property name="lineMapper" ref="lineMapper"/><!--定义严格的文件存在坚持策略,当resource中定义的文件不存在时会导致Job失败--><property name="strict" value="true"/></bean><bean id="lineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper"><!--使用delimitedLineTokenizer将行记录转换为FieldSet对象--><property name="lineTokenizer" ref="delimitedLineTokenizer"/><!--将FieldSet对象转为CreditBill对象--><property name="fieldSetMapper" ref="creditBillFieldSetMapper"/></bean><!--根据给定的分隔符,将一条记录转换为FieldSet对象--><bean id="delimitedLineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"><!--指定分隔符“,”--><property name="delimiter" value=","/><property name="names" value="accountID,name,amount,date,address"/></bean><bean id="creditBillFieldSetMapper" class="com.xj.demo25.CreditBillFieldSetMapper"/><!--写入类--><bean id="csvItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step"><property name="resource" value="file:src/main/resources/demo25/data/demo25-outputFile.csv"/><property name="lineAggregator"><bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator"><property name="delimiter" value=","/><property name="fieldExtractor"><bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor"><property name="names" value="accountID,name,amount,date,address"/></bean></property></bean></property></bean><!--注入实体类--><bean id="creditBill" class="com.xj.demo25.CreditBill" scope="prototype"/></beans>
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
3.运行结果