Spring batch sample 之 text to DB
例子很简单:
txt文件如下:
sampleSource.txt
kinaei,30
zoubin,40
ZHUTOU,65
wufeiran,51
需要存放到一张表中:
CREATE MEMORY TABLE BATCH_TEST(NAME VARCHAR(20) NOT NULL PRIMARY KEY,AGE BIGINT)
利用sts自带的spring-batch的模板生成样板代码。自己完成的无非是reader 和 writer两块。
下面是主要的配置代码:
<?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/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<description>Example job to get you started. It provides a
skeleton for a typical batch application.</description>
<job id="job1" xmlns="http://www.springframework.org/schema/batch">
<step id="step1" parent="simpleStep">
<tasklet>
<chunk reader="reader" writer="writer"/>
</tasklet>
</step>
</job>
<!--
<bean id="reader" class="org.springframework.sample.batch.example.ExampleItemReader">
<property name="sampleFile" value="classpath:sampleSource.txt" />
</bean>
-->
<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" value="classpath:sampleSource.txt" />
<property name="lineMapper" >
<bean id="defaultLineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names">
<list>
<value>name</value>
<value>age</value>
</list>
</property>
</bean>
</property>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="testbean" />
</bean>
</property>
</bean>
</property>
</bean>
<bean id="testbean" class="org.springframework.sample.batch.example.TestBean" scope="prototype"/>
<bean id="writer" class="org.springframework.sample.batch.example.ExampleItemWriter" >
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="simpleStep"
class="org.springframework.batch.core.step.item.SimpleStepFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager" />
<property name="jobRepository" ref="jobRepository" />
<property name="startLimit" value="100" />
<property name="commitInterval" value="1" />
</bean>
</beans>
主要的工作就是把需要读取的文件是一个逗号分割的文件,先把每一行转换为一个bean然后交给writer去写入数据库。
首先来看一下reader,Spring提供的基础设施及其完善,几乎不用写任何代码就能把一个reader配置完成了。一个reader主要由两部分组成:
【1】resource 告诉reader从哪里去读文件
<property name="resource" value="classpath:sampleSource.txt" />
【2】lineMapper 就是说txt文件中的每一行映射成怎样的一个bean。
如何把文件的一行映射成为一个bean呢?
【A】需要知道分割符号式什么,在spring 中就叫做:lineTokenizer。可能的话还要提供每个字段对应的名称。
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names">
<list>
<value>name</value>
<value>age</value>
</list>
</property>
</bean>
</property>
这里告诉系统用逗号分隔符,并且第一字段叫name,第二个字段叫age
【B】需要把txt文件中的字段映射到bean中的对应字段中并且做好字段的类型转换工作。Spring中就叫做fieldSetMapper
<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="testbean" />
</bean>
</property>
这里用到了spring的基础设施,即BeanWrapperFieldSetMapper它会根据txt文件读出的字段与给定的bean进行同名装配,自动的映射成了一个bean叫做testbean。
【C】 testbean 是什么的 定义如下
<bean id="testbean" class="org.springframework.sample.batch.example.TestBean" scope="prototype"/>
package org.springframework.sample.batch.example;
import java.io.Serializable;
public class TestBean implements Serializable{
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
这样reader就写好了。
剩下的就是writer了,由于spring没有提供方面的Database ItemWriters只能自己写了。
package org.springframework.sample.batch.example;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.StringUtils;
/**
* Dummy {@link ItemWriter} which only logs data it receives.
*/
public class ExampleItemWriter implements ItemWriter<Object> {
private static final Log log = LogFactory.getLog(ExampleItemWriter.class);
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* @see ItemWriter#write(Object)
*/
public void write(List<? extends Object> data) throws Exception {
TestBean rs=null;
for(Object line : data){
rs=(TestBean)line; //actual just one
}
final Object[] params=new Object[2];
params[0]=rs.getName();
params[1]=rs.getAge();
System.out.println(ToStringBuilder.reflectionToString(rs));
TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource));
transactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus arg0) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update("insert into BATCH_TEST(NAME,AGE) VALUES(?,?)",params);
return null;
}
});
}
}
配置如下:
<bean id="writer" class="org.springframework.sample.batch.example.ExampleItemWriter" >
<property name="dataSource" ref="dataSource" />
</bean>
最后谈一下总体的感觉:
【1】代码简单清晰许多,只需要把精力放在reader和writer的代码中。其他的如出错处理等都有spring代劳了。
【2】自动装配的这个reader用起来非常方便。
【3】自动实现了有状态的读取。为了防止读写过程中突然出错,在启动的时候重复读,或者漏读。以前读文件的时候要么是先把整个文件读写完毕后再commit,既浪费应用服务器的内存,又影响db的性能。要么要再写个文件记录读取的条数,重启后先判断读取的位置,在接着做。现在SPRING的这个reader自动提供了(当然代价是建立了记录状态的spring_batch的一系列的表),非常方便。
【4】在写writer的时候有spring来管理connection的事务提交。原来自己写的时候需要把connection传来传去以保证事务的完整性。比如如果要实现读两行再一次提交的话,可能connect 会贯穿于reader和writer或者再写个类去管理这个connection这里都提交给了spring。
实践下来我的结论是如果你要写一个读取文件,再做相应的处理后写入数据库的程序的话利用spring-batch绝对物有所值。
分享到:
相关推荐
Finally this book demonstrates how you can use areas of the Spring portfolio beyond just Spring Batch 4 to collaboratively develop mission-critical batch processes. You’ll see how a new class of use...
Spring Batch是Spring的一个子项目,使用Java语言并基于Spring框架为基础开发,使得已经使用 Spring 框架的开发者或者企业更容易访问和利用企业服务。 Spring Batch 提供了大量可重用的组件,包括了日志、追踪、事务、...
Spring Batch API(Spring Batch 开发文档).CHM。 官网 Spring Batch API,Spring Batch 开发文档
Spring Boot整合Spring Batch的一个小例子,在网上发现这方面的资源比较少,特此将其上传供大家学习。
spring-batch4.0.0 batch spring-batch集成 spring-batch.jar
Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统。Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问和利用企业级服务。Spring Batch可以提供...
spring batch批处理框架和对应的源码资源 rar 可以直接运行的
Spring Batch in Action is a comprehensive, in-depth guide to writing batch applications using Spring Batch. Written for developers who have basic knowledge of Java and the Spring lightweight ...
SpringBatch Tasklet sample
基于Spring Batch的大数据量并行处理 基于Spring Batch的大数据量并行处理
最近在研究springBoot+springbatch ,按照官网的实例做了一个实例。 最近在研究springBoot+springbatch ,按照官网的实例做了一个实例。
1.本项目运行在tomcat容器中,主要功能为从spring_batch_left库的user_from表抓取数据,之后批量插入到spring_batch_right库的user_to表 2.应用quartz对job进行定时触发(目前设置的定时为每隔一分钟执行一次,目前...
主要给大家介绍了Spring Batch读取txt文件并写入数据库的方法,SpringBatch 是一个轻量级、全面的批处理框架。这里我们用它来实现文件的读取并将读取的结果作处理,处理之后再写入数据库中的功能。需要的朋友可以...
难得的详细spring batch资料 难得的详细spring batch资料
SpringBatch数据库建表语句,存储springBatch批处理过程中需要保存的数据和步骤信息
Spring Batch批处理框架Spring Batch批处理框架Spring Batch批处理框架
四,Spring Batch之Step执行过程介绍 6 五,Spring Batch应用 7 1,简单应用 7 构建应用 7 对象定义 7 读写及处理接口 8 任务定义 10 任务执行 11 任务重试 13 运行时管理 14 2,高级应用 16 ...
资源名称:Spring Batch 批处理框架内容简介:《Spring Batch 批处理框架》全面、系统地介绍了批处理框架Spring Batch,通过详尽的实战示例向读者展示了Spring Batch框架对大数据批处理的基本开发能力,并对框架的...
springbatch mybatis