我将向您展示如何使用Spring Boot创建一个的Spring Batch的Hello World示例。

(循序渐进)

因此,如果您是Spring Batch的初学者,您一定会喜欢本指南。

准备好了吗?

如果您想了解更多关于Spring Batch的信息,请访问

Spring Batch教程页面。

1.Spring Batch框架工作原理

在深入研究代码之前,让我们先看看Spring Batch框架。它包含以下主要构建块:

一个Batch(批处理)过程由一个Job(作业)组成。这个实体封装了整个批处理过程。

一个Job(作业)可以由一个或多个Step(步骤)组成。在大多数情况下,一个步骤将读取数据(通过ItemReader),处理数据(使用ItemProcessor),然后写入数据(通过ItemWriter)。

JobLauncher处理启动一个Job(作业)。

最后,JobRepository存储关于配置和执行的Job(作业)的元数据。

为了演示Spring Batch是如何工作的,让我们构建一个简单的Hello World批处理作业。

在本例中,我们从person.csv文件中读取一个人的姓和名。从这些数据生成一个问候语。然后将此问候语写入greeting .txt文件。

2.示例概述

我们会使用以下工具/框架:

  • Spring Batch 4.1
  • Spring Boot 2.1
  • Maven 3.6

我们的项目目录结构如下:

3. Maven配置

我们使用Maven构建并运行示例。如果还没有,下载并安装Apache Maven。

让我们使用Spring Initializr来生成Maven项目。确保选择Batch作为依赖项。

单击Generate Project生成并下载Spring Boot项目模板。在项目的根目录中,您将发现一个pom.xml文件,它是Maven项目的XML配置文件。

为了避免必须管理不同Spring依赖项的版本兼容性,我们将从spring-boot-starter-parent 父POM继承默认配置。

生成的项目包含Spring Boo Starters管理着不同的Spring依赖项。

spring-boot-starter-batch导入Spring Boot和Spring Batch依赖项。

spring-boot-starter-test 包含用于测试Spring引导应用程序的依赖项。它导入了包括JUnit、Hamcrest和Mockito在内的库。

这个也有依赖性spring-batch-test。这个库包含一些帮助类,它们将帮助测试批处理作业。

在plugins部分,您将找到Spring Boot Maven插件:spring-boot-maven- plugin。它帮助我们构建一个单一的、可运行的uber-jar。这是执行和发布代码的一种方便方法。此外,插件允许您通过Maven命令启动示例。

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.codenotfound</groupId>
<artifactId>spring-batch-hello-world</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring-batch-hello-world</name>
<description>Spring Batch Hello World Example</description>
<url>https://codenotfound.com/spring-batch-example.html</url> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent> <properties>
<java.version>11</java.version>
</properties> <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>
</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> </project>

4. Spring Boot 配置

我们使用Spring Boot,目的是让一个Spring Batch应用程序可以“直接运行”。
首先创建一个SpringBatchApplication类。它包含main()方法,该方法使用Spring Boot的SpringApplication.run()启动应用程序。

注意
@SpringBootApplication是一个方便的注解,它添加了:@Configuration、@EnableAutoConfiguration和@ComponentScan。

有关Spring Boot的更多信息,请查看Spring Boot入门指南。

默认情况下,Spring Batch使用数据库存储已配置的批处理作业上的元数据。

在本例中,我们不直接使用数据库,而是使用基于内存映射的Map,运行Spring Batch。

spring-boot-starter-batch starter依赖于spring-boot-starter-jdbc,并将尝试实例化数据源。添加 exclude = {DataSourceAutoConfiguration.class}注解中添加@SpringBootApplication。这可以防止Spring Boot为数据库连接自动配置DataSource。

package com.codenotfound;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SpringBatchApplication { public static void main(String[] args) {
SpringApplication.run(SpringBatchApplication.class, args);
}
}

5. 创建实体模型

在处理数据之前,通常希望将其映射到实体对象。

