Skip to content

Commit

Permalink
Spring Batch处理数据
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyouzhuguli committed Mar 12, 2020
1 parent c54c5ac commit 93e149d
Show file tree
Hide file tree
Showing 12 changed files with 458 additions and 0 deletions.
49 changes: 49 additions & 0 deletions 70.spring-batch-itemprocessor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cc.mrbird</groupId>
<artifactId>spring-batch-itemprocessor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-batch-itemprocessor</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cc.mrbird.batch;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchItemprocessorApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBatchItemprocessorApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cc.mrbird.batch.entity;

import javax.validation.constraints.NotBlank;

/**
* @author MrBird
*/
public class TestData {

private int id;
private String field1;
private String field2;
@NotBlank
private String field3;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getField1() {
return field1;
}

public void setField1(String field1) {
this.field1 = field1;
}

public String getField2() {
return field2;
}

public void setField2(String field2) {
this.field2 = field2;
}

public String getField3() {
return field3;
}

public void setField3(String field3) {
this.field3 = field3;
}

@Override
public String toString() {
return "TestData{" +
"id=" + id +
", field1='" + field1 + '\'' +
", field2='" + field2 + '\'' +
", field3='" + field3 + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cc.mrbird.batch.entity.job;

import cc.mrbird.batch.entity.TestData;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.item.validator.BeanValidatingItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
* @author MrBird
*/
@Component
public class BeanValidatingItemProcessorDemo {

@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private ListItemReader<TestData> simpleReader;

@Bean
public Job beanValidatingItemProcessorJob() throws Exception {
return jobBuilderFactory.get("beanValidatingItemProcessorJob")
.start(step())
.build();
}

private Step step() throws Exception {
return stepBuilderFactory.get("step")
.<TestData, TestData>chunk(2)
.reader(simpleReader)
.processor(beanValidatingItemProcessor())
.writer(list -> list.forEach(System.out::println))
.build();
}

private BeanValidatingItemProcessor<TestData> beanValidatingItemProcessor() throws Exception {
BeanValidatingItemProcessor<TestData> beanValidatingItemProcessor = new BeanValidatingItemProcessor<>();
// 开启过滤,不符合规则的数据被过滤掉;
beanValidatingItemProcessor.setFilter(true);
beanValidatingItemProcessor.afterPropertiesSet();
return beanValidatingItemProcessor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cc.mrbird.batch.entity.job;

import cc.mrbird.batch.entity.TestData;
import cc.mrbird.batch.processor.TestDataFilterItemProcessor;
import cc.mrbird.batch.processor.TestDataTransformItemPorcessor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.support.CompositeItemProcessor;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
* @author MrBird
*/
@Component
public class CompositeItemProcessorDemo {

@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private ListItemReader<TestData> simpleReader;
@Autowired
private TestDataFilterItemProcessor testDataFilterItemProcessor;
@Autowired
private TestDataTransformItemPorcessor testDataTransformItemPorcessor;

@Bean
public Job compositeItemProcessorJob() {
return jobBuilderFactory.get("compositeItemProcessorJob")
.start(step())
.build();
}

private Step step() {
return stepBuilderFactory.get("step")
.<TestData, TestData>chunk(2)
.reader(simpleReader)
.processor(compositeItemProcessor())
.writer(list -> list.forEach(System.out::println))
.build();
}

// CompositeItemProcessor组合多种中间处理器
private CompositeItemProcessor<TestData, TestData> compositeItemProcessor() {
CompositeItemProcessor<TestData, TestData> processor = new CompositeItemProcessor<>();
List<ItemProcessor<TestData, TestData>> processors = Arrays.asList(testDataFilterItemProcessor, testDataTransformItemPorcessor);
// 代理两个processor
processor.setDelegates(processors);
return processor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cc.mrbird.batch.entity.job;

import cc.mrbird.batch.entity.TestData;
import cc.mrbird.batch.processor.TestDataFilterItemProcessor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
* @author MrBird
*/
@Component
public class TestDataFilterItemProcessorDemo {

@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private ListItemReader<TestData> simpleReader;
@Autowired
private TestDataFilterItemProcessor testDataFilterItemProcessor;

@Bean
public Job testDataFilterItemProcessorJob() {
return jobBuilderFactory.get("testDataFilterItemProcessorJob")
.start(step())
.build();
}

private Step step() {
return stepBuilderFactory.get("step")
.<TestData, TestData>chunk(2)
.reader(simpleReader)
.processor(testDataFilterItemProcessor)
.writer(list -> list.forEach(System.out::println))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cc.mrbird.batch.entity.job;

import cc.mrbird.batch.entity.TestData;
import cc.mrbird.batch.processor.TestDataFilterItemProcessor;
import cc.mrbird.batch.processor.TestDataTransformItemPorcessor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
* @author MrBird
*/
@Component
public class TestDataTransformItemPorcessorDemo {

@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private ListItemReader<TestData> simpleReader;
@Autowired
private TestDataTransformItemPorcessor testDataTransformItemPorcessor;

@Bean
public Job testDataTransformItemPorcessorJob() {
return jobBuilderFactory.get("testDataTransformItemPorcessorJob")
.start(step())
.build();
}

private Step step() {
return stepBuilderFactory.get("step")
.<TestData, TestData>chunk(2)
.reader(simpleReader)
.processor(testDataTransformItemPorcessor)
.writer(list -> list.forEach(System.out::println))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cc.mrbird.batch.entity.job;

import cc.mrbird.batch.entity.TestData;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.item.validator.ValidatingItemProcessor;
import org.springframework.batch.item.validator.ValidationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
* @author MrBird
*/
@Component
public class ValidatingItemProcessorDemo {

@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private ListItemReader<TestData> simpleReader;

@Bean
public Job validatingItemProcessorJob() {
return jobBuilderFactory.get("validatingItemProcessorJob")
.start(step())
.build();
}

private Step step() {
return stepBuilderFactory.get("step")
.<TestData, TestData>chunk(2)
.reader(simpleReader)
.processor(validatingItemProcessor())
.writer(list -> list.forEach(System.out::println))
.build();
}

private ValidatingItemProcessor<TestData> validatingItemProcessor() {
ValidatingItemProcessor<TestData> processor = new ValidatingItemProcessor<>();
processor.setValidator(value -> {
// 对每一条数据进行校验
if ("".equals(value.getField3())) {
// 如果field3的值为空串,则抛异常
throw new ValidationException("field3的值不合法");
}
});
return processor;
}
}
Loading

0 comments on commit 93e149d

Please sign in to comment.