春季批处理作业未读取第一行

Posted

技术标签:

【中文标题】春季批处理作业未读取第一行【英文标题】:spring batch job not read first line 【发布时间】:2018-10-04 19:08:46 【问题描述】:

我有一个春季批处理作业来读取 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();
    


【问题讨论】:

How to skip lines with ItemReader in Spring-Batch?的可能重复 spring batch ItemReader FlatFileItemReader set cursor to start reading from a particular line or set linestoskip dynamically的可能重复 【参考方案1】:

您可以使用 FlatFileItemReader 的 setLinesToSkip 方法。

https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/file/FlatFileItemReader.html#setLinesToSkip-int-

【讨论】:

以上是关于春季批处理作业未读取第一行的主要内容,如果未能解决你的问题,请参考以下文章

未找到产品名称的数据库类型:春季批处理中的 [Apache Hive]

春季启动批处理到具有多个作业的春季云任务

动态创建作业的春季批处理测试

TransactionRequiredException:没有事务正在进行春季批处理作业和休眠

春季批处理中每个作业的不同Jobrepository数据源?

春季批处理:如果作业在最后 xx 分钟内失败,则向 grafana 和 prometheus 发出警报