Spring Batch教程(五)spring boot实现batch功能注解示例:读写文本文件(读取一行数据,针对一行数据进行求和)

Spring batch 系列文章

Spring Batch教程(一) 简单的介绍以及通过springbatch将xml文件转成txt文件
Spring Batch教程(二)示例:将txt文件转成xml文件以及读取xml文件内容存储到数据库mysql
Spring Batch教程(三)示例:从mysql中读取数据写入文本和从多个文本中读取内容写入mysql
Spring Batch教程(四)tasklet使用示例:spring batch的定时任务使用
Spring Batch教程(五)spring boot实现batch功能注解示例:读写文本文件
Spring Batch教程(六)spring boot实现batch功能注解示例:读文件写入mysql


文章目录

  • Spring batch 系列文章
  • 一、示例1:读取文本文件写文本文件
    • 1、maven依赖
    • 2、java bean
      • 1)、Student
      • 2)、StudentTotalScore
    • 3、配置spring batch的ItemReader、ItemWriter和ItemProcessor
    • 4、创建ItemProcessor实现类
    • 5、创建一个运行job的main类
    • 6、准备测试数据
    • 7、验证


本文介绍了1个示例,即通过spring boot 启动spring batch的任务,该任务是通过注解实现的。
本文使用的是jdk8版本,最新版本的spring core和springb batch用不了。

一、示例1:读取文本文件写文本文件

本示例是读取一行数据,针对一行数据进行求和。

1、maven依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-batch</artifactId>
			<version>2.3.12.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.2</version>
			<scope>provided</scope>
		</dependency>

2、java bean

1)、Student

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 
 * @author alanchan
 *
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
	private String id;
	private int chinese;
	private int math;
	private int english;

}

2)、StudentTotalScore

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 
 * @author alanchan
 *
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class StudentTotalScore {
	private String id;
	private int totalScore;

}

3、配置spring batch的ItemReader、ItemWriter和ItemProcessor

逻辑上与spring batch一致


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.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
import org.springframework.batch.item.file.transform.DelimitedLineAggregator;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

import com.win.byannotation.bean.Student;
import com.win.byannotation.bean.StudentTotalScore;

/**
 * 
 * @author alanchan
 *
 */
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
	private final String inputFiles = "student-data.txt";
	private final String outputFiles = "d:/studentscore-data.txt";
//	private final String outputFiles = "studentscore-data.txt";

	@Bean
	public ItemReader<Student> reader() {
		FlatFileItemReader<Student> reader = new FlatFileItemReader<Student>();
		reader.setResource(new ClassPathResource(inputFiles));
		reader.setLineMapper(new DefaultLineMapper<Student>() {
			{
				setLineTokenizer(new DelimitedLineTokenizer() {
					{
						setNames(new String[] { "id", "chinese", "math", "english" });
					}
				});
				setFieldSetMapper(new BeanWrapperFieldSetMapper<Student>() {
					{
						setTargetType(Student.class);
					}
				});
			}
		});
		return reader;
	}

	@Bean
	public ItemWriter<StudentTotalScore> writer() {
		FlatFileItemWriter<StudentTotalScore> writer = new FlatFileItemWriter<StudentTotalScore>();
		writer.setResource(new FileSystemResource(outputFiles));
//		writer.setResource(new ClassPathResource(outputFiles));
		DelimitedLineAggregator<StudentTotalScore> delLineAgg = new DelimitedLineAggregator<StudentTotalScore>();
		delLineAgg.setDelimiter(",");
		BeanWrapperFieldExtractor<StudentTotalScore> fieldExtractor = new BeanWrapperFieldExtractor<StudentTotalScore>();
		fieldExtractor.setNames(new String[] { "id", "totalScore" });
		delLineAgg.setFieldExtractor(fieldExtractor);
		writer.setLineAggregator(delLineAgg);
		return writer;
	}

	@Bean
	public ItemProcessor<Student, StudentTotalScore> processor() {
		return new StudentItemProcessor();
	}

	@Bean
	public Job createMarkSheet(JobBuilderFactory jobs, Step step) {
		return jobs.get("createMarkSheet").flow(step).end().build();
	}

	@Bean
	public Step step(StepBuilderFactory stepBuilderFactory, ItemReader<Student> reader, ItemWriter<StudentTotalScore> writer, ItemProcessor<Student, StudentTotalScore> processor) {
		return stepBuilderFactory.get("step").<Student, StudentTotalScore>chunk(5).reader(reader).processor(processor).writer(writer).build();
	}

}

