Spring Batch批量處理數(shù)據(jù)實(shí)現(xiàn)方式
Spring Batch批量處理數(shù)據(jù)
Spring Batch 是一個由 Pivotal Software(原 SpringSource,現(xiàn)屬于 VMware)開發(fā)的批處理框架,它是 Spring 框架的一部分,主要用于創(chuàng)建高效、健壯的批量數(shù)據(jù)處理應(yīng)用。
Spring Batch 設(shè)計用于處理大量的記錄,例如在夜間處理或定期運(yùn)行的數(shù)據(jù)加載、轉(zhuǎn)換和整合操作。
Spring Batch的主要特性包括:
- 事務(wù)管理:支持事務(wù)邊界內(nèi)的數(shù)據(jù)處理,確保數(shù)據(jù)完整性。
- 并發(fā)處理:允許并行處理數(shù)據(jù),提高處理速度。
- 重試機(jī)制:當(dāng)出現(xiàn)故障時,可以配置重試策略以重新處理失敗的記錄。
- 跳過機(jī)制:能夠跳過某些失敗的記錄而不中斷整個批處理作業(yè)。
- 持久化狀態(tài)管理:使用
JobRepository來跟蹤作業(yè)的狀態(tài),即使在系統(tǒng)重啟后也能恢復(fù)作業(yè)。 - 分片/分區(qū):可以將數(shù)據(jù)集分割成小塊,并在多個處理器上并行處理。
- 遠(yuǎn)程執(zhí)行:支持跨機(jī)器的作業(yè)執(zhí)行。
- 監(jiān)控和日志:提供詳細(xì)的日志記錄和作業(yè)執(zhí)行的監(jiān)控能力。
Spring Batch的架構(gòu)包括以下幾個核心組件:
- Job:這是批處理作業(yè)的最高級別抽象,可以包含一個或多個步驟。
- Step:是批處理作業(yè)中的一個邏輯單元,可以是任務(wù)步驟(如讀取、處理、寫入數(shù)據(jù))或決策步驟。
- ItemReader:負(fù)責(zé)從數(shù)據(jù)源讀取數(shù)據(jù)項。
- ItemProcessor:對讀取的數(shù)據(jù)項進(jìn)行處理。
- ItemWriter:將處理后的數(shù)據(jù)寫入目標(biāo)數(shù)據(jù)源。
- JobLauncher:負(fù)責(zé)啟動和執(zhí)行作業(yè)。
- JobRepository:管理作業(yè)的元數(shù)據(jù)和狀態(tài),通常與數(shù)據(jù)庫交互。
Spring Batch 不是一個調(diào)度框架,它專注于批處理作業(yè)的實(shí)現(xiàn)細(xì)節(jié),通常需要與其他調(diào)度框架(如 Quartz 或 Cron)結(jié)合使用,以便控制作業(yè)何時啟動。由于其高度的可配置性和靈活性,Spring Batch 成為了企業(yè)級批處理應(yīng)用的首選框架之一。
在Spring Boot項目中集成Spring Batch涉及幾個關(guān)鍵步驟,下面舉個例子,說明如何設(shè)置一個基本的Spring Batch環(huán)境:
1. 添加依賴
首先,在pom.xml文件中添加Spring Batch和Spring Boot Starter Batch的依賴:
<dependencies>
<!-- Spring Batch -->
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>4.x.y.RELEASE</version> <!-- 使用最新穩(wěn)定版 -->
</dependency>
<!-- Spring Boot Starter Batch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<!-- 數(shù)據(jù)庫連接池 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 數(shù)據(jù)庫驅(qū)動 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 如果使用H2作為內(nèi)存數(shù)據(jù)庫 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
2. 配置數(shù)據(jù)源和JobRepository
Spring Batch需要一個數(shù)據(jù)源來存儲作業(yè)元數(shù)據(jù)和狀態(tài)。
這通常通過application.properties或application.yml文件配置:
spring.datasource.url=jdbc:mysql://localhost:3306/batchdb spring.datasource.username=batchuser spring.datasource.password=batchpassword spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # Spring Batch配置 spring.batch.job.enabled=false # 設(shè)置為false,避免在啟動時自動執(zhí)行任何job
3. 創(chuàng)建Job和Step
定義一個Job,并為其創(chuàng)建一個或多個Step。
這通常通過一個@Configuration類和@EnableBatchProcessing注解完成:
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job importUserJob() {
return jobBuilderFactory.get("importUserJob")
.incrementer(new RunIdIncrementer())
.flow(importUserDataStep())
.end()
.build();
}
@Bean
public Step importUserDataStep() {
return stepBuilderFactory.get("importUserDataStep")
.<User, User>chunk(10)
.reader(userItemReader(null))
.processor(userItemProcessor())
.writer(userItemWriter())
.build();
}
}
4. 實(shí)現(xiàn)ItemReader, ItemProcessor, 和 ItemWriter
在上面的示例中,importUserDataStep()使用chunk-oriented步驟,這意味著它將數(shù)據(jù)分批處理。
你需要實(shí)現(xiàn)ItemReader, ItemProcessor, 和 ItemWriter來分別讀取、處理和寫入數(shù)據(jù):
@Bean
public FlatFileItemReader<User> userItemReader(Resource resource) {
DefaultLineMapper<User> lineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
tokenizer.setNames("firstName", "lastName");
BeanWrapperFieldSetMapper<User> fieldSetMapper = new BeanWrapperFieldSetMapper<>();
fieldSetMapper.setTargetType(User.class);
lineMapper.setLineTokenizer(tokenizer);
lineMapper.setFieldSetMapper(fieldSetMapper);
FlatFileItemReader<User> itemReader = new FlatFileItemReader<>();
itemReader.setResource(resource);
itemReader.setLinesToSkip(1); // 跳過標(biāo)題行
itemReader.setLineMapper(lineMapper);
return itemReader;
}
@Bean
public ItemProcessor<User, User> userItemProcessor() {
return new ItemProcessor<User, User>() {
@Override
public User process(User item) throws Exception {
item.setFirstName(item.getFirstName().toUpperCase());
return item;
}
};
}
@Bean
public JpaPagingItemWriter<User> userItemWriter(JpaItemWriterBuilder<User> builder) {
return builder
.entityManagerFactory(entityManagerFactory)
.build();
}
5. 啟動Job
在你的主類中,你可以注入JobLauncher和Job,然后調(diào)用它們來啟動作業(yè):
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job importUserJob;
// 在適當(dāng)?shù)牡胤秸{(diào)用
jobLauncher.run(importUserJob, new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters());
總結(jié)
以上步驟會幫助你在一個Spring Boot項目中集成Spring Batch。請注意,實(shí)際的配置可能需要根據(jù)你的具體需求和環(huán)境進(jìn)行調(diào)整。
這些僅為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 多線程并發(fā)編程_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java 多線程并發(fā)編程的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-05-05
springboot實(shí)現(xiàn)的https單向認(rèn)證和雙向認(rèn)證(java生成證書)
springboot https單向認(rèn)證和雙向認(rèn)證,本文主要介紹了springboot實(shí)現(xiàn)的https單向認(rèn)證和雙向認(rèn)證,具有一定的參考價值,感興趣的可以了解一下2024-04-04
SpringMVC @GetMapping注解路徑?jīng)_突問題解決
MD5對密碼進(jìn)行加密存儲是常見的一種加密方式,本文主要介紹了Java雙重MD5加密實(shí)現(xiàn)安全登錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
SpringMVC跨服務(wù)器上傳文件中出現(xiàn)405錯誤的解決
這篇文章主要介紹了SpringMVC跨服務(wù)器上傳文件中出現(xiàn)405錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Java數(shù)據(jù)結(jié)構(gòu)之平衡二叉樹的實(shí)現(xiàn)詳解
平衡二叉樹又被稱為AVL樹(有別于AVL算法),且具有以下性質(zhì):它是一棵空樹或它的左右兩個子樹的高度差的絕對值不超過1,并且左右兩個子樹都是一棵平衡二叉樹。本文將詳解介紹一下平衡二叉樹的原理與實(shí)現(xiàn),需要的可以參考一下2022-03-03
idea數(shù)據(jù)庫驅(qū)動下載失敗的問題及解決
這篇文章主要介紹了idea數(shù)據(jù)庫驅(qū)動下載失敗的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
idea2019導(dǎo)入maven項目中的某些問題及解決方法
這篇文章主要介紹了idea2019導(dǎo)入maven項目中的某些問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
mybatis 使用jdbc.properties文件設(shè)置不起作用的解決方法
這篇文章主要介紹了mybatis 使用jdbc.properties文件設(shè)置不起作用的解決方法,需要的朋友可以參考下2018-03-03

