第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

春季批處理:如何使用平面文件讀取CSV文件的頁腳并進(jìn)行驗(yàn)證

春季批處理:如何使用平面文件讀取CSV文件的頁腳并進(jìn)行驗(yàn)證

喵喵時光機(jī) 2022-09-14 16:23:43
我正在使用彈簧批處理和扁平文件條目閱讀器來讀取.CSV 文件。文件具有頁眉(第一行)、詳細(xì)信息和頁腳(最后一行)。因此,我想通過頁腳行驗(yàn)證詳細(xì)信息的總數(shù)。這是我.csv文件的示例。電影.csv名稱|類型|諾丁山|浪漫喜劇|1999年玩具總動員3|動畫|2010美國隊長3:第一復(fù)仇者|動作|20113從示例文件第一行是標(biāo)題(我忽略它)。第 2-4 行是細(xì)節(jié)行,最后是頁腳。我想讀取頁腳并獲取值(最后一行= 3),然后,獲取詳細(xì)信息的總數(shù)(在這種情況下,我們有3行),最后我將驗(yàn)證頁腳(3)的總數(shù),詳細(xì)信息記錄的總數(shù)(3)是否相等?這是我的代碼。@Bean@StepScopepublic 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;}
查看完整描述

1 回答

?
莫回?zé)o

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個贊

可以使用面向塊的步驟作為作業(yè)業(yè)務(wù)邏輯之前的驗(yàn)證步驟。此步驟將使用 a 保存最后一項,使用 a 進(jìn)行驗(yàn)證。下面是一個快速示例:ItemReadListenerStepExecutionListener


import 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

@EnableBatchProcessing

public class MyJob {


    @Autowired

    private JobBuilderFactory jobs;


    @Autowired

    private StepBuilderFactory steps;


    @Bean

    @StepScope

    public FlatFileItemReader<String> itemReader() {

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

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

        reader.setResource(new ByteArrayResource("header\nitem1\nitem2\n2".getBytes()));

        reader.setLineMapper(new PassThroughLineMapper());

        return reader;

    }


    @Bean

    public ItemWriter<String> itemWriter() {

        return items -> {

            for (String item : items) {

                System.out.println("item = " + item);

            }

        };

    }


    @Bean

    public Step step1() {

        MyListener myListener = new MyListener();

        return steps.get("step1")

                .<String, String>chunk(5)

                .reader(itemReader())

                .writer(itemWriter())

                .listener((ItemReadListener<String>) myListener)

                .listener((StepExecutionListener) myListener)

                .build();

    }


    @Bean

    public Step step2() {

        return steps.get("step2")

                .tasklet((contribution, chunkContext) -> {

                    System.out.println("Total count is ok as validated by step1");

                    return RepeatStatus.FINISHED;

                })

                .build();

    }


    @Bean

    public Job job() {

        return jobs.get("job")

                .start(step1())

                .next(step2())

                .build();

    }


    static class MyListener extends StepExecutionListenerSupport implements ItemReadListener<String> {


        private String lastItem;


        @Override

        public void beforeRead() {

        }


        @Override

        public void afterRead(String item) {

            this.lastItem = item;

        }


        @Override

        public void onReadError(Exception ex) {


        }


        @Override

        public ExitStatus afterStep(StepExecution stepExecution) {

            int readCount = stepExecution.getReadCount();

            int totalCountInFooter = Integer.valueOf(this.lastItem); // TODO sanity checks (number format, etc)

            System.out.println("readCount = " + (readCount - 1)); // substract footer from the read count

            System.out.println("totalCountInFooter = " + totalCountInFooter);

            // TODO do validation on readCount vs totalCountInFooter

            return ExitStatus.COMPLETED; // return appropriate exit status according to validation result

        }

    }


    public static void main(String[] args) throws Exception {

        ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);

        JobLauncher jobLauncher = context.getBean(JobLauncher.class);

        Job job = context.getBean(Job.class);

        jobLauncher.run(job, new JobParameters());

    }


}

此示例打?。?/p>


item = item1

item = item2

item = 2

readCount = 2

totalCountInFooter = 2

Total count is ok as validated by step1

希望這有幫助。


查看完整回答
反對 回復(fù) 2022-09-14
  • 1 回答
  • 0 關(guān)注
  • 91 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號