容错机制
异常处理
重试机制
多个Writer在一个step中进行
博客记不到了 直接上我的代码把
Writer_1
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
/**
* 将处理好的数据写入数据库
* 写入用户表
* @author tn
* @className UserDb2DbItemWriter
* @date 2021-06-23 10:21
*/
@Component
@Slf4j
public class UserDb2DbItemWriter {
@Bean
public ItemWriter userDb2DbWriter(@Qualifier("datasource") DataSource swuplDatasource) {
final String userSql = " sql ";
try {
JdbcBatchItemWriter<UserBean> build = new JdbcBatchItemWriterBuilder<UserBean>()
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
.sql(userSql)
.dataSource(swuplDatasource)
.build();
return build;
} catch (Exception e) {
log.error("插入错误", e);
}
return null;
}
}
Writer_2
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
/**
* 将处理好的数据写入数据库
*
* @author tn
* @className StudentDb2DbItemWriter
* @date 2021-06-23 10:21
*/
@Component
@Slf4j
public class StudentDb2DbItemWriter {
@Bean
public ItemWriter studentDb2DbWriter(@Qualifier("datasource") DataSource swuplDatasource) {
final String studentSql = "sql";
try {
JdbcBatchItemWriter<ClassesInstructorBean> build = new JdbcBatchItemWriterBuilder<ClassesInstructorBean>()
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
.sql(studentSql)
.dataSource(swuplDatasource)
.build();
return build;
} catch (Exception e) {
log.error("插入错误", e);
}
return null;
}
}
组合两个 Writer
/**
* 同时写两个表
* @param studentWriter
* @param userWriter
* @return
* @throws Exception
*/
@Bean
public CompositeItemWriter s2uCompositeWriter(
@Qualifier("studentDb2DbWriter")
ItemWriter studentWriter ,
@Qualifier("userDb2DbWriter")
ItemWriter userWriter) throws Exception {
CompositeItemWriter itemWriter = new CompositeItemWriter<>();
// 指定多个输出对象
itemWriter.setDelegates(Arrays.asList(studentWriter,userWriter));
itemWriter.afterPropertiesSet(); return itemWriter;
}
step中使用
@Bean
public Step studentDb2DbStep(@Qualifier("studentDb2DbReader")
ItemReader reader,
ItemProcessor studentDb2DbProcessor,
@Qualifier("s2uCompositeWriter")
ItemWriter writer) {
String funcName = Thread.currentThread().getStackTrace()[1].getMethodName();
return stepBuilderFactory.get(funcName)
.<Bean1,Bean2>chunk(10)
.reader(reader)
.processor(studentDb2DbProcessor)
.writer(writer)
.faultTolerant()
.skipPolicy(new ExceptionSkipPolicy())
.build();
}
不同方式的读取数据
FlatFileItemReader
从CVS文件中读取数据
StaxEventItemReader
从XML文件中读取数据
MultiResourceItemReader
从多个文件读取数据
JdbcCursorItemReader
使用游标读取数据集
HibernateCursorItemReader
通过Hibernate框架进行游标的控制
StoredProcedureItemReader
和JdbcCursorItemReader一致,但是底层逻辑是先执行存储过程,然后返回存储过程执行结果游标。不同的数据库存储过程游标返回会有一些差异
JdbcPagingItemReader
分页查询