在我的示例中,输入数据存储在src/test/resources/csv/persons.csv文件中。

文件中的每一行都包含一个逗号分隔的姓和名。

John, Doe
Jane, Doe

我们将把这个数据映射到Person对象。这是一个包含姓和名的简单POJO。

package com.codenotfound.model;

public class Person {
private String firstName;
private String lastName; public Person() {
// default constructor
} public String getFirstName() {
return firstName;
} public void setFirstName(String firstName) {
this.firstName = firstName;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
} @Override
public String toString() {
return firstName + " " + lastName;
}
}

6. 配置 Spring Batch Job

我们首先创建一个BatchConfig类,它将配置Spring Batch。类顶部的@Configuration注解表明Spring可以使用该类作为bean定义的源。

我们添加了@EnableBatchProcessing注解,它支持所有所需Spring Batch特性。它还提供了设置批处理作业的基本配置。

通过添加这个注解会需要很多操作。下面是@EnableBatchProcessing创建的概述:

  • JobRepository (bean名称 "jobRepository")
  • JobLauncher (bean名称 "jobLauncher")
  • JobRegistry (bean名称 "jobRegistry")
  • JobExplorer (bean名称 "jobExplorer")
  • PlatformTransactionManager (bean名称 "transactionManager")
  • JobBuilderFactory (bean名称"jobBuilders"),它可以方便地防止您必须将作业存储库注入到每个Job(作业)中
  • StepBuilderFactory (bean名称 "stepBuilders"),以方便您避免将作业存储库和事务管理器注入到每个Step(步骤)中

为了使Spring Batch使用基于Map的JobRepository,我们需要扩展DefaultBatchConfigurer。重写setDataSource()方法以不设置DataSource。这将导致自动配置使用基于Map的JobRepository。

package com.codenotfound.batch;

import javax.sql.DataSource;
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.context.annotation.Configuration; @Configuration@EnableBatchProcessingpublic class BatchConfig extends DefaultBatchConfigurer { @Override
public void setDataSource(DataSource dataSource) {
// initialize will use a Map based JobRepository (instead of database)
}
}

现在让我们继续配置Hello World Spring Batch 作业。

创建一个HelloWorldJobConfig配置类,并用添加@Configuration注解。

在HelloWorldJobConfig Bean中,我们使用JobBuilderFactory来创建作业。我们传递Job(作业)的名称和需要运行的Step(步骤)。

注意
在helloWorlJob()Bean中,Spring将自动注入 jobBuilders 和 stepBuilders Beans。

在HelloWorldStepBean中定义了我们的步骤执行的不同项。我们使用StepBuilderFactory创建步骤。

首先,我们传入步骤的名称。使用chunk(),我们指定每个事务中处理的项的数量。Chunk还指定步骤的输入(Person)和输出(String)类型。然后,我们将ItemReader (reader)、ItemProcessor (processor)和ItemWriter (writer)添加到步骤中。

我们使用FlatFileItemReader读取person CSV文件。这个类提供了读取和解析CSV文件的基本功能。

有一个FlatFileItemReaderBuilder实现,它允许我们创建一个FlatFileItemReader。我们首先指定读取文件中每一行的结果是Person对象。然后,我们将使用name()方法为FlatFileItemReader添加一个名称,并指定需要读取的资源(在本例中是persons.csv文件)。

为了让FlatFileItemReader处理我们的文件,我们需要指定一些额外的信息。首先,我们定义文件中的数据是带分隔符的(默认为逗号作为分隔符)。

我们还指定了如何将一行中的每个字段映射到Person对象。这是使用names()来完成的,通过将名称与对象上的setter匹配,可以使Spring Batch映射字段。
在本文的例子中,一行的第一个字段将使用firstName setter进行映射。为了实现这一点,我们还需要指定targetType,即Person对象。

注意:

names(new String[] {"firstName", "lastName"})

PersonItemProcessor处理数据。它将一个Person转换成一个问候String。我们将在下面的一个单独的类中定义它。

