Detab poms and source
This commit is contained in:
parent
5992da45b3
commit
fc8fc09804
324
README.md
324
README.md
@ -54,12 +54,12 @@ In a project directory of your choosing, create the following subdirectory struc
|
|||||||
```xml
|
```xml
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?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"
|
<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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>gs-batch-processing</artifactId>
|
<artifactId>gs-batch-processing</artifactId>
|
||||||
<version>0.1.0</version>
|
<version>0.1.0</version>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
@ -67,7 +67,7 @@ In a project directory of your choosing, create the following subdirectory struc
|
|||||||
<version>0.5.0.BUILD-SNAPSHOT</version>
|
<version>0.5.0.BUILD-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-batch</artifactId>
|
<artifactId>spring-boot-starter-batch</artifactId>
|
||||||
@ -76,27 +76,24 @@ In a project directory of your choosing, create the following subdirectory struc
|
|||||||
<groupId>org.hsqldb</groupId>
|
<groupId>org.hsqldb</groupId>
|
||||||
<artifactId>hsqldb</artifactId>
|
<artifactId>hsqldb</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<url>http://repo.springsource.org/libs-snapshot</url>
|
||||||
|
<snapshots><enabled>true</enabled></snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<pluginRepositories>
|
||||||
|
<pluginRepository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<url>http://repo.springsource.org/libs-snapshot</url>
|
||||||
|
<snapshots><enabled>true</enabled></snapshots>
|
||||||
|
</pluginRepository>
|
||||||
|
</pluginRepositories>
|
||||||
|
|
||||||
<repositories>
|
|
||||||
<repository>
|
|
||||||
<id>spring-snapshots</id>
|
|
||||||
<url>http://repo.springsource.org/libs-snapshot</url>
|
|
||||||
<snapshots><enabled>true</enabled></snapshots>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
|
||||||
<id>spring-milestones</id>
|
|
||||||
<url>http://repo.springsource.org/libs-milestone</url>
|
|
||||||
<snapshots><enabled>true</enabled></snapshots>
|
|
||||||
</repository>
|
|
||||||
</repositories>
|
|
||||||
<pluginRepositories>
|
|
||||||
<pluginRepository>
|
|
||||||
<id>spring-snapshots</id>
|
|
||||||
<url>http://repo.springsource.org/libs-snapshot</url>
|
|
||||||
<snapshots><enabled>true</enabled></snapshots>
|
|
||||||
</pluginRepository>
|
|
||||||
</pluginRepositories>
|
|
||||||
</project>
|
</project>
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -128,9 +125,9 @@ Next, you write a SQL script to create a table to store the data.
|
|||||||
DROP TABLE people IF EXISTS;
|
DROP TABLE people IF EXISTS;
|
||||||
|
|
||||||
CREATE TABLE people (
|
CREATE TABLE people (
|
||||||
person_id BIGINT IDENTITY NOT NULL PRIMARY KEY,
|
person_id BIGINT IDENTITY NOT NULL PRIMARY KEY,
|
||||||
first_name VARCHAR(20),
|
first_name VARCHAR(20),
|
||||||
last_name VARCHAR(20)
|
last_name VARCHAR(20)
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -149,19 +146,19 @@ package hello;
|
|||||||
public class Person {
|
public class Person {
|
||||||
private String lastName;
|
private String lastName;
|
||||||
private String firstName;
|
private String firstName;
|
||||||
|
|
||||||
public Person() {
|
public Person() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Person(String firstName, String lastName) {
|
public Person(String firstName, String lastName) {
|
||||||
this.firstName = firstName;
|
this.firstName = firstName;
|
||||||
this.lastName = lastName;
|
this.lastName = lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFirstName(String firstName) {
|
public void setFirstName(String firstName) {
|
||||||
this.firstName = firstName;
|
this.firstName = firstName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFirstName() {
|
public String getFirstName() {
|
||||||
return firstName;
|
return firstName;
|
||||||
@ -172,13 +169,14 @@ public class Person {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setLastName(String lastName) {
|
public void setLastName(String lastName) {
|
||||||
this.lastName = lastName;
|
this.lastName = lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "firstName: " + firstName + ", lastName: " + lastName;
|
return "firstName: " + firstName + ", lastName: " + lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -196,17 +194,19 @@ package hello;
|
|||||||
import org.springframework.batch.item.ItemProcessor;
|
import org.springframework.batch.item.ItemProcessor;
|
||||||
|
|
||||||
public class PersonItemProcessor implements ItemProcessor<Person, Person> {
|
public class PersonItemProcessor implements ItemProcessor<Person, Person> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Person process(final Person person) throws Exception {
|
public Person process(final Person person) throws Exception {
|
||||||
final String firstName = person.getFirstName().toUpperCase();
|
final String firstName = person.getFirstName().toUpperCase();
|
||||||
final String lastName = person.getLastName().toUpperCase();
|
final String lastName = person.getLastName().toUpperCase();
|
||||||
|
|
||||||
final Person transformedPerson = new Person(firstName, lastName);
|
final Person transformedPerson = new Person(firstName, lastName);
|
||||||
|
|
||||||
System.out.println("Converting (" + person + ") into (" + transformedPerson + ")");
|
System.out.println("Converting (" + person + ") into (" + transformedPerson + ")");
|
||||||
|
|
||||||
return transformedPerson;
|
return transformedPerson;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -258,72 +258,72 @@ import org.springframework.jdbc.core.RowMapper;
|
|||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
public class BatchConfiguration {
|
public class BatchConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ItemReader<Person> reader() {
|
public ItemReader<Person> reader() {
|
||||||
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
|
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
|
||||||
reader.setResource(new ClassPathResource("sample-data.csv"));
|
reader.setResource(new ClassPathResource("sample-data.csv"));
|
||||||
reader.setLineMapper(new DefaultLineMapper<Person>() {{
|
reader.setLineMapper(new DefaultLineMapper<Person>() {{
|
||||||
setLineTokenizer(new DelimitedLineTokenizer() {{
|
setLineTokenizer(new DelimitedLineTokenizer() {{
|
||||||
setNames(new String[] { "firstName", "lastName" });
|
setNames(new String[] { "firstName", "lastName" });
|
||||||
}});
|
}});
|
||||||
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
|
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
|
||||||
setTargetType(Person.class);
|
setTargetType(Person.class);
|
||||||
}});
|
}});
|
||||||
}});
|
}});
|
||||||
return reader;
|
return reader;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ItemProcessor<Person, Person> processor() {
|
public ItemProcessor<Person, Person> processor() {
|
||||||
return new PersonItemProcessor();
|
return new PersonItemProcessor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ItemWriter<Person> writer(DataSource dataSource) {
|
public ItemWriter<Person> writer(DataSource dataSource) {
|
||||||
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
|
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
|
||||||
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
|
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
|
||||||
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
|
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
|
||||||
writer.setDataSource(dataSource);
|
writer.setDataSource(dataSource);
|
||||||
return writer;
|
return writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Job importUserJob(JobBuilderFactory jobs, Step s1) {
|
public Job importUserJob(JobBuilderFactory jobs, Step s1) {
|
||||||
return jobs.get("importUserJob")
|
return jobs.get("importUserJob")
|
||||||
.incrementer(new RunIdIncrementer())
|
.incrementer(new RunIdIncrementer())
|
||||||
.flow(s1)
|
.flow(s1)
|
||||||
.end()
|
.end()
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Person> reader,
|
public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Person> reader,
|
||||||
ItemWriter<Person> writer, ItemProcessor<Person, Person> processor) {
|
ItemWriter<Person> writer, ItemProcessor<Person, Person> processor) {
|
||||||
return stepBuilderFactory.get("step1")
|
return stepBuilderFactory.get("step1")
|
||||||
.<Person, Person> chunk(10)
|
.<Person, Person> chunk(10)
|
||||||
.reader(reader)
|
.reader(reader)
|
||||||
.processor(processor)
|
.processor(processor)
|
||||||
.writer(writer)
|
.writer(writer)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
|
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
|
||||||
return new JdbcTemplate(dataSource);
|
return new JdbcTemplate(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ApplicationContext ctx = SpringApplication.run(BatchConfiguration.class, args);
|
ApplicationContext ctx = SpringApplication.run(BatchConfiguration.class, args);
|
||||||
List<Person> results = ctx.getBean(JdbcTemplate.class).query("SELECT first_name, last_name FROM people", new RowMapper<Person>() {
|
List<Person> results = ctx.getBean(JdbcTemplate.class).query("SELECT first_name, last_name FROM people", new RowMapper<Person>() {
|
||||||
@Override
|
@Override
|
||||||
public Person mapRow(ResultSet rs, int row) throws SQLException {
|
public Person mapRow(ResultSet rs, int row) throws SQLException {
|
||||||
return new Person(rs.getString(1), rs.getString(2));
|
return new Person(rs.getString(1), rs.getString(2));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
for (Person person : results) {
|
for (Person person : results) {
|
||||||
System.out.println("Found <" + person + "> in the database.");
|
System.out.println("Found <" + person + "> in the database.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -333,34 +333,34 @@ Break it down:
|
|||||||
|
|
||||||
`src/main/java/hello/BatchConfiguration.java`
|
`src/main/java/hello/BatchConfiguration.java`
|
||||||
```java
|
```java
|
||||||
@Bean
|
@Bean
|
||||||
public ItemReader<Person> reader() {
|
public ItemReader<Person> reader() {
|
||||||
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
|
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
|
||||||
reader.setResource(new ClassPathResource("sample-data.csv"));
|
reader.setResource(new ClassPathResource("sample-data.csv"));
|
||||||
reader.setLineMapper(new DefaultLineMapper<Person>() {{
|
reader.setLineMapper(new DefaultLineMapper<Person>() {{
|
||||||
setLineTokenizer(new DelimitedLineTokenizer() {{
|
setLineTokenizer(new DelimitedLineTokenizer() {{
|
||||||
setNames(new String[] { "firstName", "lastName" });
|
setNames(new String[] { "firstName", "lastName" });
|
||||||
}});
|
}});
|
||||||
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
|
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
|
||||||
setTargetType(Person.class);
|
setTargetType(Person.class);
|
||||||
}});
|
}});
|
||||||
}});
|
}});
|
||||||
return reader;
|
return reader;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ItemProcessor<Person, Person> processor() {
|
public ItemProcessor<Person, Person> processor() {
|
||||||
return new PersonItemProcessor();
|
return new PersonItemProcessor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ItemWriter<Person> writer(DataSource dataSource) {
|
public ItemWriter<Person> writer(DataSource dataSource) {
|
||||||
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
|
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
|
||||||
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
|
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
|
||||||
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
|
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
|
||||||
writer.setDataSource(dataSource);
|
writer.setDataSource(dataSource);
|
||||||
return writer;
|
return writer;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The first chunk of code defines the input, processor, and output.
|
The first chunk of code defines the input, processor, and output.
|
||||||
@ -372,25 +372,25 @@ The next chunk focuses on the actual job configuration.
|
|||||||
|
|
||||||
`src/main/java/hello/BatchConfiguration.java`
|
`src/main/java/hello/BatchConfiguration.java`
|
||||||
```java
|
```java
|
||||||
@Bean
|
@Bean
|
||||||
public Job importUserJob(JobBuilderFactory jobs, Step s1) {
|
public Job importUserJob(JobBuilderFactory jobs, Step s1) {
|
||||||
return jobs.get("importUserJob")
|
return jobs.get("importUserJob")
|
||||||
.incrementer(new RunIdIncrementer())
|
.incrementer(new RunIdIncrementer())
|
||||||
.flow(s1)
|
.flow(s1)
|
||||||
.end()
|
.end()
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Person> reader,
|
public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Person> reader,
|
||||||
ItemWriter<Person> writer, ItemProcessor<Person, Person> processor) {
|
ItemWriter<Person> writer, ItemProcessor<Person, Person> processor) {
|
||||||
return stepBuilderFactory.get("step1")
|
return stepBuilderFactory.get("step1")
|
||||||
.<Person, Person> chunk(10)
|
.<Person, Person> chunk(10)
|
||||||
.reader(reader)
|
.reader(reader)
|
||||||
.processor(processor)
|
.processor(processor)
|
||||||
.writer(writer)
|
.writer(writer)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The first method defines the job and the second one defines a single step. Jobs are built from steps, where each step can involve a reader, a processor, and a writer.
|
The first method defines the job and the second one defines a single step. Jobs are built from steps, where each step can involve a reader, a processor, and a writer.
|
||||||
@ -405,23 +405,23 @@ Finally, you run the application.
|
|||||||
|
|
||||||
`src/main/java/hello/BatchConfiguration.java`
|
`src/main/java/hello/BatchConfiguration.java`
|
||||||
```java
|
```java
|
||||||
@Bean
|
@Bean
|
||||||
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
|
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
|
||||||
return new JdbcTemplate(dataSource);
|
return new JdbcTemplate(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ApplicationContext ctx = SpringApplication.run(BatchConfiguration.class, args);
|
ApplicationContext ctx = SpringApplication.run(BatchConfiguration.class, args);
|
||||||
List<Person> results = ctx.getBean(JdbcTemplate.class).query("SELECT first_name, last_name FROM people", new RowMapper<Person>() {
|
List<Person> results = ctx.getBean(JdbcTemplate.class).query("SELECT first_name, last_name FROM people", new RowMapper<Person>() {
|
||||||
@Override
|
@Override
|
||||||
public Person mapRow(ResultSet rs, int row) throws SQLException {
|
public Person mapRow(ResultSet rs, int row) throws SQLException {
|
||||||
return new Person(rs.getString(1), rs.getString(2));
|
return new Person(rs.getString(1), rs.getString(2));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
for (Person person : results) {
|
for (Person person : results) {
|
||||||
System.out.println("Found <" + person + "> in the database.");
|
System.out.println("Found <" + person + "> in the database.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This example uses a memory-based database (provided by `@EnableBatchProcessing`), meaning that when it's done, the data is gone. For demonstration purposes, there is extra code to create a `JdbcTemplate`, query the database, and print out the names of people the batch job inserts.
|
This example uses a memory-based database (provided by `@EnableBatchProcessing`), meaning that when it's done, the data is gone. For demonstration purposes, there is extra code to create a `JdbcTemplate`, query the database, and print out the names of people the batch job inserts.
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?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"
|
<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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>gs-batch-processing</artifactId>
|
<artifactId>gs-batch-processing</artifactId>
|
||||||
<version>0.1.0</version>
|
<version>0.1.0</version>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
@ -13,7 +13,7 @@
|
|||||||
<version>0.5.0.BUILD-SNAPSHOT</version>
|
<version>0.5.0.BUILD-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-batch</artifactId>
|
<artifactId>spring-boot-starter-batch</artifactId>
|
||||||
@ -22,38 +22,35 @@
|
|||||||
<groupId>org.hsqldb</groupId>
|
<groupId>org.hsqldb</groupId>
|
||||||
<artifactId>hsqldb</artifactId>
|
<artifactId>hsqldb</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<start-class>hello.BatchConfiguration</start-class>
|
<start-class>hello.BatchConfiguration</start-class>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<url>http://repo.springsource.org/libs-snapshot</url>
|
||||||
|
<snapshots><enabled>true</enabled></snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<pluginRepositories>
|
||||||
|
<pluginRepository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<url>http://repo.springsource.org/libs-snapshot</url>
|
||||||
|
<snapshots><enabled>true</enabled></snapshots>
|
||||||
|
</pluginRepository>
|
||||||
|
</pluginRepositories>
|
||||||
|
|
||||||
<repositories>
|
|
||||||
<repository>
|
|
||||||
<id>spring-snapshots</id>
|
|
||||||
<url>http://repo.springsource.org/libs-snapshot</url>
|
|
||||||
<snapshots><enabled>true</enabled></snapshots>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
|
||||||
<id>spring-milestones</id>
|
|
||||||
<url>http://repo.springsource.org/libs-milestone</url>
|
|
||||||
<snapshots><enabled>true</enabled></snapshots>
|
|
||||||
</repository>
|
|
||||||
</repositories>
|
|
||||||
<pluginRepositories>
|
|
||||||
<pluginRepository>
|
|
||||||
<id>spring-snapshots</id>
|
|
||||||
<url>http://repo.springsource.org/libs-snapshot</url>
|
|
||||||
<snapshots><enabled>true</enabled></snapshots>
|
|
||||||
</pluginRepository>
|
|
||||||
</pluginRepositories>
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -35,76 +35,76 @@ import org.springframework.jdbc.core.RowMapper;
|
|||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
public class BatchConfiguration {
|
public class BatchConfiguration {
|
||||||
|
|
||||||
// {!begin readerwriterprocessor}
|
// {!begin readerwriterprocessor}
|
||||||
@Bean
|
@Bean
|
||||||
public ItemReader<Person> reader() {
|
public ItemReader<Person> reader() {
|
||||||
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
|
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
|
||||||
reader.setResource(new ClassPathResource("sample-data.csv"));
|
reader.setResource(new ClassPathResource("sample-data.csv"));
|
||||||
reader.setLineMapper(new DefaultLineMapper<Person>() {{
|
reader.setLineMapper(new DefaultLineMapper<Person>() {{
|
||||||
setLineTokenizer(new DelimitedLineTokenizer() {{
|
setLineTokenizer(new DelimitedLineTokenizer() {{
|
||||||
setNames(new String[] { "firstName", "lastName" });
|
setNames(new String[] { "firstName", "lastName" });
|
||||||
}});
|
}});
|
||||||
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
|
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
|
||||||
setTargetType(Person.class);
|
setTargetType(Person.class);
|
||||||
}});
|
}});
|
||||||
}});
|
}});
|
||||||
return reader;
|
return reader;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ItemProcessor<Person, Person> processor() {
|
public ItemProcessor<Person, Person> processor() {
|
||||||
return new PersonItemProcessor();
|
return new PersonItemProcessor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ItemWriter<Person> writer(DataSource dataSource) {
|
public ItemWriter<Person> writer(DataSource dataSource) {
|
||||||
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
|
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
|
||||||
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
|
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
|
||||||
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
|
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
|
||||||
writer.setDataSource(dataSource);
|
writer.setDataSource(dataSource);
|
||||||
return writer;
|
return writer;
|
||||||
}
|
}
|
||||||
// {!end readerwriterprocessor}
|
// {!end readerwriterprocessor}
|
||||||
|
|
||||||
// {!begin jobstep}
|
// {!begin jobstep}
|
||||||
@Bean
|
@Bean
|
||||||
public Job importUserJob(JobBuilderFactory jobs, Step s1) {
|
public Job importUserJob(JobBuilderFactory jobs, Step s1) {
|
||||||
return jobs.get("importUserJob")
|
return jobs.get("importUserJob")
|
||||||
.incrementer(new RunIdIncrementer())
|
.incrementer(new RunIdIncrementer())
|
||||||
.flow(s1)
|
.flow(s1)
|
||||||
.end()
|
.end()
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Person> reader,
|
public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Person> reader,
|
||||||
ItemWriter<Person> writer, ItemProcessor<Person, Person> processor) {
|
ItemWriter<Person> writer, ItemProcessor<Person, Person> processor) {
|
||||||
return stepBuilderFactory.get("step1")
|
return stepBuilderFactory.get("step1")
|
||||||
.<Person, Person> chunk(10)
|
.<Person, Person> chunk(10)
|
||||||
.reader(reader)
|
.reader(reader)
|
||||||
.processor(processor)
|
.processor(processor)
|
||||||
.writer(writer)
|
.writer(writer)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
// {!end jobstep}
|
// {!end jobstep}
|
||||||
|
|
||||||
// {!begin templatemain}
|
// {!begin templatemain}
|
||||||
@Bean
|
@Bean
|
||||||
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
|
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
|
||||||
return new JdbcTemplate(dataSource);
|
return new JdbcTemplate(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ApplicationContext ctx = SpringApplication.run(BatchConfiguration.class, args);
|
ApplicationContext ctx = SpringApplication.run(BatchConfiguration.class, args);
|
||||||
List<Person> results = ctx.getBean(JdbcTemplate.class).query("SELECT first_name, last_name FROM people", new RowMapper<Person>() {
|
List<Person> results = ctx.getBean(JdbcTemplate.class).query("SELECT first_name, last_name FROM people", new RowMapper<Person>() {
|
||||||
@Override
|
@Override
|
||||||
public Person mapRow(ResultSet rs, int row) throws SQLException {
|
public Person mapRow(ResultSet rs, int row) throws SQLException {
|
||||||
return new Person(rs.getString(1), rs.getString(2));
|
return new Person(rs.getString(1), rs.getString(2));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
for (Person person : results) {
|
for (Person person : results) {
|
||||||
System.out.println("Found <" + person + "> in the database.");
|
System.out.println("Found <" + person + "> in the database.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// {!end templatemain}
|
// {!end templatemain}
|
||||||
}
|
}
|
||||||
|
@ -3,19 +3,19 @@ package hello;
|
|||||||
public class Person {
|
public class Person {
|
||||||
private String lastName;
|
private String lastName;
|
||||||
private String firstName;
|
private String firstName;
|
||||||
|
|
||||||
public Person() {
|
public Person() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Person(String firstName, String lastName) {
|
public Person(String firstName, String lastName) {
|
||||||
this.firstName = firstName;
|
this.firstName = firstName;
|
||||||
this.lastName = lastName;
|
this.lastName = lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFirstName(String firstName) {
|
public void setFirstName(String firstName) {
|
||||||
this.firstName = firstName;
|
this.firstName = firstName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFirstName() {
|
public String getFirstName() {
|
||||||
return firstName;
|
return firstName;
|
||||||
@ -26,11 +26,12 @@ public class Person {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setLastName(String lastName) {
|
public void setLastName(String lastName) {
|
||||||
this.lastName = lastName;
|
this.lastName = lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "firstName: " + firstName + ", lastName: " + lastName;
|
return "firstName: " + firstName + ", lastName: " + lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,17 @@ package hello;
|
|||||||
import org.springframework.batch.item.ItemProcessor;
|
import org.springframework.batch.item.ItemProcessor;
|
||||||
|
|
||||||
public class PersonItemProcessor implements ItemProcessor<Person, Person> {
|
public class PersonItemProcessor implements ItemProcessor<Person, Person> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Person process(final Person person) throws Exception {
|
public Person process(final Person person) throws Exception {
|
||||||
final String firstName = person.getFirstName().toUpperCase();
|
final String firstName = person.getFirstName().toUpperCase();
|
||||||
final String lastName = person.getLastName().toUpperCase();
|
final String lastName = person.getLastName().toUpperCase();
|
||||||
|
|
||||||
final Person transformedPerson = new Person(firstName, lastName);
|
final Person transformedPerson = new Person(firstName, lastName);
|
||||||
|
|
||||||
System.out.println("Converting (" + person + ") into (" + transformedPerson + ")");
|
System.out.println("Converting (" + person + ") into (" + transformedPerson + ")");
|
||||||
|
|
||||||
return transformedPerson;
|
return transformedPerson;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
DROP TABLE people IF EXISTS;
|
DROP TABLE people IF EXISTS;
|
||||||
|
|
||||||
CREATE TABLE people (
|
CREATE TABLE people (
|
||||||
person_id BIGINT IDENTITY NOT NULL PRIMARY KEY,
|
person_id BIGINT IDENTITY NOT NULL PRIMARY KEY,
|
||||||
first_name VARCHAR(20),
|
first_name VARCHAR(20),
|
||||||
last_name VARCHAR(20)
|
last_name VARCHAR(20)
|
||||||
);
|
);
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?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"
|
<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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>gs-batch-processing</artifactId>
|
<artifactId>gs-batch-processing</artifactId>
|
||||||
<version>0.1.0</version>
|
<version>0.1.0</version>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
@ -13,7 +13,7 @@
|
|||||||
<version>0.5.0.BUILD-SNAPSHOT</version>
|
<version>0.5.0.BUILD-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-batch</artifactId>
|
<artifactId>spring-boot-starter-batch</artifactId>
|
||||||
@ -22,25 +22,22 @@
|
|||||||
<groupId>org.hsqldb</groupId>
|
<groupId>org.hsqldb</groupId>
|
||||||
<artifactId>hsqldb</artifactId>
|
<artifactId>hsqldb</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<url>http://repo.springsource.org/libs-snapshot</url>
|
||||||
|
<snapshots><enabled>true</enabled></snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<pluginRepositories>
|
||||||
|
<pluginRepository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<url>http://repo.springsource.org/libs-snapshot</url>
|
||||||
|
<snapshots><enabled>true</enabled></snapshots>
|
||||||
|
</pluginRepository>
|
||||||
|
</pluginRepositories>
|
||||||
|
|
||||||
<repositories>
|
|
||||||
<repository>
|
|
||||||
<id>spring-snapshots</id>
|
|
||||||
<url>http://repo.springsource.org/libs-snapshot</url>
|
|
||||||
<snapshots><enabled>true</enabled></snapshots>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
|
||||||
<id>spring-milestones</id>
|
|
||||||
<url>http://repo.springsource.org/libs-milestone</url>
|
|
||||||
<snapshots><enabled>true</enabled></snapshots>
|
|
||||||
</repository>
|
|
||||||
</repositories>
|
|
||||||
<pluginRepositories>
|
|
||||||
<pluginRepository>
|
|
||||||
<id>spring-snapshots</id>
|
|
||||||
<url>http://repo.springsource.org/libs-snapshot</url>
|
|
||||||
<snapshots><enabled>true</enabled></snapshots>
|
|
||||||
</pluginRepository>
|
|
||||||
</pluginRepositories>
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
DROP TABLE people IF EXISTS;
|
DROP TABLE people IF EXISTS;
|
||||||
|
|
||||||
CREATE TABLE people (
|
CREATE TABLE people (
|
||||||
person_id BIGINT IDENTITY NOT NULL PRIMARY KEY,
|
person_id BIGINT IDENTITY NOT NULL PRIMARY KEY,
|
||||||
first_name VARCHAR(20),
|
first_name VARCHAR(20),
|
||||||
last_name VARCHAR(20)
|
last_name VARCHAR(20)
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user