下面是手把手教你搭建第一个Spring Batch项目的步骤:
1. 确保所需环境已安装
在开始配置Spring Batch之前,需要确保以下环境已安装:
- JDK 1.8或更高版本
- IDE(例如Eclipse或IntelliJ IDEA)
- Gradle或Maven(这里我们选择Gradle)
2. 创建Gradle项目
可以通过以下方式创建Gradle项目:
- 在IDE中选择“File” -> “New” -> “Project”
- 选择Gradle项目模板,并按照提示输入项目名称和位置
- 建立项目后,在“build.gradle”文件中添加Spring Batch依赖项:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-batch'
//其他依赖(如果需要)
}
3. 编写Batch Job
在“src/main/java”目录下新建类(例如“MyBatchJob”)。它将是我们的第一个Spring Batch Job。
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.file.MultiResourceItemReader;
import org.springframework.batch.item.file.builder.MultiResourceItemReaderBuilder;
import org.springframework.batch.item.file.mapping.JsonLineMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.batch.item.support.CompositeItemProcessor;
import org.springframework.batch.item.support.builder.CompositeItemProcessorBuilder;
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.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.validation.Validator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Configuration
@EnableBatchProcessing
public class MyBatchJob {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Autowired
private Validator validator;
@Bean
public Job myJob() {
return jobs.get("myJob")
.incrementer(new RunIdIncrementer())
.start(step())
.build();
}
@Bean
public Step step() {
return steps.get("step")
.<MyObject, MyObject>chunk(2)
.reader(reader())
.processor(compositeProcessor())
.writer(writer())
.build();
}
@Bean
public CompositeItemProcessor<MyObject, MyObject> compositeProcessor() {
CompositeItemProcessor<MyObject, MyObject> processor = new CompositeItemProcessorBuilder<MyObject, MyObject>()
.delegates(Arrays.asList(validatingProcessor(), myItemProcessor()))
.build();
processor.afterPropertiesSet();
return processor;
}
@Bean
public ValidatingItemProcessor<MyObject> validatingProcessor() {
ValidatingItemProcessor<MyObject> validator = new ValidatingItemProcessor<>();
validator.setValidator(validator);
return validator;
}
@Bean
public ItemProcessor<MyObject, MyObject> myItemProcessor() {
return item -> {
item.setTitle(item.getTitle().toUpperCase());
return item;
};
}
@Bean
public ItemReader<MyObject> reader() {
return new MultiResourceItemReaderBuilder<MyObject>()
.name("jsonItemReader")
.resources(new ClassPathResource("test.json"))
.delegate(jsonItemReader())
.build();
}
@Bean
public ItemWriter<MyObject> writer() {
return list -> {
for (MyObject item : list) {
System.out.println(item);
}
};
}
@Bean
public ItemReader<MyObject> jsonItemReader() {
JsonLineMapper<MyObject> lineMapper = new JsonLineMapper<>();
lineMapper.setFieldSetMapper(fieldSet -> {
MyObject item = new MyObject();
item.setTitle(fieldSet.readString("title"));
item.setPrice(fieldSet.readString("price"));
return item;
});
return new TestItemReader("test.json", lineMapper);
}
private static class TestItemReader extends MultiResourceItemReader<MyObject> {
public TestItemReader(String fileName, JsonLineMapper<MyObject> lineMapper) {
super();
setResources(new ClassPathResource(fileName));
setDelegate(new JsonLineItemReader(lineMapper));
}
}
private static class JsonLineItemReader extends org.springframework.batch.item.file.FlatFileItemReader<MyObject> {
public JsonLineItemReader(JsonLineMapper<MyObject> lineMapper) {
setLineMapper(lineMapper);
}
}
public static class MyObject {
private String title;
private String price;
// getter / setter
@Override
public String toString() {
return "MyObject{" +
"title='" + title + '\'' +
", price='" + price + '\'' +
'}';
}
}
}
4. 运行Batch Job
在IDE中右键单击项目,选择“Run As” -> “Spring Boot App”。这将运行应用程序并触发Batch Job。可以在控制台输出中查看Batch Job的进度和输出。
这是一个简单的例子,从一个JSON文件中读取商品信息,并将商品标题重命名为大写字母。在复杂的Spring Batch项目中,可以添加许多其他步骤和处理器来解析,转换和过滤数据。
5. 示例
以下是处理器(ItemProcessor)的示例代码,用于将商品价格转换为美元:
public class ConvertPriceToUsdProcessor implements ItemProcessor<MyObject, MyObject> {
private static final double EUR_TO_USD_EXCHANGE_RATE = 1.11;
@Override
public MyObject process(MyObject item) throws Exception {
String price = item.getPrice();
double priceInEur = Double.parseDouble(price.replace("€", ""));
double priceInUsd = priceInEur * EUR_TO_USD_EXCHANGE_RATE;
item.setPrice("$" + String.format("%.2f", priceInUsd));
return item;
}
}
该处理器需要在上面的“compositeProcessor”中添加作为一个委托(Delegate),并且需要在调用compositesProcessor.afterPropertiesSet()之前添加。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:手把手教你搭建第一个Spring Batch项目的步骤 - Python技术站