Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Creating a Batch Service :: Learn how to create a basic batch-driven solution.

License

NotificationsYou must be signed in to change notification settings

spring-guides/gs-batch-processing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

This guide walks you through the process of creating a basic batch-driven solution.

What You Will build

You will build a service that imports data from a CSV spreadsheet, transforms it with custom code, and stores the final results in a database.

Starting with Spring Initializr

You can use thispre-initialized project and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.

To manually initialize the project:

  1. Navigate tohttps://start.spring.io.This service pulls in all the dependencies you need for an application and does most of the setup for you.

  2. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.

  3. ClickDependencies and selectSpring Batch andHyperSQL Database.

  4. ClickGenerate.

  5. Download the resulting ZIP file, which is an archive of an application that is configured with your choices.

Note
If your IDE has the Spring Initializr integration, you can complete this process from your IDE.
Note
You can also fork the project from GitHub and open it in your IDE or other editor.

Business Data

Typically, your customer or a business analyst supplies a spreadsheet. For this simpleexample, you can find some made-up data insrc/main/resources/sample-data.csv:

link:initial/src/main/resources/sample-data.csv[role=include]

This spreadsheet contains a first name and a last name on each row, separated by a comma.This is a fairly common pattern that Spring can handle without customization.

Next, you need to write an SQL script to create a table to store the data. You can findsuch a script insrc/main/resources/schema-all.sql:

link:initial/src/main/resources/schema-all.sql[role=include]
Note
Spring Boot runsschema-@@platform@@.sql automatically during startup.-all is the default for all platforms.

Create a Business Class

Now that you can see the format of data inputs and outputs, you can write code torepresent a row of data, as the following example (fromsrc/main/java/com/example/batchprocessing/Person.java) shows:

link:complete/src/main/java/com/example/batchprocessing/Person.java[role=include]

You can instantiate thePerson record with first name and last name through the constructor.

Create an Intermediate Processor

A common paradigm in batch processing is to ingest data, transform it, and then pipe itout somewhere else. Here, you need to write a simple transformer that converts the namesto uppercase. The following listing (fromsrc/main/java/com/example/batchprocessing/PersonItemProcessor.java) shows how to do so:

link:complete/src/main/java/com/example/batchprocessing/PersonItemProcessor.java[role=include]

PersonItemProcessor implements Spring Batch’sItemProcessor interface. This makes iteasy to wire the code into a batch job that you will define later in this guide. Accordingto the interface, you receive an incomingPerson object, after which you transform it toan upper-casedPerson.

Note
The input and output types need not be the same. In fact, after one source of datais read, sometimes the application’s data flow needs a different data type.

Put Together a Batch Job

Now you need to put together the actual batch job. Spring Batch provides many utilityclasses that reduce the need to write custom code. Instead, you can focus on the businesslogic.

To configure your job, you must first create a Spring@Configuration class like the following example insrc/main/java/com/example/batchprocessing/BatchConfiguration.java. This example uses a memory-based database,meaning that, when it is done, the data is gone.Now add the following beans to yourBatchConfiguration class to define a reader, a processor, and a writer:

link:complete/src/main/java/com/example/batchprocessing/BatchConfiguration.java[role=include]

The first chunk of code defines the input, processor, and output.

  • reader() creates anItemReader. It looks for a file calledsample-data.csv andparses each line item with enough information to turn it into aPerson.

  • processor() creates an instance of thePersonItemProcessor that you defined earlier,meant to convert the data to upper case.

  • writer(DataSource) creates anItemWriter. This one is aimed at a JDBC destination andautomatically gets aDataSource created by Spring Boot. Itincludes the SQL statement needed to insert a singlePerson, driven by Java record components.

The last chunk (fromsrc/main/java/com/example/batchprocessing/BatchConfiguration.java)shows the actual job configuration:

link:complete/src/main/java/com/example/batchprocessing/BatchConfiguration.java[role=include]

The first method defines the job, and the second one defines a single step. Jobs are builtfrom steps, where each step can involve a reader, a processor, and a writer.

You then list each step, (though this job has only one step). The jobends, and the Java API produces a perfectly configured job.

In the step definition, you define how much data to write at a time. In this case, itwrites up to three records at a time. Next, you configure the reader, processor, and writerby using the beans injected earlier.

Note
chunk() is prefixed<Person,Person> because it is a generic method. Thisrepresents the input and output types of each “chunk” of processing and lines up withItemReader<Person> andItemWriter<Person>.

The last bit of batch configuration is a way to get notified when the job completes. Thefollowing example (fromsrc/main/java/com/example/batchprocessing/JobCompletionNotificationListener.java) showssuch a class:

link:complete/src/main/java/com/example/batchprocessing/JobCompletionNotificationListener.java[role=include]

TheJobCompletionNotificationListener listens for when a job isBatchStatus.COMPLETEDand then usesJdbcTemplate to inspect the results.

Make the Application Executable

Although batch processing can be embedded in web apps and WAR files, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Javamain() method.

The Spring Initializr created an application class for you. For this simple example, itworks without further modification. The following listing (fromsrc/main/java/com/example/batchprocessing/BatchProcessingApplication.java) shows the application class:

link:complete/src/main/java/com/example/batchprocessing/BatchProcessingApplication.java[role=include]

Note thatSpringApplication.exit() andSystem.exit() ensure that the JVM exits upon job completion.See theApplication Exit section in Spring Boot Reference documentation for more details.

For demonstration purposes, there is code to inject aJdbcTemplate, query the database,and print out the names of people the batch job inserts.

Note

Note how the application does not use the@EnableBatchProcessing annotation.Previously,@EnableBatchProcessing could be used to enable Spring Boot’s auto-configuration of Spring Batch.A bean that is annotated with@EnableBatchProcessing or that extends Spring Batch’sDefaultBatchConfiguration can now be defined to tell the auto-configurationto back off, allowing the application to take complete control of how Spring Batch is configured.

The job prints out a line for each person that gets transformed. After the job runs, youcan also see the output from querying the database. It should resemble the followingoutput:

Converting (Person[firstName=Jill, lastName=Doe]) into (Person[firstName=JILL, lastName=DOE])Converting (Person[firstName=Joe, lastName=Doe]) into (Person[firstName=JOE, lastName=DOE])Converting (Person[firstName=Justin, lastName=Doe]) into (Person[firstName=JUSTIN, lastName=DOE])Converting (Person[firstName=Jane, lastName=Doe]) into (Person[firstName=JANE, lastName=DOE])Converting (Person[firstName=John, lastName=Doe]) into (Person[firstName=JOHN, lastName=DOE])Found <Person[firstName=JILL, lastName=DOE]> in the database.Found <Person[firstName=JOE, lastName=DOE]> in the database.Found <Person[firstName=JUSTIN, lastName=DOE]> in the database.Found <Person[firstName=JANE, lastName=DOE]> in the database.Found <Person[firstName=JOHN, lastName=DOE]> in the database.

Summary

Congratulations! You built a batch job that ingested data from a spreadsheet, processedit, and wrote it to a database.

About

Creating a Batch Service :: Learn how to create a basic batch-driven solution.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors22

Languages


[8]ページ先頭

©2009-2025 Movatter.jp