一旦数据被处理,我们将把它写入一个文本文件。我们使用FlatFileItemWriter来完成这项任务。

我们使用FlatFileItemWriterBuilder实现来创建一个FlatFileItemWriter。我们为writer添加一个名称,并指定需要将数据写入其中的资源(在本例中是greeting.txt文件)。

FlatFileItemWriter需要知道如何将生成的输出转换成可以写入文件的单个字符串。在本例中,我们的输出已经是一个字符串,我们可以使用PassThroughLineAggregator。这是最基本的实现,它假定对象已经是一个字符串。

package com.codenotfound.batch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import com.codenotfound.model.Person; @Configurationpublic class HelloWorldJobConfig { @Bean
public Job helloWorlJob(JobBuilderFactory jobBuilders,
StepBuilderFactory stepBuilders) {
return jobBuilders.get("helloWorldJob")
.start(helloWorldStep(stepBuilders)).build();
} @Bean
public Step helloWorldStep(StepBuilderFactory stepBuilders) {
return stepBuilders.get("helloWorldStep")
.<Person, String>chunk(10).reader(reader())
.processor(processor()).writer(writer()).build();
} @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();
} @Bean
public PersonItemProcessor processor() {
return new PersonItemProcessor();
} @Bean
public FlatFileItemWriter<String> writer() {
return new FlatFileItemWriterBuilder<String>()
.name("greetingItemWriter")
.resource(new FileSystemResource(
"target/test-outputs/greetings.txt"))
.lineAggregator(new PassThroughLineAggregator<>()).build();
}
}

7. 处理数据

在大多数情况下,您将希望在批处理作业期间应用一些数据处理。可以使用ItemProcessor来操作。

在我们的示例中,我们将Person对象转换为一个简单的问候语String

为此,我们创建一个实现ItemProcessor接口的PersonItemProcessor。我们实现了process()方法,它将人名和姓氏添加到字符串中。

调试的过程中,我们记录日志结果。

package com.codenotfound.batch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import com.codenotfound.model.Person; public 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;
}
}

8.测试Spring Batch 示例

为了测试本的例子,我们创建了一个基本的单元测试用例。它将运行批处理作业并检查是否成功完成。

我们使用@RunWith和@SpringBootTest测试注解告诉JUnit使用Spring的测试支持运行,并使用SpringBoot的支持引导。

Spring Batch附带一个JobLauncherTestUtils实用程序类,用于测试批处理作业。

我们首先创建一个内部BatchTestConfig类,将helloWorld作业添加到JobLauncherTestUtils bean中。然后使用此bean的launchJob()方法运行批处理作业。

如果执行的作业没有任何错误,则ExitCode的值为COMPLETED。

package com.codenotfound;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import com.codenotfound.batch.job.BatchConfig;
import com.codenotfound.batch.job.HelloWorldJobConfig; @RunWith(SpringRunner.class)
@SpringBootTest(
classes = {SpringBatchApplicationTests.BatchTestConfig.class})
public class SpringBatchApplicationTests { @Autowired
private JobLauncherTestUtils jobLauncherTestUtils; @Test
public void testHelloWorldJob() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
assertThat(jobExecution.getExitStatus().getExitCode())
.isEqualTo("COMPLETED");
} @Configuration
@Import({BatchConfig.class, HelloWorldJobConfig.class})
static class BatchTestConfig { @Autowired
private Job helloWorlJob; @Bean
JobLauncherTestUtils jobLauncherTestUtils()
throws NoSuchJobException {
JobLauncherTestUtils jobLauncherTestUtils =
new JobLauncherTestUtils();
jobLauncherTestUtils.setJob(helloWorlJob); return jobLauncherTestUtils;
}
}
}

要触发上述测试用例,请在项目根文件夹中打开命令提示符,并执行以下Maven命令:

mvn test

