Spring Boot 之 Spring Batch 批处理实践
2021-01-20 02:13
标签:tomcat 用户 except nis cti return exists 应用 处理 从 MariaDB 一张表内读 10 万条记录,经处理后写到 MongoDB 。 1、新建 Spring Boot 应用,依赖如下: 2、创建一张表,并生成 10 万条数据 3、创建 Person 类 4、创建一个中间处理器 5、创建 6、创建任务完成的监听 7、构建批处理任务 0出错,耗时 2 分钟左右,测试机 Mac 本文由博客一文多发平台 OpenWrite 发布! Spring Boot 之 Spring Batch 批处理实践 标签:tomcat 用户 except nis cti return exists 应用 处理 原文地址:https://www.cnblogs.com/Jafeney/p/12902941.html实践内容
具体实现
DROP TABLE people IF EXISTS;
CREATE TABLE people (
id BIGINT IDENTITY NOT NULL PRIMARY KEY,
first_name VARCHAR(20),
last_name VARCHAR(20)
);
@Data
public class Person {
private Long id;
private String lastName;
private String firstName;
}
PersonItemProcessor
import org.springframework.batch.item.ItemProcessor;
@Log4j2
public class PersonItemProcessor implements ItemProcessor
PersonMapper
,用户数据库映射public class PersonMapper implements RowMapper {
private static final String ID_COLUMN = "id";
private static final String NICKNAME_COLUMN = "first_name";
private static final String EMAIL_COLUMN = "last_name";
@Override
public Object mapRow(ResultSet resultSet, int i) throws SQLException {
Person user = new Person();
person.setId(resultSet.getLong(ID_COLUMN));
person.setNickname(resultSet.getString(NICKNAME_COLUMN));
person.setEmail(resultSet.getString(EMAIL_COLUMN));
return person;
}
}
JobCompletionNotificationListener
@Log4j2
@Component
public class JobCompletionNotificationListener extends JobExecutionListenerSupport {
@Override
public void afterJob(JobExecution jobExecution) {
if(jobExecution.getStatus() == BatchStatus.COMPLETED) {
log.info("!!! JOB FINISHED! Time to verify the results");
}
}
}
BatchConfiguration
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
public DataSource dataSource;
@Autowired
public MongoTemplate mongoTemplate;
@Bean
public JdbcCursorItemReader
任务处理结果
文章标题:Spring Boot 之 Spring Batch 批处理实践
文章链接:http://soscw.com/index.php/essay/44332.html