久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

手把手教你搭建第一個Spring Batch項目的步驟

瀏覽:7日期:2023-08-17 17:52:54

一、概述

Spring Batch是一個輕量級,全面的批處理框架。

一個典型的批處理過程可能是:

從數(shù)據(jù)庫,文件或隊列中讀取大量記錄。 以某種方式處理數(shù)據(jù)。 以修改之后的形式寫回數(shù)據(jù)

Spring Batch 應(yīng)用架構(gòu)圖:

手把手教你搭建第一個Spring Batch項目的步驟

一個Batch(批處理)過程由一個Job(作業(yè))組成。這個實體封裝了整個批處理過程。

一個Job(作業(yè))可以由一個或多個Step(步驟)組成。在大多數(shù)情況下,一個步驟將讀取數(shù)據(jù)(通過ItemReader),處理數(shù)據(jù)(使用ItemProcessor),然后寫入數(shù)據(jù)(通過ItemWriter)。

JobLauncher處理啟動一個Job(作業(yè))。

最后,JobRepository存儲關(guān)于配置和執(zhí)行的Job(作業(yè))的元數(shù)據(jù)。

二、實例

1、新建 springboot項目

創(chuàng)建項目傳送門

選擇配置,添加依賴,GENERATE 后導(dǎo)入到你的IDE

手把手教你搭建第一個Spring Batch項目的步驟

2、springboot 項目配置

2.1 在新建項目時添加依賴了,就會發(fā)現(xiàn)pom中引入了 spring-barch的相關(guān)依賴,如新建項目時沒有添加依賴,則需要手動添加。

//pom.xml<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-batch</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.batch</groupId><artifactId>spring-batch-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>

2.2 為主程序的@SpringBootApplication注解添加exclude屬性,可以防止 SpringBoot 為數(shù)據(jù)庫連接自動配置 DataSource

//主程序@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)public class Springbatch2020829Application {public static void main(String[] args) {SpringApplication.run(Springbatch2020829Application.class, args);}}

2.3 新建實體model

//Person.javapublic class Person { private String firstName; private String lastName;}//構(gòu)造函數(shù),get,set方法, toString()方法略

2.4 配置 Spring Batch Job

2.4.1 新建 BatchConfig 類,重寫父類 setDataSource 方法

//BatchConfig.java@Configuration@EnableBatchProcessingpublic class BatchConfig extends DefaultBatchConfigurer { @Override public void setDataSource(DataSource dataSource) { }}

2.4.2 新建 HelloWorldJobConfig 類,配置 job ,step

//HelloWorldJobConfig.java@Configurationpublic class HelloWorldJobConfig { //新建 Job,Spring 將自動注入 jobBuilders ,stepBuilders兩個 beans @Bean public Job helloWorlJob(JobBuilderFactory jobBuilders, StepBuilderFactory stepBuilders) { return jobBuilders.get('helloWorldJob') .start(helloWorldStep(stepBuilders)).build(); } //新建 Step,使用 StepBuilderFactory 創(chuàng)建 @Bean public Step helloWorldStep(StepBuilderFactory stepBuilders) { return stepBuilders.get('helloWorldStep') .<Person, String>chunk(10).reader(reader()) .processor((Function<? super Person, ? extends String>) processor()).writer(writer()).build(); } //讀取數(shù)據(jù),指定需要讀取的資源 @Bean public FlatFileItemReader<Person> reader() { return new FlatFileItemReaderBuilder<Person>() .name('personItemReader') .resource(new ClassPathResource('csv/persons.csv')) .delimited().names(new String[] {'firstName', 'lastName'}) .targetType(Person.class).build(); } //處理數(shù)據(jù) @Bean public PersonItemProcessor processor() { return new PersonItemProcessor(); } //寫入數(shù)據(jù),指定寫入路徑文件 @Bean public FlatFileItemWriter<String> writer() { return new FlatFileItemWriterBuilder<String>() .name('greetingItemWriter') .resource(new FileSystemResource( 'target/test-outputs/greetings.txt')) .lineAggregator(new PassThroughLineAggregator<>()).build(); }}

