史上最轻松入门之Spring Batch - 轻量级批处理框…
2019-10-29 16:09:43来源:博客园 阅读 ()
史上最轻松入门之Spring Batch - 轻量级批处理框架实践
从 MariaDB 一张表内读 10 万条记录,经处理后写到 MongoDB 。
Batch 任务模型
具体实现
1、新建 Spring Boot 应用,依赖如下:
<!-- Web 应用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Web 容器 undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!-- 日志 Log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- MongoDB -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<!-- Brantch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<!-- Mariadb 驱动 -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.0.2</version>
</dependency>
<!-- Lombok 代码简化 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
</dependency>
2、创建一张表,并生成 10 万条数据
DROP TABLE people IF EXISTS;
CREATE TABLE people (
id BIGINT IDENTITY NOT NULL PRIMARY KEY,
first_name VARCHAR(20),
last_name VARCHAR(20)
);
3、创建 Person 类
@Data
public class Person {
private Long id;
private String lastName;
private String firstName;
}
4、创建一个中间处理器 PersonItemProcessor
import org.springframework.batch.item.ItemProcessor;
@Log4j2
public class PersonItemProcessor implements ItemProcessor<Person, Person> {
@Override
public Person process(final Person person) throws Exception {
final String firstName = person.getFirstName().toUpperCase();
final String lastName = person.getLastName().toUpperCase();
final Person transformedPerson = new Person(firstName, lastName);
log.info("Converting (" + person + ") into (" + transformedPerson + ")");
return transformedPerson;
}
}
5、创建 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;
}
}
6、创建任务完成的监听 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");
}
}
}
7、构建批处理任务 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<Person> reader(){
JdbcCursorItemReader<Person> itemReader = new JdbcCursorItemReader<Person>();
itemReader.setDataSource(dataSource);
itemReader.setSql("select id, nickname, email from people");
itemReader.setRowMapper(new PersonMapper());
return itemReader;
}
@Bean
public PersonItemProcessor processor() {
return new PersonItemProcessor();
}
@Bean
MongoItemWriter<Person> writer(){
MongoItemWriter<Person> itemWriter = new MongoItemWriter<Person>();
itemWriter.setTemplate(mongoTemplate);
itemWriter.setCollection("branch");
return itemWriter;
}
@Bean
public Step step() {
return stepBuilderFactory.get("step")
.<Person, Person> chunk(10)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
}
@Bean
public Job importUserJob(JobCompletionNotificationListener listener) {
return jobBuilderFactory.get("importUserJob")
.incrementer(new RunIdIncrementer())
.listener(listener)
.flow(step())
.end()
.build();
}
}
任务处理结果
0出错,耗时 2 分钟左右,测试机 Mac
? 著作权归作者所有,转载或内容合作请联系作者
● Spring Cloud Gateway - 快速开始
● APM工具寻找了一圈,发现SkyWalking才是我的真爱
● Spring Boot 注入外部配置到应用内部的静态变量
● 将 HTML 转化为 PDF新姿势
● Java 使用 UnixSocket 调用 Docker API
● Fastjson致命缺陷
● Service Mesh - gRPC 本地联调远程服务
● 使用 Thymeleaf 动态渲染 HTML
● Fastjson致命缺陷
● Spring Boot 2 集成log4j2日志框架
● Java面试通关要点汇总集之核心篇参考答案
● Java面试通关要点汇总集之框架篇参考答案
● Spring Security 实战干货:如何保护用户密码
● Spring Boot RabbitMQ - 优先级队列
原文链接: https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=2247486167&idx=1&sn=5983bd5437d14fffe48d42134da46c69&chksm=fb3f132ccc489a3aa813fb07ee64206bf3a51f57aef9b58ab7a8ab18c3d608619ed807a5828e&token=280305379&lang=zh_CN#rd
本文由博客一文多发平台 OpenWrite 发布!
原文链接:https://www.cnblogs.com/springforall/p/11762341.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- logstash系列-入门整理 2020-06-10
- Java 入门教程 2020-06-09
- RocketMQ4.4 入门进阶+实战 2020-06-08
- 因为 MongoDB 没入门,我丢了一份实习工作 2020-06-07
- Java 从入门到精通-反射机制 2020-06-03
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash