如何使用Spring批处理用空体写csv文件

时间:2022-04-10 02:04:11

Im using spring boot to write my outbound batch which one of the requirement is to send and empty file with header only if there is no record found.

我使用spring boot来编写我的出站批处理,其中一个要求是发送并且只有在没有找到记录时才用标题清空文件。

im using BeanPropertyRowMapper to map my JdbcCursorItemReader... why im using BeanPropertyRowMapper because there is alot column to map. if im using rowmapper it will be hell for me lol. this is the configuration :

我使用BeanPropertyRowMapper映射我的JdbcCursorItemReader ...为什么我使用BeanPropertyRowMapper因为有很多列要映射。如果我使用rowmapper它将是我的地狱大声笑。这是配置:

    @Bean
public Step RawDataOutBoundSteps() throws IOException {

    return stepBuilderFactory.get("step1").<RawDataExtractionDTO, RawDataExtractionDTO>chunk(1000)//.faultTolerant()
            //.skip(Exception.class)
            //.skipLimit(1000)
            .reader(myDBReader()).processor(myProcessor())
            .writer(myWriter()).build();
}

the problem is, if myDBreader() queries return empty resultset, my code wont go to myProcessor hence myWriter also.

问题是,如果myDBreader()查询返回空结果集,我的代码也不会转到myProcessor,因此myWriter也是如此。

myProcessor :

public class RawDataJobProcessor implements 
ItemProcessor<RawDataExtractionDTO, RawDataExtractionDTO>{

@Override
public RawDataExtractionDTO process(RawDataExtractionDTO item) throws Exception {
    System.out.println(" Raw DAta Process");

    if(item == null )
    return null;

    return item;
}}

my DB reader :

我的数据库读者:

    @Bean
ItemReader<RawDataExtractionDTO> rawDataDBReader() {

    String jobName = Application.jobName;
    String sql_query = "";

    if("RawDataOutBoundweekly".equalsIgnoreCase(jobName)){
        log.info("=========RawDataOutBoundweekly=========");

        sql_query = "select * from vw_rawdata_weekly";

    }else if("RawDataOutBoundmonthly".equalsIgnoreCase(jobName)){
        log.info("================RawDataOutBoundmonthly=============");

        sql_query = "select * from vw_rawdata_monthly";
    }
    log.info("ENTERING rawDataDBReader  ===================");


    JdbcCursorItemReader<RawDataExtractionDTO> datareader = new 
    JdbcCursorItemReader<>();
    datareader.setSql(sql_query);
    datareader.setDataSource(dataSource);
    datareader.setRowMapper(new BeanPropertyRowMapper<>
    (RawDataExtractionDTO.class));
    return datareader;
}

no error throw, but if i put debug point after my reader it wont go to my processor at all, it just shutdown the job.

没有错误抛出,但如果我把调试点放在我的阅读器后它根本不会进入我的处理器,它只是关闭了工作。

1 个解决方案

#1


0  

You can implement FlatFileHeaderCallback to write header of file.

您可以实现FlatFileHeaderCallback来编写文件头。

http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/file/FlatFileHeaderCallback.html

Some sample code to configure FlatFileHeaderCallback. Even reader does not read any record FlatFileHeaderCallback will be called and will write header. final file will have header only

一些示例代码用于配置FlatFileHeaderCallback。即使读者没有读取任何记录,也会调用FlatFileHeaderCallback并写入标题。最终文件只有标题

@Bean
    public FlatFileItemWriter<Person> myWriter()
    {
        System.out.println("FlatFileItemWriter*******************");
        FlatFileItemWriter<Person> writer = new FlatFileItemWriter<Person>();
        writer.setResource(new FileSystemResource("output.csv"));
        DelimitedLineAggregator<Person> delLineAgg = new DelimitedLineAggregator<Person>();
        delLineAgg.setDelimiter(",");
        BeanWrapperFieldExtractor<Person> fieldExtractor = new BeanWrapperFieldExtractor<Person>();
        fieldExtractor.setNames(new String[] {"firstName", "lastName"});
        delLineAgg.setFieldExtractor(fieldExtractor);
        writer.setLineAggregator(delLineAgg);

   writer.setHeaderCallback(myFlatFileHeaderCallback);      


        return writer;
    }


@Component
public class MyFlatFileHeaderCallback implements FlatFileHeaderCallback {

    @Override
    public void writeHeader(Writer writer) throws IOException {
        System.out.println("Header called");

    }

}

#1


0  

You can implement FlatFileHeaderCallback to write header of file.

您可以实现FlatFileHeaderCallback来编写文件头。

http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/file/FlatFileHeaderCallback.html

Some sample code to configure FlatFileHeaderCallback. Even reader does not read any record FlatFileHeaderCallback will be called and will write header. final file will have header only

一些示例代码用于配置FlatFileHeaderCallback。即使读者没有读取任何记录,也会调用FlatFileHeaderCallback并写入标题。最终文件只有标题

@Bean
    public FlatFileItemWriter<Person> myWriter()
    {
        System.out.println("FlatFileItemWriter*******************");
        FlatFileItemWriter<Person> writer = new FlatFileItemWriter<Person>();
        writer.setResource(new FileSystemResource("output.csv"));
        DelimitedLineAggregator<Person> delLineAgg = new DelimitedLineAggregator<Person>();
        delLineAgg.setDelimiter(",");
        BeanWrapperFieldExtractor<Person> fieldExtractor = new BeanWrapperFieldExtractor<Person>();
        fieldExtractor.setNames(new String[] {"firstName", "lastName"});
        delLineAgg.setFieldExtractor(fieldExtractor);
        writer.setLineAggregator(delLineAgg);

   writer.setHeaderCallback(myFlatFileHeaderCallback);      


        return writer;
    }


@Component
public class MyFlatFileHeaderCallback implements FlatFileHeaderCallback {

    @Override
    public void writeHeader(Writer writer) throws IOException {
        System.out.println("Header called");

    }

}