2.5 處理數(shù)據(jù)

//PersonItemProcessor.javapublic class PersonItemProcessor implements ItemProcessor<Person, String> { private static final Logger LOGGER = LoggerFactory.getLogger(PersonItemProcessor.class); //打印日志信息 @Override public String process(Person person) throws Exception { String greeting = 'Hello ' + person.getFirstName() + ' ' + person.getLastName() + '!'; LOGGER.info('converting ’{}’ into ’{}’', person, greeting); return greeting; }}

2.6 測試 Spring Batch 示例

//PersonItemProcessor.javapublic class PersonItemProcessor implements ItemProcessor<Person, String> { private static final Logger LOGGER = LoggerFactory.getLogger(PersonItemProcessor.class); //打印日志信息 @Override public String process(Person person) throws Exception { String greeting = 'Hello ' + person.getFirstName() + ' ' + person.getLastName() + '!'; LOGGER.info('converting ’{}’ into ’{}’', person, greeting); return greeting; }}

2.7 啟動項目,在 target/test-outputs/greetings.txt 文件中找到結(jié)果。

手把手教你搭建第一個Spring Batch項目的步驟

三、理解

JobRepository

從字面上可以理解為'任務(wù)倉庫',如果把一個批處理比作一個任務(wù)的話,這個倉庫存儲了很多這種任務(wù)。JobRepository 會將任務(wù)包括其狀態(tài)等數(shù)據(jù)持久化,存儲到許多數(shù)據(jù)庫中。Spring Batch 默認(rèn)會提供一個 SimpleJobRepository 倉庫,方便我們開啟批處理。

Job

“任務(wù)”。每個批處理都是一個任務(wù),除了任務(wù)本身之外,任務(wù)也存在成功和失敗等等狀態(tài),所以可以引出兩個概念 JobInstance 與 JobExecution 。job 是一個接口,JobInstance 是其實現(xiàn),代表了“任務(wù)”本身,提供了 getJobName、getInstanceId 等方法供我們獲取任務(wù)本身的一些屬性。JobExecution 代表任務(wù)的狀態(tài),如創(chuàng)建時間、結(jié)束時間、結(jié)束狀態(tài)、拋出的異常等等。

Step

“步驟”。批處理任務(wù)肯定有非常多的步驟,如一個最基本的數(shù)據(jù)庫同步,從 A 數(shù)據(jù)庫讀取數(shù)據(jù),存入到 B 數(shù)據(jù)庫中,這里就分為了兩個步驟。在 Spring Batch 中,一個任務(wù)可以有很多個步驟,每個步驟大致分為三步:讀、處理、寫,其對應(yīng)的類分別就是 Item Reader,Item Processor,Item Writer。

JobLauncher

“任務(wù)裝置”。如火箭發(fā)射裝置就是用來操作火箭發(fā)射的,這里的任務(wù)裝置就是用來執(zhí)行任務(wù)的。

到此這篇關(guān)于手把手教你搭建第一個Spring Batch項目的步驟的文章就介紹到這了,更多相關(guān)Spring Batch項目搭建內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 新和县| 黎平县| 松溪县| 页游| 加查县| 武城县| 宽甸| 嘉善县| 淮南市| 仙桃市| 台山市| 清涧县| 苍溪县| 永福县| 南漳县| 象州县| 全州县| 武强县| 略阳县| 昌邑市| 巴林左旗| 建湖县| 黄浦区| 乌海市| 宜川县| 湖南省| 册亨县| 大余县| 来宾市| 赫章县| 西乡县| 皮山县| 衡东县| 合川市| 泽库县| 灵石县| 瓦房店市| 杭州市| 永定县| 苏州市| 堆龙德庆县|