结果是构建成功,并在此期间执行批处理作业。

 .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.5.RELEASE) 2019-05-30 19:11:12.784 INFO 14588 --- [ main] c.c.SpringBatchApplicationTests : Starting SpringBatchApplicationTests on DESKTOP-2RB3C1U with PID 14588 (started by Codenotfound in C:\Users\Codenotfound\repos\spring-batch\spring-batch-hello-world)
2019-05-30 19:11:12.785 INFO 14588 --- [ main] c.c.SpringBatchApplicationTests : No active profile set, falling back to default profiles: default
2019-05-30 19:11:13.305 WARN 14588 --- [ main] o.s.b.c.c.a.DefaultBatchConfigurer : No datasource was provided...using a Map based JobRepository
2019-05-30 19:11:13.306 WARN 14588 --- [ main] o.s.b.c.c.a.DefaultBatchConfigurer : No transaction manager was provided, using a ResourcelessTransactionManager
2019-05-30 19:11:13.328 INFO 14588 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : No TaskExecutor has been set, defaulting to synchronous executor.
2019-05-30 19:11:13.350 INFO 14588 --- [ main] c.c.SpringBatchApplicationTests : Started SpringBatchApplicationTests in 0.894 seconds (JVM running for 1.777)
2019-05-30 19:11:13.732 INFO 14588 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=helloWorldJob]] launched with the following parameters: [{random=459672}]
2019-05-30 19:11:13.759 INFO 14588 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [helloWorldStep]
2019-05-30 19:11:13.812 INFO 14588 --- [ main] c.c.batch.PersonItemProcessor : converting 'John Doe' into 'Hello John Doe!'
2019-05-30 19:11:13.822 INFO 14588 --- [ main] c.c.batch.PersonItemProcessor : converting 'Jane Doe' into 'Hello Jane Doe!'
2019-05-30 19:11:13.842 INFO 14588 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=helloWorldJob]] completed with the following parameters: [{random=459672}] and the following status: [COMPLETED]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.953 s - in com.codenotfound.SpringBatchApplicationTests
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.852 s
[INFO] Finished at: 2019-05-30T19:11:14+02:00
[INFO] ------------------------------------------------------------------------

您可以在target/test-output /greeting .txt文件中找到结果:

Hello John Doe!
Hello Jane Doe!

如果您想运行上面的代码示例,您可以在这里获得完整的源代码。

在本入门教程中,您学习了如何使用Spring Boot和Maven创建一个简单的Spring Batch示例。

原文链接:https://codenotfound.com/spring-batch-example.html

作者:codenotfound

译者:李东

Spring Batch 入门级示例教程的更多相关文章

  1. 简单的Spring Batch示例

    使用Spring Batch做为批处理框架,可以完成常规的数据量不是特别大的离线计算. 现在写一个简单的入门版示例. 这里默认大家已经掌握了Spring Batch的基本知识,示例只是为了快速上手实践 ...

  2. Spring Batch教程

    Spring Batch系列总括 : http://www.cnblogs.com/gulvzhe/archive/2011/12/20/2295090.html

  3. Spring Batch在大型企业中的最佳实践

    在大型企业中,由于业务复杂.数据量大.数据格式不同.数据交互格式繁杂,并非所有的操作都能通过交互界面进行处理.而有一些操作需要定期读取大批量的数据,然后进行一系列的后续处理.这样的过程就是" ...

  4. Spring Batch 批处理框架

    <Spring Batch 批处理框架>基本信息作者: 刘相 出版社:电子工业出版社ISBN:9787121252419上架时间:2015-1-24出版日期:2015 年2月开本:16开页 ...

  5. Spring Batch实践

    Spring Batch在大型企业中的最佳实践 在大型企业中,由于业务复杂.数据量大.数据格式不同.数据交互格式繁杂,并非所有的操作都能通过交互界面进行处理.而有一些操作需要定期读取大批量的数据,然后 ...

  6. Spring Batch学习

    今天准备研究下Spring Batch,然后看了一系列资料,如下还是比较好的教程吧. 链接: http://www.cnblogs.com/gulvzhe/archive/2011/12/20/229 ...

  7. 图书简介:Spring Batch批处理框架

    大数据时代批处理利器,国内首度原创解析Spring Batch框架. 内容简介: <Spring Batch 批处理框架>全面.系统地介绍了批处理框架Spring Batch,通过详尽的实 ...

  8. Spring Batch 专题

    如今微服务架构讨论的如火如荼.但在企业架构里除了大量的OLTP交易外,还存在海量的批处理交易.在诸如银行的金融机构中,每天有3-4万笔的批处理作业需要处理.针对OLTP,业界有大量的开源框架.优秀的架 ...

  9. Spring Cloud Alibaba基础教程:Nacos的集群部署

    继续说说生产环境的Nacos搭建,通过上一篇<Spring Cloud Alibaba基础教程:Nacos的数据持久化>的介绍,我们已经知道Nacos对配置信息的存储原理,在集群搭建的时候 ...

