春季批处理:如何使用平面文件读取CSV文件的页脚并进行验证

我正在使用弹簧批处理和扁平文件条目阅读器来读取.CSV 文件。文件具有页眉(第一行)、详细信息和页脚(最后一行)。因此,我想通过页脚行验证详细信息的总数。

这是我.csv文件的示例。

电影.csv

名称|类型|诺丁山|
浪漫喜剧|1999
年玩具总动员3|动画|2010
美国队长3:第一复仇者|动作|2011
3

从示例文件
第一行是标题(我忽略它)。
第 2-4 行是细节行,最后是页脚。

我想读取页脚并获取值(最后一行= 3),
然后,获取详细信息的总数(在这种情况下,我们有3行),
最后我将验证页脚(3)的总数,详细信息记录的总数(3)是否相等?


这是我的代码。

@Bean

@StepScope

public FlatFileItemReader<Movie> movieItemReader(String filePath) {

        FlatFileItemReader<Movie> reader = new FlatFileItemReader<>();

        reader.setLinesToSkip(1);   //skip header line

        reader.setResource(new PathResource(filePath));


        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer("|");

        DefaultLineMapper<Movie> movieLineMapper = new DefaultLineMapper<>();

        FieldSetMapper<Movie> movieMapper = movieFieldSetMapper();


        movieLineMapper.setLineTokenizer(tokenizer);

        movieLineMapper.setFieldSetMapper(movieFieldSetMapper);

        movieLineMapper.afterPropertiesSet();

        reader.setLineMapper(movieLineMapper);

        return reader;

}


public FieldSetMapper<Movie> movieFieldSetMapper() {

        BeanWrapperFieldSetMapper<Movie> movieMapper = new BeanWrapperFieldSetMapper<>();

        movieMapper.setTargetType(Movie.class);

        return movieMapper;

}


喵喵时光机
浏览 115回答 1
1回答

莫回无

可以使用面向块的步骤作为作业业务逻辑之前的验证步骤。此步骤将使用 a 保存最后一项,使用 a 进行验证。下面是一个快速示例:ItemReadListenerStepExecutionListenerimport org.springframework.batch.core.ExitStatus;import org.springframework.batch.core.ItemReadListener;import org.springframework.batch.core.Job;import org.springframework.batch.core.JobParameters;import org.springframework.batch.core.Step;import org.springframework.batch.core.StepExecution;import org.springframework.batch.core.StepExecutionListener;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.configuration.annotation.StepScope;import org.springframework.batch.core.launch.JobLauncher;import org.springframework.batch.core.listener.StepExecutionListenerSupport;import org.springframework.batch.item.ItemWriter;import org.springframework.batch.item.file.FlatFileItemReader;import org.springframework.batch.item.file.mapping.PassThroughLineMapper;import org.springframework.batch.repeat.RepeatStatus;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ByteArrayResource;@Configuration@EnableBatchProcessingpublic class MyJob {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private JobBuilderFactory jobs;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private StepBuilderFactory steps;&nbsp; &nbsp; @Bean&nbsp; &nbsp; @StepScope&nbsp; &nbsp; public FlatFileItemReader<String> itemReader() {&nbsp; &nbsp; &nbsp; &nbsp; FlatFileItemReader<String> reader = new FlatFileItemReader<>();&nbsp; &nbsp; &nbsp; &nbsp; reader.setLinesToSkip(1);&nbsp; &nbsp;//skip header line&nbsp; &nbsp; &nbsp; &nbsp; reader.setResource(new ByteArrayResource("header\nitem1\nitem2\n2".getBytes()));&nbsp; &nbsp; &nbsp; &nbsp; reader.setLineMapper(new PassThroughLineMapper());&nbsp; &nbsp; &nbsp; &nbsp; return reader;&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; public ItemWriter<String> itemWriter() {&nbsp; &nbsp; &nbsp; &nbsp; return items -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String item : items) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("item = " + item);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; public Step step1() {&nbsp; &nbsp; &nbsp; &nbsp; MyListener myListener = new MyListener();&nbsp; &nbsp; &nbsp; &nbsp; return steps.get("step1")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<String, String>chunk(5)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .reader(itemReader())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .writer(itemWriter())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .listener((ItemReadListener<String>) myListener)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .listener((StepExecutionListener) myListener)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; public Step step2() {&nbsp; &nbsp; &nbsp; &nbsp; return steps.get("step2")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .tasklet((contribution, chunkContext) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Total count is ok as validated by step1");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return RepeatStatus.FINISHED;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; public Job job() {&nbsp; &nbsp; &nbsp; &nbsp; return jobs.get("job")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .start(step1())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .next(step2())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; }&nbsp; &nbsp; static class MyListener extends StepExecutionListenerSupport implements ItemReadListener<String> {&nbsp; &nbsp; &nbsp; &nbsp; private String lastItem;&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void beforeRead() {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void afterRead(String item) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.lastItem = item;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onReadError(Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public ExitStatus afterStep(StepExecution stepExecution) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int readCount = stepExecution.getReadCount();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int totalCountInFooter = Integer.valueOf(this.lastItem); // TODO sanity checks (number format, etc)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("readCount = " + (readCount - 1)); // substract footer from the read count&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("totalCountInFooter = " + totalCountInFooter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO do validation on readCount vs totalCountInFooter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ExitStatus.COMPLETED; // return appropriate exit status according to validation result&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);&nbsp; &nbsp; &nbsp; &nbsp; JobLauncher jobLauncher = context.getBean(JobLauncher.class);&nbsp; &nbsp; &nbsp; &nbsp; Job job = context.getBean(Job.class);&nbsp; &nbsp; &nbsp; &nbsp; jobLauncher.run(job, new JobParameters());&nbsp; &nbsp; }}此示例打印:item = item1item = item2item = 2readCount = 2totalCountInFooter = 2Total count is ok as validated by step1希望这有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java