4、创建ItemProcessor实现类


import org.springframework.batch.item.ItemProcessor;

import com.win.byannotation.bean.StudentTotalScore;
import com.win.byannotation.bean.Student;

/**
 * 
 * @author alanchan
 *
 */
public class StudentItemProcessor implements ItemProcessor<Student, StudentTotalScore> {

	@Override
	public StudentTotalScore process(final Student student) throws Exception {
		int totalScore = student.getChinese() + student.getMath() + student.getEnglish();
		System.out.println("student id:" + student.getId() + " and Total score:" + totalScore);
		StudentTotalScore marksheet = new StudentTotalScore(student.getId(), totalScore);
		return marksheet;
	}

}

5、创建一个运行job的main类


import java.io.IOException;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

/**
 * 
 * @author alanchan
 *
 */

@ComponentScan
//@EnableAutoConfiguration/@SpringBootApplication 该组件包含数据源配置
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class App {
	public static void main(String[] args) throws IOException {
		ApplicationContext ctx = SpringApplication.run(App.class, args);
		
	}
}

6、准备测试数据

文件位置:/sping-batch/src/main/resources/student-data.txt

student-1,90,85,96
student-2,92,97,94
student-3,95,93,100

7、验证

启动应用程序,观察输出文件内容
Spring Batch教程(五)spring boot实现batch功能注解示例:读写文本文件(读取一行数据,针对一行数据进行求和)_第1张图片
程序控制台输出

2023-07-21 18:14:07.106  INFO 52200 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.
2023-07-21 18:14:07.106  INFO 52200 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler version: 2.2.1
2023-07-21 18:14:07.106  INFO 52200 --- [           main] org.quartz.core.QuartzScheduler          : JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@410954b
2023-07-21 18:14:07.135  INFO 52200 --- [           main] o.s.s.quartz.SchedulerFactoryBean        : Starting Quartz Scheduler now
2023-07-21 18:14:07.136  INFO 52200 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler quartzScheduler_$_NON_CLUSTERED started.
2023-07-21 18:14:07.143  INFO 52200 --- [           main] com.win.byannotation.App                 : Started App in 0.92 seconds (JVM running for 1.196)
2023-07-21 18:14:07.145  INFO 52200 --- [           main] o.s.b.a.b.JobLauncherApplicationRunner   : Running default command line with: []
2023-07-21 18:14:07.145  WARN 52200 --- [           main] o.s.b.c.c.a.DefaultBatchConfigurer       : No datasource was provided...using a Map based JobRepository
2023-07-21 18:14:07.145  WARN 52200 --- [           main] o.s.b.c.c.a.DefaultBatchConfigurer       : No transaction manager was provided, using a ResourcelessTransactionManager
2023-07-21 18:14:07.156  INFO 52200 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2023-07-21 18:14:07.183  INFO 52200 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=createMarkSheet]] launched with the following parameters: [{}]
2023-07-21 18:14:07.210  INFO 52200 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step]
student id:student-1 and Total score:271
student id:student-2 and Total score:283
student id:student-3 and Total score:288
2023-07-21 18:14:07.240  INFO 52200 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [step] executed in 30ms
2023-07-21 18:14:07.244  INFO 52200 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=createMarkSheet]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 43ms

以上,通过spring boot 启动spring batch的任务,该任务是通过注解实现的。

你可能感兴趣的:(#,spring,batch,spring,batch,spring,boot,spring,批处理,spring,cloud,spring,定时任务,spring,mvc)