This topic describes how to build an application using the Spring Batch framework and OceanBase Cloud to perform basic operations such as creating tables, inserting data, and querying data.
Prerequisites
- You have registered an OceanBase Cloud account and created a cluster instance and an OceanBase Cloud tenant in Oracle-compatible mode. For more information, see Create a cluster instance and Create a tenant.
- You have installed JDK 1.8 and Maven.
- You have installed IntelliJ IDEA.
Note
The tool used to run the sample code in this topic is IntelliJ IDEA 2021.3.2 (Community Edition). You can also choose a tool that suits your personal preference to run the sample code.
Procedure
Note
The procedure described in this topic is based on the Windows environment. If you are using a different operating system or compiler, the steps may vary slightly.
- Obtain the connection string for the OceanBase Cloud database.
- Import the
java-oceanbase-springbatchproject into IntelliJ IDEA. - Modify the database connection information in the
java-oceanbase-springbatchproject. - Run the
java-oceanbase-springbatchproject.
Step 1: Obtain the OceanBase Cloud database connection string
Log in to the OceanBase Cloud console. On the instance list page, expand the target instance's information, and under the target tenant, select Connect > Get connection string.
For more detailed operations, see Obtain a connection string.
Fill in the corresponding information in the URL below based on your created OceanBase Cloud database.
Example URL for connecting to an OceanBase Cloud database in Oracle-compatible mode:
jdbc:oceanbase://$host:$port/$schema_name?user=$user_name&password=$passwordParameter description:
$host: The connection address for the OceanBase Cloud database, for example,t********.********.oceanbase.cloud.$port: The connection port for the OceanBase Cloud database, default is 1521.$schema_name: The name of the schema to access.$user_name: The account for accessing the database.$password: The password for the account.
For more information about URL parameters, see Database URL.
Step 2: Import the java-oceanbase-springbatch project into IntelliJ IDEA
Open IntelliJ IDEA and select File > Open....
In the Open File or Project window, select the corresponding project file and click OK to import it.
IntelliJ IDEA automatically recognizes the various files in the project. In the Project tool window, you can view the project's directory structure, file list, module list, dependencies, and other information. The Project tool window is usually located on the far left side of the IntelliJ IDEA interface and is open by default. If the Project tool window is closed, you can reopen it by clicking View > Tool Windows > Project in the menu bar or using the shortcut key Alt + 1.
Note
When importing a project with IntelliJ IDEA, it automatically detects the pom.xml file in the project, downloads the required dependency libraries based on the dependencies described in the file, and adds them to the project.
View the project.
Step 3: Modify the database connection information in the java-oceanbase-springbatch project
Modify the database connection information in the application.properties file based on the information obtained in Step 1: Obtain the OceanBase Cloud connection string.
Example:
- The database driver name is:
com.oceanbase.jdbc.Driver. - The OceanBase Cloud connection address is
t5******.********.oceanbase.cloud. - The access port is 1521.
- The name of the schema to access is
sys. - The tenant connection account is oracle001.
- The password is
******.
Sample code:
spring.datasource.driver-class-name=com.oceanbase.jdbc.Driver
spring.datasource.url=jdbc:oceanbase://t5******.********.oceanbase.cloud:1521/sys?characterEncoding=utf-8
spring.datasource.username=oracle001
spring.datasource.password=******
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.batch.job.enabled=false
logging.level.org.springframework=INFO
logging.level.com.example=DEBUG
Step 4: Run the java-oceanbase-springbatch project
Run the
AddDescPeopleWriterTest.javafile.- Locate the
AddDescPeopleWriterTest.javafile in the src > test > java directory of the project structure. - In the toolbar menu, select Run > Run... > AddDescPeopleWriterTest.testWrite, or directly click the green triangle in the upper-right corner to run it.
- View the project's log information and output results through IDEA's console.
Data in the people_desc table: PeopleDESC [name=John, age=25, desc=This is John with age 25] PeopleDESC [name=Alice, age=30, desc=This is Alice with age 30] Batch Job execution completed.- Locate the
Run the
AddPeopleWriterTest.javafile.- Locate the
AddPeopleWriterTest.javafile in the src > test > java directory of the project structure. - In the toolbar menu, select Run > Run... > AddPeopleWriterTest.testWrite, or directly click the green triangle in the upper-right corner to run it.
- View the project's log information and output results through IDEA's console.
Data in the people table: People [name=zhangsan, age=27] People [name=lisi, age=35] Batch Job execution completed.- Locate the
Sample project overview
Click java-oceanbase-springbatch to download the project code, which is a compressed file named java-oceanbase-springbatch.
After decompressing it, you will get a folder named java-oceanbase-springbatch. The directory structure is as follows:
│ pom.xml
│
├─.idea
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─oceanbase
│ │ │ └─example
│ │ │ └─batch
│ │ │ │──BatchApplication.java
│ │ │ │
│ │ │ ├─config
│ │ │ │ └─BatchConfig.java
│ │ │ │
│ │ │ ├─model
│ │ │ │ ├─People.java
│ │ │ │ └─PeopleDESC.java
│ │ │ │
│ │ │ ├─processor
│ │ │ │ └─AddPeopleDescProcessor.java
│ │ │ │
│ │ │ └─writer
│ │ │ ├─AddDescPeopleWriter.java
│ │ │ └─AddPeopleWriter.java
│ │ │
│ │ └─resources
│ │ └─application.properties
│ │
│ └─test
│ └─java
│ └─com
│ └─oceanbase
│ └─example
│ └─batch
│ ├─config
│ │ └─BatchConfigTest.java
│ │
│ ├─processor
│ │ └─AddPeopleDescProcessorTest.java
│ │
│ └─writer
│ ├─AddDescPeopleWriterTest.java
│ └─AddPeopleWriterTest.java
│
└─target
File description:
pom.xml: The configuration file for the Maven project, containing information such as project dependencies, plugins, and build settings..idea: A directory used by the IDE (Integrated Development Environment) to store project-related configuration information.src: Typically denotes the directory that holds the project's source code.main: The directory for storing the main source code and resource files.java: The directory for storing Java source code.com: The root directory for Java packages.oceanbase: The root directory for the project.example: The root directory for the project.batch: The main package name of the project.BatchApplication.java: The entry class of the application, containing the main method.config: The folder for configuration classes, containing the application's configuration classes.BatchConfig.java: The application's configuration class, used to configure some properties and behaviors of the application.model: The folder for model classes, containing the application's data model classes.People.java: A personnel data model class.PeopleDESC.java: A personnel DESC data model class.processor: The folder for processor classes, containing the application's processor classes.AddPeopleDescProcessor.java: A processor class for adding personnel DESC information.writer: The folder for writer classes, containing the application's writer classes.AddDescPeopleWriter.java: A writer class for writing personnel DESC information.AddPeopleWriter.java: A writer class for writing personnel information.resources: The resource folder, containing the application's configuration files and other static resource files.application.properties: The application's configuration file, used to configure application properties.test: The directory for storing test code and resource files.BatchConfigTest.java: The test class for the application's configuration class.AddPeopleDescProcessorTest.java: The test class for the add-personnel-desc processor.AddDescPeopleWriterTest.java: The test class for the writer that writes personnel DESC information.AddPeopleWriterTest.java: The test class for the writer that writes personnel information.target: The directory for storing compiled class files, JAR packages, and other files.
Introduction to pom.xml code
Note
If you just want to verify the example, use the default code without modification. You can also modify the pom.xml file according to your needs as described below.
The content of the pom.xml configuration file is as follows:
The file declaration statement.
Declares that this file is an XML file, using XML version
1.0and character encodingUTF-8.Code:
<?xml version="1.0" encoding="UTF-8"?>Configure the POM namespace and POM model version.
- Use
xmlnsto specify the POM namespace ashttp://maven.apache.org/POM/4.0.0. - Use
xmlns:xsito specify the XML namespace ashttp://www.w3.org/2001/XMLSchema-instance. - Use
xsi:schemaLocationto specify the POM namespace,http://maven.apache.org/POM/4.0.0, and the location of its XSD file,https://maven.apache.org/xsd/maven-4.0.0.xsd. - Use the
<modelVersion>element to specify the POM model version used by this POM file as4.0.0.
Code:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> </project>- Use
Configure parent information.
- Use
<groupId>to specify the parent identifier asorg.springframework.boot. - Use
<artifactId>to specify the parent dependency asspring-boot-starter-parent. - Use
<version>to specify the parent version as2.7.11. - Use
relativePathto indicate that the parent path is empty.
Code:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.11</version> <relativePath/> </parent>- Use
Configure basic information.
- Use
<groupId>to set the group ID tocom.oceanbase. - Use
<artifactId>to set the artifact ID tojava-oceanbase-springboot. - Use
<version>to specify the project version as0.0.1-SNAPSHOT. - Use
descriptionto introduce the project information asDemo project for Spring Batch.
Code:
<groupId>com.oceanbase</groupId> <artifactId>java-oceanbase-springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>java-oceanbase-springbatch</name> <description>Demo project for Spring Batch</description>- Use
Configure the Java version.
Specify the Java version used by the project as 1.8.
Sample code:
<properties> <java.version>1.8</java.version> </properties>Configure core dependencies.
Specify the dependency's organization as
org.springframework.bootand its name asspring-boot-starter. This dependency allows you to use the component dependencies supported by Spring Boot by default, including features for web, data processing, security, and testing.Sample code:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>Specify the dependency's organization as
org.springframework.bootand its name asspring-boot-starter-jdbc. This dependency enables you to use JDBC-related features provided by Spring Boot, such as connection pooling and data source configuration.Sample code:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>Specify the dependency's organization as
org.springframework.bootand its name asspring-boot-starter-test, with a scope oftest. This dependency allows you to use the testing frameworks and tools provided by Spring Boot, such as JUnit, Mockito, and Hamcrest.Sample code:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>Specify the dependency's organization as
com.oceanbase, its name asoceanbase-client, and its version as2.4.3. This dependency enables you to use client features provided by OceanBase, such as connection, query, and transaction.Sample code:
<dependency> <groupId>com.oceanbase</groupId> <artifactId>oceanbase-client</artifactId> <version>2.4.3</version> </dependency>Specify the dependency's organization as
org.springframework.bootand its name asspring-boot-starter-batch. This dependency enables you to use batch processing features provided by Spring Boot.Sample code:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency>Specify the dependency's organization as
org.springframework.bootand its name asspring-boot-starter-data-jpa. This dependency enables you to use the necessary dependencies and configurations for data access using JPA. Spring Boot Starter Data JPA is a Spring Boot starter.Sample code:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>Specify the dependency's organization as
org.apache.tomcatand its name astomcat-jdbc. This dependency enables you to use the JDBC connection pool feature provided by Tomcat, including connection pool configuration, connection acquisition and release, and connection management.Sample code:
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </dependency>Specify the dependency's test architecture as
junit, name asjunit, version as4.10, and scope astest. This dependency is used to add configuration for the JUnit unit test dependency.Sample code:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency>Specify the dependency's organization as
javax.activation, name asjavax.activation-api, and version as1.2.0. This dependency is used to introduce the Java Activation Framework (JAF) library.Sample code:
<dependency> <groupId>javax.activation</groupId> <artifactId>javax.activation-api</artifactId> <version>1.2.0</version> </dependency>Specify the dependency's organization as
jakarta.persistence, name asjakarta.persistence-api, and version as2.2.3. This dependency is used to add configuration for the Jakarta Persistence API. Sample code:<dependency> <groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> <version>2.2.3</version> </dependency>
Configure Maven plugins.
Specify the dependency's organization as
org.springframework.bootand name asspring-boot-maven-plugin. This plugin is used to package Spring Boot applications into executable JAR or WAR packages that can be run directly.Sample code:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Introduction to the application.properties file
The application.properties file is used to configure database connections and other related parameters, such as the database driver, connection URL, username, and password. It also includes configurations for JPA (Java Persistence API) and Spring Batch, as well as log level settings.
Database connection configuration.
- Use
spring.datasource.driverto specify the database driver ascom.oceanbase.jdbc.Driver, which is used to establish a connection with OceanBase Cloud. - Use
spring.datasource.urlto specify the URL for connecting to the database. - Use
spring.datasource.usernameto specify the username for connecting to the database. - Use
spring.datasource.passwordto specify the password for connecting to the database.
Sample code:
spring.datasource.driver-class-name=com.oceanbase.jdbc.Driver spring.datasource.url=jdbc:oceanbase://host:port/schema_name?characterEncoding=utf-8 spring.datasource.username=user_name spring.datasource.password=******- Use
JPA configuration.
- Use
spring.jpa.show-sqlto specify whether to display SQL statements in the logs. Setting it totruemeans SQL statements will be displayed. - Use
spring.jpa.hibernate.ddl-autoto specify the DDL operation behavior of Hibernate. Here it is set toupdate, meaning the database schema is automatically updated when the application starts.
Sample code:
spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update- Use
Spring Batch configuration:
Use the
spring.batch.job.enabledproperty to specify whether to enable Spring Batch jobs. Set this property tofalseto disable Spring Batch jobs.Sample code:
spring.batch.job.enabled=falseLogging configuration:
- Use the
logging.level.org.springframeworkproperty to set the log level for the Spring framework toINFO. - Use the
logging.level.com.exampleproperty to set the log level for application custom code toDEBUG.
Sample code:
logging.level.org.springframework=INFO logging.level.com.example=DEBUG- Use the
Introduction to the BatchApplication.java file
The BatchApplication.java file is the entry file for the Spring Boot application.
The code in the BatchApplication.java file mainly consists of the following parts:
References to other classes and interfaces.
Declare that this file contains the following interfaces and classes:
SpringApplicationclass: used to start a Spring Boot application.SpringBootApplicationannotation: used to mark this class as the entry point of the Spring Boot application.
Sample code:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;Definition of the
BatchApplicationclass.Use the
@SpringBootApplicationannotation to mark theBatchApplicationclass as the entry point of the Spring Boot application. Define a staticmainmethod in theBatchApplicationclass as the application's entry point. In this method, use theSpringApplication.runmethod to start the Spring Boot application. Also define a method namedrunBatchJobto run the batch processing job.Sample code:
@SpringBootApplication public class BatchApplication { public static void main(String[] args) { SpringApplication.run(BatchApplication.class, args); } public void runBatchJob() { } }
Introduction to the BatchConfig.java file
The BatchConfig.java file is used to configure components such as the steps, readers, processors, and writers for the batch processing job.
The code in the BatchConfig.java file mainly consists of the following parts:
References to other classes and interfaces.
Declare that this file contains the following interfaces and classes:
Peopleclass: used to store the personnel information read from the database.PeopleDESCclass: used to store the descriptive information after converting or processing the personnel information.AddPeopleDescProcessorclass: implements theItemProcessorinterface and converts the readPeopleobjects intoPeopleDESCobjects.AddDescPeopleWriterclass: implements theItemWriterinterface and writes thePeopleDESCobjects to the target location.Jobinterface: represents a batch processing job.Stepinterface: represents a step in a job.EnableBatchProcessingannotation: a Spring Batch configuration annotation used to enable and configure the Spring Batch processing feature.JobBuilderFactoryclass: used to create and configure jobs.StepBuilderFactoryclass: used to create and configure steps.RunIdIncrementerclass: an auto-incrementer for the run ID (Run ID) of Spring Batch, used to increase the run ID each time a job is executed.ItemProcessorinterface: used to process or convert the read items.ItemReaderinterface: used to read items from a data source.ItemWriterinterface: used to write the processed or converted items to a specified target location.JdbcCursorItemReaderclass: used to read data from the database and return a cursor result set.Autowiredannotation: used for dependency injection.Beanannotation: used to create and configure beans.ComponentScanannotation: used to specify the package or class for component scanning.Configurationannotation: used to mark a class as a configuration class.EnableAutoConfigurationannotation: used to enable automatic configuration of Spring Boot.SpringBootApplicationannotation: used to mark this class as the entry point of a Spring Boot application.DataSourceinterface: used to represent a database connection.
Sample code:
import com.oceanbase.example.batch.model.People; import com.oceanbase.example.batch.model.PeopleDESC; import com.oceanbase.example.batch.processor.AddPeopleDescProcessor; import com.oceanbase.example.batch.writer.AddDescPeopleWriter; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.launch.support.RunIdIncrementer; import org.springframework.batch.item.ItemProcessor; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemWriter; import org.springframework.batch.item.database.JdbcCursorItemReader; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.BeanPropertyRowMapper; import javax.sql.DataSource;Define the
BatchConfigclass.This is a simple Spring Batch job. It defines how data is read, processed, and written, and encapsulates these steps into a job. Using Spring Batch annotations and auto-configuration, you can create corresponding component instances through the various
@Beanmethods in the configuration class, and use these components instep1to complete data reading, processing, and writing.- The
@Configurationannotation indicates that this class is a configuration class. - The
@EnableBatchProcessingannotation enables the Spring Batch processing feature. It automatically creates necessarybeans, such asJobRepositoryandJobLauncher. - The
@SpringBootApplicationannotation is used for the main class of a Spring Boot application and serves as the startup point of the application. - The
@ComponentScanannotation specifies the package for component scanning, telling Spring to scan and register all components in this package and its subpackages. - The
@EnableAutoConfigurationannotation enables the infrastructure for automatic configuration of the Spring Boot application.
Sample code:
@Configuration @EnableBatchProcessing @SpringBootApplication @ComponentScan("com.oceanbase.example.batch.writer") @EnableAutoConfiguration public class BatchConfig { }Define the
@Autowiredannotation.Use the
@Autowiredannotation to injectJobBuilderFactory,StepBuilderFactory, andDataSourceinto the member variables of theBatchConfigclass.JobBuilderFactoryis a factory class for creating and configuring jobs,StepBuilderFactoryis a factory class for creating and configuring steps, andDataSourceis an interface for obtaining a database connection.Sample code:
@Autowired private JobBuilderFactory jobBuilderFactory; @Autowired private StepBuilderFactory stepBuilderFactory; @Autowired private DataSource dataSource;Define the
@Beanannotation.The
@Beanannotation is used to define several methods for creating the reader, processor, writer, step, and job of the batch processing job.The
peopleReadermethod creates an instance of theItemReadercomponent. This component usesJdbcCursorItemReaderto readPeopleobject data from the database. Set the data sourcedataSource, configure theRowMapperto map database rows toPeopleobjects, and set the SQL query statement toSELECT * FROM people.The
addPeopleDescProcessormethod creates an instance of theItemProcessorcomponent. This component usesAddPeopleDescProcessorto processPeopleobjects and returns them converted intoPeopleDESCobjects.The
addDescPeopleWritermethod creates an instance of theItemWritercomponent. This component usesAddDescPeopleWriterto writePeopleDESCobjects to the target location.The
step1method creates an instance of theStepcomponent with the namestep1. It obtains the step builder throughstepBuilderFactory.get, sets the reader as theItemReadercomponent, the processor as theItemProcessorcomponent, the writer as theItemWritercomponent, sets thechunksize to10, and finally callsbuildto construct and return the configuredStep.The
importJobmethod creates an instance of theJobcomponent with the nameimportJob. It obtains the job builder throughjobBuilderFactory.get, sets the incrementer asRunIdIncrementer, sets the initial step of the job'sflowasStep, and finally callsbuildto construct and return the configuredJob.Sample code:
@Bean public ItemReader<People> peopleReader() { JdbcCursorItemReader<People> reader = new JdbcCursorItemReader<>(); reader.setDataSource((javax.sql.DataSource) dataSource); reader.setRowMapper(new BeanPropertyRowMapper<>(People.class)); reader.setSql("SELECT * FROM people"); return reader; } @Bean public ItemProcessor<People, PeopleDESC> addPeopleDescProcessor() { return new AddPeopleDescProcessor(); } @Bean public ItemWriter<PeopleDESC> addDescPeopleWriter() { return new AddDescPeopleWriter(); } @Bean public Step step1(ItemReader<People> reader, ItemProcessor<People, PeopleDESC> processor, ItemWriter<PeopleDESC> writer) { return stepBuilderFactory.get("step1") .<People, PeopleDESC>chunk(10) .reader(reader) .processor(processor) .writer(writer) .build(); } @Bean public Job importJob(Step step1) { return jobBuilderFactory.get("importJob") .incrementer(new RunIdIncrementer()) .flow(step1) .end() .build(); }
- The
Introduction to the People.java file
The People.java file defines a data model for the People class, which represents a person's information. The class contains two private member variables, name and age, along with their corresponding getter and setter methods. The toString method is overridden to print the object's information. Here, name represents the person's name, and age represents the person's age. These attributes can be accessed and modified using the getter and setter methods.
This class serves as a way to store and pass data for input and output in the batch processing program. In batch processing read and write operations, People objects are used to store data, set data using the setter method, and retrieve data using the getter method.
Sample code:
public class People {
private String name;
private int age;
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "People [name=" + name + ", age=" + age + "]";
}
// Getters and setters
}
Introduction to the PeopleDESC.java file
The PeopleDESC.java file defines a data model for the PeopleDESC class, which represents a person's information. The PeopleDESC class has four attributes: name, age, desc, and id, representing the person's name, age, description, and identifier, respectively. The class includes corresponding getter and setter methods to access and set the attribute values. The toString method is overridden to return a string representation of the class, including the name, age, and description.
Similar to the People class, the PeopleDESC class is also used to store and pass data in the input and output of the batch processing program.
Sample code:
public class PeopleDESC {
private String name;
private int age;
private String desc;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "PeopleDESC [name=" + name + ", age=" + age + ", desc=" + desc + "]";
}
}
Introduction to the AddPeopleDescProcessor.java file
The AddPeopleDescProcessor.java file defines a class named AddPeopleDescProcessor, which implements the ItemProcessor interface. It is used to convert People objects into PeopleDESC objects.
The code in the AddPeopleDescProcessor.java file mainly consists of the following parts:
References to other classes and interfaces.
Declare that this file contains the following interfaces and classes:
Peopleclass: used to store personnel information read from the database.PeopleDESCclass: used to store descriptive information after converting or processing personnel information.ItemProcessorinterface: used to process or convert the read items.
Code:
import com.oceanbase.example.batch.model.People; import com.oceanbase.example.batch.model.PeopleDESC; import org.springframework.batch.item.ItemProcessor;Definition of the
AddPeopleDescProcessorclass.The
AddPeopleDescProcessorclass, which implements theItemProcessorinterface, is used to convertPeopleobjects intoPeopleDESCobjects, implementing the processing logic for input data during batch processing.In the
processmethod of this class, first create aPeopleDESCobject nameddesc. Then, obtain the properties (nameandage) of thePeopleobject through theitemparameter and set these properties to thedescobject. Also, assign a value to thedescproperty of thedescobject. The assignment logic generates a descriptive string based on the properties of thePeopleobject. Finally, return the processedPeopleDESCobject.Code:
public class AddPeopleDescProcessor implements ItemProcessor<People, PeopleDESC> { @Override public PeopleDESC process(People item) throws Exception { PeopleDESC desc = new PeopleDESC(); desc.setName(item.getName()); desc.setAge(item.getAge()); desc.setDesc("This is " + item.getName() + " with age " + item.getAge()); return desc; } }
Introduction to the AddDescPeopleWriter.java file
The AddDescPeopleWriter.java file defines AddDescPeopleWriter, which implements ItemWriter<PeopleDESC> and writes PeopleDESC objects to the database.
The code in the AddDescPeopleWriter.java file mainly consists of the following parts:
References to other classes and interfaces.
Declare that this file contains the following interfaces and classes:
PeopleDESCclass: used to store descriptive information after converting or processing personnel information.ItemWriterinterface: used to write processed or converted items to a specified target location.Autowiredannotation: used for dependency injection.JdbcTemplateclass: provides methods for executing SQL statements.Listinterface: used to operate on query result sets.
Code:
import com.oceanbase.example.batch.model.PeopleDESC; import org.springframework.batch.item.ItemWriter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List;Definition of the
AddDescPeopleWriterclass.Use the
@Autowiredannotation to automatically inject aJdbcTemplateinstance, which is then used to perform database operations when writing data.Sample code:
@Autowired private JdbcTemplate jdbcTemplate;In the
writemethod, iterate through the passedList<? extends PeopleDESC>and retrieve eachPeopleDESCobject in sequence. First, execute the SQL statementDROP TABLE people_descto drop any existing table namedpeople_desc. Then, execute the SQL statementCREATE TABLE people_desc (id INT PRIMARY KEY, name VARCHAR2(255), age INT, description VARCHAR2(255))to create a table namedpeople_descwith four columns:id,name,age, anddescription. Next, use the SQL statementINSERT INTO people_desc (id, name, age, description) VALUES (?, ?, ?, ?)to insert the attribute values of eachPeopleDESCobject into thepeople_desctable.Sample code:
@Override public void write(List<? extends PeopleDESC> items) throws Exception { // Delete any existing tables first. jdbcTemplate.execute("DROP TABLE people_desc"); // CREATE TABLE Statement String createTableSql = "CREATE TABLE people_desc (id INT PRIMARY KEY, name VARCHAR2(255), age INT, description VARCHAR2(255))"; jdbcTemplate.execute(createTableSql); for (PeopleDESC item : items) { String sql = "INSERT INTO people_desc (id, name, age, description) VALUES (?, ?, ?, ?)"; jdbcTemplate.update(sql, item.getId(), item.getName(), item.getAge(), item.getDesc()); } }
Introduction to the AddPeopleWriter.java file
The AddPeopleWriter.java file defines AddPeopleWriter, which implements ItemWriter<People> and writes People objects to the database.
The code in the AddPeopleWriter.java file mainly consists of the following parts:
References to other classes and interfaces.
Declare that this file contains the following interfaces and classes:
- The
Peopleclass: used to store personnel information read from the database. - The
ItemWriterinterface: used to write processed or transformed items to a specified target location. - The
Autowiredannotation: used for dependency injection. - The
JdbcTemplateclass: provides methods for executing SQL statements. - The
Componentannotation: used to mark this class as a Spring component. - The
Listinterface: used to manipulate query result sets.
Sample code:
import com.oceanbase.example.batch.model.People; import org.springframework.batch.item.ItemWriter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; import java.util.List;- The
Definition of the
AddPeopleWriterclass.Use the
@Autowiredannotation to automatically inject aJdbcTemplateinstance, which is then used to perform database operations when writing data.Sample code:
@Autowired private JdbcTemplate jdbcTemplate;In the
writemethod, iterate through the passedList<? extends People>and retrieve eachPeopleobject in sequence. First, execute the SQL statementDROP TABLE peopleto drop any existing table namedpeople. Then, execute the SQL statementCREATE TABLE people (name VARCHAR2(255), age INT)to create a table namedpeoplewith two columns:nameandage. Next, use the SQL statementINSERT INTO people (name, age) VALUES (?, ?)to insert the attribute values of eachPeopleobject into thepeopletable.Sample code:
@Override public void write(List<? extends People> items) throws Exception { // Delete any existing tables first. jdbcTemplate.execute("DROP TABLE people"); // CREATE TABLE Statement String createTableSql = "CREATE TABLE people (name VARCHAR2(255), age INT)"; jdbcTemplate.execute(createTableSql); for (People item : items) { String sql = "INSERT INTO people (name, age) VALUES (?, ?)"; jdbcTemplate.update(sql, item.getName(), item.getAge()); } }
Introduction to the BatchConfigTest.java file
The BatchConfigTest.java file is a test class using JUnit to test job configurations for Spring Batch.
The code in the BatchConfigTest.java file mainly consists of the following parts:
References to other classes and interfaces.
Declares that this file contains the following interfaces and classes:
Assertclass: used to assert test results.Testannotation: used to mark test methods.RunWithannotation: used to specify the test runner.Jobinterface: represents a batch processing job.JobExecutionclass: used to represent the execution of a batch processing job.JobParametersclass: used to represent the parameters of a batch processing job.JobParametersBuilderclass: used to build the parameters for a batch processing job.JobLauncherinterface: used to start a batch processing job.Autowiredannotation: used for dependency injection.SpringBootTestannotation: used to specify the test class as a Spring Boot test.SpringRunnerclass: used to specify the test runner as SpringRunner.
Code:
import org.junit.Assert; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.batch.core.BatchStatus;Definition of the
BatchConfigTestclass.By using the
SpringBootTestannotation and theSpringRunnerrunner, you can perform integration testing for Spring Boot. In thetestJobmethod, use theJobLauncherTestUtilsutility class to start a batch processing job and use assertions to verify the job's execution status.Use the
@Autowiredannotation to automatically inject an instance ofJobLauncherTestUtils.Code:
@Autowired private JobLauncherTestUtils jobLauncherTestUtils;Use the
@Testannotation to mark thetestJobmethod as a test method. In this method, first create aJobParametersobject, then use thejobLauncherTestUtils.launchJobmethod to start the batch processing job, and use theAssert.assertEqualsmethod to assert that the job's execution status isCOMPLETED.Code:
@Test public void testJob() throws Exception { JobParameters jobParameters = new JobParametersBuilder() .addString("jobParam", "paramValue") .toJobParameters(); JobExecution jobExecution = jobLauncherTestUtils.launchJob(jobParameters); Assert.assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); }Use the
@Autowiredannotation to automatically inject an instance ofJobLauncher.Code:
@Autowired private JobLauncher jobLauncher;Use the
@Autowiredannotation to automatically inject an instance ofJob.Code:
@Autowired private Job job;Defined an inner class named
JobLauncherTestUtilsto assist in launching the batch processing job. Within this class, alaunchJobmethod is defined to start the batch processing job. In this method, thejobLauncher.runmethod is used to launch the job and return its execution result.Sample code:
private class JobLauncherTestUtils { public JobExecution launchJob(JobParameters jobParameters) throws Exception { return jobLauncher.run(job, jobParameters); } }
Introduction to the AddPeopleDescProcessorTest.java file
The AddPeopleDescProcessorTest.java file is a test class that uses JUnit to test job configurations in Spring Batch.
The code in the AddPeopleDescProcessorTest.java file mainly includes the following parts:
References to other classes and interfaces.
Declares that this file contains the following interfaces and classes:
Peopleclass: used to store personnel information read from the database.PeopleDESCclass: used to store description information after converting or processing personnel information.Testannotation: used to mark test methods.RunWithannotation: used to specify the test runner.Autowiredannotation: used for dependency injection.SpringBootTestannotation: used to specify the test class as a Spring Boot test.SpringRunnerclass: used to specify the test runner as SpringRunner.
Sample code:
import com.oceanbase.example.batch.model.People; import com.oceanbase.example.batch.model.PeopleDESC; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;Definition of the
AddPeopleDescProcessorTestclass.Performs integration testing for Spring Boot by using the
SpringBootTestannotation and theSpringRunnerrunner.Automatically injects an instance of
AddPeopleDescProcessorusing the@Autowiredannotation.Sample code:
@Autowired private AddPeopleDescProcessor processor;Marks the
testProcessmethod as a test method using the@Testannotation. In this method, first create aPeopleobject, then process the object using theprocessor.processmethod, and assign the result to aPeopleDESCobject.Sample code:
@Test public void testProcess() throws Exception { People people = new People(); PeopleDESC desc = processor.process(people); }
Introduction to the AddDescPeopleWriterTest.java file
The AddDescPeopleWriterTest.java file is a test class that uses JUnit to test the write logic of AddDescPeopleWriter.
The code in the AddDescPeopleWriterTest.java file mainly includes the following parts:
References to other classes and interfaces.
Declares that this file contains the following interfaces and classes:
PeopleDESCclass: used to store the description information after converting or processing personnel information.Assertclass: used to assert test results.Testannotation: used to mark a test method.RunWithannotation: used to specify the test runner.Autowiredannotation: used for dependency injection.SpringBootTestannotation: used to specify that the test class is a Spring Boot test.JdbcTemplateclass: provides methods for executing SQL statements.SpringRunnerclass: used to specify the test runner as SpringRunner.ArrayListclass, used to create an empty list.Listinterface: used to operate on query result sets.
Sample code:
import com.oceanbase.example.batch.model.PeopleDESC; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.List;Define the
AddDescPeopleWriterTestclass.Use the
SpringBootTestannotation and theSpringRunnerrunner to perform integration testing for Spring Boot.Use
@Autowiredto inject instances. Use the@Autowiredannotation to automatically inject instances ofAddPeopleDescProcessorandJdbcTemplate.Sample code:
@Autowired private AddDescPeopleWriter writer; @Autowired private JdbcTemplate jdbcTemplate;Use
@Testto test data insertion and output. Use the@Testannotation to mark thetestWritemethod as a test method. In this method, first create an emptypeopleDescListlist and add twoPeopleDESCobjects to it. Then use thewriter.writemethod to write the data in the list to the database. Next, usejdbcTemplateto execute a query statement, retrieve data from thepeople_desctable, and use assertion statements to verify the correctness of the data. Finally, output the query results to the console and print a message indicating the job has completed.Insert data into the
people_desctable. First, create an emptyPeopleDESCobject list namedpeopleDescList. Then create twoPeopleDESCobjects nameddesc1anddesc2, and set their attribute values respectively. Adddesc1anddesc2to thepeopleDescListlist. Next, call thewritemethod ofwriterto write the objects inpeopleDescListto thepeople_desctable in the database. Then useJdbcTemplateto execute a query statementSELECT COUNT(*) FROM people_descto get the number of records in thepeople_desctable and assign the result to the variablecount. Finally, use theAssert.assertEqualsmethod to assert whether the value ofcountequals2.Sample code:
List<PeopleDESC> peopleDescList = new ArrayList<>(); PeopleDESC desc1 = new PeopleDESC(); desc1.setId(1); desc1.setName("John"); desc1.setAge(25); desc1.setDesc("This is John with age 25"); peopleDescList.add(desc1); PeopleDESC desc2 = new PeopleDESC(); desc2.setId(2); desc2.setName("Alice"); desc2.setAge(30); desc2.setDesc("This is Alice with age 30"); peopleDescList.add(desc2); writer.write(peopleDescList); String selectSql = "SELECT COUNT(*) FROM people_desc"; int count = jdbcTemplate.queryForObject(selectSql, Integer.class); Assert.assertEquals(2, count);Output the data in the
people_desctable. First, useJdbcTemplateto execute a query statementSELECT * FROM people_descand process the query result using alambdaexpression. In thelambdaexpression, use methods likers.getIntandrs.getStringto retrieve field values from the query result set and set them into a newly createdPeopleDESCobject. Add each newly createdPeopleDESCobject to a result list namedresultDesc. Then print a prompt lineData in the people_desc table:. Use aforloop to iterate through eachPeopleDESCobject in theresultDesclist and print its content usingSystem.out.println. Finally, print a message indicating the job has completed.Sample code:
List<PeopleDESC> resultDesc = jdbcTemplate.query("SELECT * FROM people_desc", (rs, rowNum) -> { PeopleDESC desc = new PeopleDESC(); desc.setId(rs.getInt("id")); desc.setName(rs.getString("name")); desc.setAge(rs.getInt("age")); desc.setDesc(rs.getString("description")); return desc; }); System.out.println("Data in the people_desc table:"); for (PeopleDESC desc : resultDesc) { System.out.println(desc); } // Output information after the job is completed System.out.println("Batch Job execution completed.");
Introduction to the AddPeopleWriterTest.java file
The AddPeopleWriterTest.java file is a test class that uses JUnit to test the write logic of AddPeopleWriter.
The code in the AddPeopleWriterTest.java file mainly consists of the following parts:
References to other classes and interfaces.
Declare that this file contains the following interfaces and classes:
Peopleclass: used to store personnel information read from the database.Testannotation: used to mark test methods.RunWithannotation: used to specify the test runner.Autowiredannotation: used for dependency injection.SpringBootApplicationannotation: used to mark this class as the entry point of a Spring Boot application.SpringBootTestannotation: used to specify the test class as a Spring Boot test.ComponentScanannotation: used to specify the package or class for component scanning.JdbcTemplateclass: provides methods for executing SQL statements.SpringRunnerclass: used to specify the test runner asSpringRunner.ArrayListclass, used to create an empty list.Listinterface: used to operate on query result sets.
Code:
import com.oceanbase.example.batch.model.People; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.ComponentScan; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.List;Definition of the
AddPeopleWriterTestclass.Use the
SpringBootTestannotation and theSpringRunnerrunner to perform integration testing with Spring Boot, and use the@ComponentScanannotation to specify the package path to scan.Inject instances using
@Autowired. Use the@Autowiredannotation to automatically inject instances ofaddPeopleWriterandJdbcTemplate.Code:
@Autowired private AddPeopleWriter addPeopleWriter; @Autowired private JdbcTemplate jdbcTemplate;Use
@Testto test data insertion and output.Insert data into the
peopletable. First, create an emptyPeopleobject list namedpeopleList. Then, create twoPeopleobjects namedperson1andperson2, and set their name and age properties. Next, add these twoPeopleobjects to thepeopleListlist. After that, call thewritemethod ofaddPeopleWriter, passingpeopleListas a parameter to write thesePeopleobjects to the database.Code:
List<People> peopleList = new ArrayList<>(); People person1 = new People(); person1.setName("zhangsan"); person1.setAge(27); peopleList.add(person1); People person2 = new People(); person2.setName("lisi"); person2.setAge(35); peopleList.add(person2); addPeopleWriter.write(peopleList);Output the data from the
peopletable. First, useJdbcTemplateto execute a query statementSELECT * FROM peopleand process the query results using alambdaexpression. In thelambdaexpression, use thers.getStringandrs.getIntmethods to retrieve field values from the query result set and set them into a newly createdPeopleobject. Add each newly createdPeopleobject to a result list namedresult. Then, print a prompt lineData in the people table:. Next, use aforloop to iterate through eachPeopleobject in theresultlist and print the content of each object usingSystem.out.println. Finally, print a message indicating the job has completed.Code:
List<People> result = jdbcTemplate.query("SELECT * FROM people", (rs, rowNum) -> { People person = new People(); person.setName(rs.getString("name")); person.setAge(rs.getInt("age")); return person; }); System.out.println("Data in the people table:"); for (People person : result) { System.out.println(person); } // Output information after the job is completed System.out.println("Batch Job execution completed.");
Complete sample code
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.oceanbase</groupId>
<artifactId>java-oceanbase-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>java-oceanbase-springbatch</name>
<description>Demo project for Spring Batch</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.oceanbase</groupId>
<artifactId>oceanbase-client</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
#configuration database
spring.datasource.driver-class-name=com.oceanbase.jdbc.Driver
spring.datasource.url=jdbc:oceanbase://host:port/schema_name?characterEncoding=utf-8
spring.datasource.username=user_name
spring.datasource.password=
# JPA
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
# Spring Batch
spring.batch.job.enabled=false
#
logging.level.org.springframework=INFO
logging.level.com.example=DEBUG
package com.oceanbase.example.batch;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BatchApplication {
public static void main(String[] args) {
SpringApplication.run(BatchApplication.class, args);
}
public void runBatchJob() {
}
}
package com.oceanbase.example.batch.config;
import com.oceanbase.example.batch.model.People;
import com.oceanbase.example.batch.model.PeopleDESC;
import com.oceanbase.example.batch.processor.AddPeopleDescProcessor;
import com.oceanbase.example.batch.writer.AddDescPeopleWriter;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import javax.sql.DataSource;
//import javax.activation.DataSource;
@Configuration
@EnableBatchProcessing
@SpringBootApplication
@ComponentScan("com.oceanbase.example.batch.writer")
@EnableAutoConfiguration
public class BatchConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private DataSource dataSource; // Use the default dataSource provided by Spring Boot's auto-configuration.
@Bean
public ItemReader<People> peopleReader() {
JdbcCursorItemReader<People> reader = new JdbcCursorItemReader<>();
reader.setDataSource((javax.sql.DataSource) dataSource);
reader.setRowMapper(new BeanPropertyRowMapper<>(People.class));
reader.setSql("SELECT * FROM people");
return reader;
}
@Bean
public ItemProcessor<People, PeopleDESC> addPeopleDescProcessor() {
return new AddPeopleDescProcessor();
}
@Bean
public ItemWriter<PeopleDESC> addDescPeopleWriter() {
return new AddDescPeopleWriter();
}
@Bean
public Step step1(ItemReader<People> reader, ItemProcessor<People, PeopleDESC> processor,
ItemWriter<PeopleDESC> writer) {
return stepBuilderFactory.get("step1")
.<People, PeopleDESC>chunk(10)
.reader(reader)
.processor(processor)
.writer(writer)
.build();
}
@Bean
public Job importJob(Step step1) {
return jobBuilderFactory.get("importJob")
.incrementer(new RunIdIncrementer())
.flow(step1)
.end()
.build();
}
}
package com.oceanbase.example.batch.model;
public class People {
private String name;
private int age;
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "People [name=" + name + ", age=" + age + "]";
}
// Getters and setters
}
package com.oceanbase.example.batch.model;
public class PeopleDESC {
private String name;
private int age;
private String desc;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "PeopleDESC [name=" + name + ", age=" + age + ", desc=" + desc + "]";
}
}
package com.oceanbase.example.batch.processor;
import com.oceanbase.example.batch.model.People;
import com.oceanbase.example.batch.model.PeopleDESC;
import org.springframework.batch.item.ItemProcessor;
public class AddPeopleDescProcessor implements ItemProcessor<People, PeopleDESC> {
@Override
public PeopleDESC process(People item) throws Exception {
PeopleDESC desc = new PeopleDESC();
desc.setName(item.getName());
desc.setAge(item.getAge());
desc.setDesc("This is " + item.getName() + " with age " + item.getAge());
return desc;
}
}
package com.oceanbase.example.batch.writer;
import com.oceanbase.example.batch.model.PeopleDESC;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class AddDescPeopleWriter implements ItemWriter<PeopleDESC> {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void write(List<? extends PeopleDESC> items) throws Exception {
// Delete any existing tables first.
jdbcTemplate.execute("DROP TABLE people_desc");
// CREATE TABLE Statement
String createTableSql = "CREATE TABLE people_desc (id INT PRIMARY KEY, name VARCHAR2(255), age INT, description VARCHAR2(255))";
jdbcTemplate.execute(createTableSql);
for (PeopleDESC item : items) {
String sql = "INSERT INTO people_desc (id, name, age, description) VALUES (?, ?, ?, ?)";
jdbcTemplate.update(sql, item.getId(), item.getName(), item.getAge(), item.getDesc());
}
}
}
package com.oceanbase.example.batch.writer;
import com.oceanbase.example.batch.model.People;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class AddPeopleWriter implements ItemWriter<People> {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void write(List<? extends People> items) throws Exception {
// Delete any existing tables first.
jdbcTemplate.execute("DROP TABLE people");
// CREATE TABLE Statement
String createTableSql = "CREATE TABLE people (name VARCHAR2(255), age INT)";
jdbcTemplate.execute(createTableSql);
for (People item : items) {
String sql = "INSERT INTO people (name, age) VALUES (?, ?)";
jdbcTemplate.update(sql, item.getName(), item.getAge());
}
}
}
package com.oceanbase.example.batch.config;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.batch.core.BatchStatus;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BatchConfigTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void testJob() throws Exception {
JobParameters jobParameters = new JobParametersBuilder()
.addString("jobParam", "paramValue")
.toJobParameters();
JobExecution jobExecution = jobLauncherTestUtils.launchJob(jobParameters);
Assert.assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
}
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
private class JobLauncherTestUtils {
public JobExecution launchJob(JobParameters jobParameters) throws Exception {
return jobLauncher.run(job, jobParameters);
}
}
}
package com.oceanbase.example.batch.processor;
import com.oceanbase.example.batch.model.People;
import com.oceanbase.example.batch.model.PeopleDESC;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AddPeopleDescProcessorTest {
@Autowired
private AddPeopleDescProcessor processor;
@Test
public void testProcess() throws Exception {
People people = new People();
// people.setName("John");
// people.setAge(25);
PeopleDESC desc = processor.process(people);
// Assert.assertEquals("John", desc.getName());
// Assert.assertEquals(25, desc.getAge());
// Assert.assertEquals("This is John with age 25", desc.getDesc());
}
}
package com.oceanbase.example.batch.writer;
import com.oceanbase.example.batch.model.PeopleDESC;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AddDescPeopleWriterTest {
@Autowired
private AddDescPeopleWriter writer;
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testWrite() throws Exception {
// Insert data into the people_desc table.
List<PeopleDESC> peopleDescList = new ArrayList<>();
PeopleDESC desc1 = new PeopleDESC();
desc1.setId(1);
desc1.setName("John");
desc1.setAge(25);
desc1.setDesc("This is John with age 25");
peopleDescList.add(desc1);
PeopleDESC desc2 = new PeopleDESC();
desc2.setId(2);
desc2.setName("Alice");
desc2.setAge(30);
desc2.setDesc("This is Alice with age 30");
peopleDescList.add(desc2);
writer.write(peopleDescList);
String selectSql = "SELECT COUNT(*) FROM people_desc";
int count = jdbcTemplate.queryForObject(selectSql, Integer.class);
Assert.assertEquals(2, count);
// Output the data in the people_desc table.
List<PeopleDESC> resultDesc = jdbcTemplate.query("SELECT * FROM people_desc", (rs, rowNum) -> {
PeopleDESC desc = new PeopleDESC();
desc.setId(rs.getInt("id"));
desc.setName(rs.getString("name"));
desc.setAge(rs.getInt("age"));
desc.setDesc(rs.getString("description"));
return desc;
});
System.out.println("Data in the people_desc table:");
for (PeopleDESC desc : resultDesc) {
System.out.println(desc);
}
// Output information after the job is completed
System.out.println("Batch Job execution completed.");
}
}
package com.oceanbase.example.batch.writer;
import com.oceanbase.example.batch.model.People;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@SpringBootApplication
@ComponentScan("com.oceanbase.example.batch.writer")
public class AddPeopleWriterTest {
@Autowired
private AddPeopleWriter addPeopleWriter;
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testWrite() throws Exception {
// Insert data into the people table.
List<People> peopleList = new ArrayList<>();
People person1 = new People();
person1.setName("zhangsan");
person1.setAge(27);
peopleList.add(person1);
People person2 = new People();
person2.setName("lisi");
person2.setAge(35);
peopleList.add(person2);
addPeopleWriter.write(peopleList);
// Query and output the results
List<People> result = jdbcTemplate.query("SELECT * FROM people", (rs, rowNum) -> {
People person = new People();
person.setName(rs.getString("name"));
person.setAge(rs.getInt("age"));
return person;
});
System.out.println("Data in the people table:");
for (People person : result) {
System.out.println(person);
}
// Output information after the job is completed
System.out.println("Batch Job execution completed.");
}
}
References
For more information about OceanBase Connector/J, see OceanBase JDBC Driver