春季批量工作不读第一行

时间:2018-04-24 13:35:19

标签: java spring spring-boot spring-batch

我有一个弹簧批处理作业来读取csv文件。 我想问一下如何避免在读取后处理第一行? 第一行可以读取,但只是不处理它/不保存它。 我该怎么做? 以下是批处理作业配置代码                         。 .................................................. .................................................. .................................................. .................................................. .................................................. .........

                    .


@Configuration
public class ImportJobConfig<JobCompletionNotificationListener> {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private DataSource dataSource;

    private JdbcTemplate jdbcTemplate;



    @Bean
    @Scope(value = "step", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public FlatFileItemReader<Customer> importReader1(@Value("#{jobParameters[fullPathFileName]}") String pathToFile) {
        FlatFileItemReader<Customer> reader = new FlatFileItemReader<>();
        reader.setResource(new FileSystemResource(pathToFile));
        reader.setLineMapper(new DefaultLineMapper<Customer>() {{
            System.out.println("#### importReader!!");
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames(new String[]{"custCustomerName3", stMainPhone","custMainEmail"});
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Customer>() {{
                setTargetType(Customer.class);
            }});
        }});
        return reader;
    }

    @Bean
    public CustomerItemProcessor processor() {
        System.out.println("#### CustomerItemProcessor processor!!"); 
        return new CustomerItemProcessor();
    }

    @Bean
    public JdbcBatchItemWriter<Customer> writer() {
        System.out.println("#### writer !!"); 
        JdbcBatchItemWriter<Customer> writer = new JdbcBatchItemWriter<>();
        writer.setItemSqlParameterSourceProvider(
            new BeanPropertyItemSqlParameterSourceProvider<Customer>());
        writer.setSql("INSERT INTO  customer (customer_name,customer_phone,comp_id,customer_email)"
            + " VALUES(:custCutomerName, :custMainPhone,:compId,:custMainEmail)");
        writer.setDataSource(dataSource);
        return writer;
    }

    @Bean
    public Job importUserJob(ItemReader<Customer> importReader) {
        System.out.println("#### importUserJob");       
        return jobBuilderFactory.get("importUserJob")
            .incrementer(new RunIdIncrementer())
            .flow(step1(importReader))
            .end()
            .build();
    }

    @Bean
    public Step step1(ItemReader<Customer> importReader) {
        System.out.println("#### public Step step1(ItemReader<Customer> importReader) TRIGGERED");
        return stepBuilderFactory.get("step1")
            .<Customer, Customer>chunk(10)
            .reader(importReader)
            .processor(processor())
            .writer(writer())
            .build();
    }

}

1 个答案:

答案 0 :(得分:1)