随机推荐

  1. 专访黄勇:Java在未来的很长一段时间仍是主流(把老板当情人,把同事当小孩,把客户当病人)

    url:http://www.csdn.net/article/2015-09-06/2825621 2015-09-06 13:18 摘要:本文采访了现任阿里巴巴公司系统架构师黄勇,从事近十年的Ja ...

  2. TCP网络通讯如何解决分包粘包问题(有模拟代码)

    TCP作为常用的网络传输协议,数据流解析是网络应用开发人员永远绕不开的一个问题. TCP数据传输是以无边界的数据流传输形式,所谓无边界是指数据发送端发送的字节数,在数据接收端接受时并不一定等于发送的字 ...

  3. ios 双指捏合放大缩小图片的例子

    图片跟随双指捏合的距离放大或者缩小. 利用-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  实现. touchesMov ...

  4. rdlc水晶报表在wpf里的使用

    1引用程序集 Microsoft.ReportViewer.WinForms 2 xaml 命名空间 xmlns:rv="clr-namespace:Microsoft.Reporting. ...

  5. Win8 Metro(C#)数字图像处理--2.71Sigma平滑滤波器

    原文:Win8 Metro(C#)数字图像处理--2.71Sigma平滑滤波器  [算法说明]   Sigma平滑滤波器是构造一个模板,比如3*3大小的模板,计算这个模板对应的像素的标准差d,然后 ...

  6. Redis实现Timeline

    上回写了[使用Redis实现关注关系][1],这次说说使用Redis实现Timeline. Timeline的实现一般有推模式.拉模式.推拉结合这几种. 推模式:某人发布内容之后推送给所有粉丝,空间换 ...

  7. Resolve conflict using "MERGE_HEAD (origin/HEAD)"

    Git进行同步的时候,经常会出现冲突,有时候冲突的选项会有图示中的三种选项: 1.Resolved:直接把文件标识为冲突已经解决,一般是自己手动查看并解决完冲突以后使用. 2.Resolve conf ...

  8. 危险的DDD聚合根

    原文:危险的DDD聚合根 DDD的核心是聚合.这没有问题,大家都认同.但关于DDD中的聚合方式,其实我还是有些担心,下面说说我的想法,希望大家参与讨论.其实当初第一次看到DDD中关于聚合根部分论述的时 ...

  9. .net core 利用Selenium和PhantomJS后台生成EChart图片

    1.引用 NuGet安装: Selenium.Support Selenium.WebDriver Selenium.WebDriver.PhantomJS.CrossPlatform  (分布Lin ...

  10. 企业级架构 MVVM 模式指南 (WPF 和 Silverlight 实现) 译(2)

    本书包含的章节内容 第一章:表现模式,以一个例子呈献给读者表现模式的发展历程,我们会用包括MVC和MVP在内的各种方式实现一个收费项目的例子.沿此方向,我们会发现每一种模式的问题所在,这也是触发设计模 ...