从春季批处理中压缩来自数据库的CSV导出文件

时间:2018-08-16 17:42:27

标签: java spring-boot spring-batch

我有一个在启动spring boot应用程序后运行的批处理(未配置启动程序,仅由于spring.batch.job.enabled=true我的工作正在运行)。 我的工作目的是从数据库中导出csv文件。

创建CSV文件后,我想压缩该文件。 我可以在其中压缩的步骤中,在itemWriter中尝试过,压缩后的文件为空,

我的源代码是:

@Bean
public FlatFileItemWriter<User> writer(){
    log.info("start writing user ... ");
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.systemDefault());
    Instant instant = Instant.now();
    date = formatter.format(instant);
    date = date.replaceAll("[/,\\s,:]", "-");
    FlatFileItemWriter<User> writer = new FlatFileItemWriter<User>();
    filePath = config.getPath + date + ".csv";
    writer.setResource(new FileSystemResource (filePath));
    writer.setAppendAllowed(true);
    writer.setLineAggregator(new DelimitedLineAggregator<User>() {
        {
            setDelimiter(",");
            setFieldExtractor(new BeanWrapperFieldExtractor<User>() {
                {
                    setNames(new String[] { "Id", "Name" });
                }
            });
        }
    });
    writer.close();

    try {
        File file = new File(config.getPath() + "\\" + date + ".zip");
        ZipOutputStream zop = new ZipOutputStream(new FileOutputStream (file));
        ZipEntry entry = new ZipEntry(filePath);
        zop.putNextEntry(entry);
        zop.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return writer;
}

1 个答案:

答案 0 :(得分:0)

  

我可以在其中压缩的步骤中,在itemWriter中尝试过,压缩后的文件为空,

您需要创建两个步骤:

  • 一个创建文件的文件(面向块的步骤)
  • 另一个将其压缩的文件(tasklet步骤)

您的配置不正确。您不需要调用writer.close,Spring Batch会在编写者生命周期中进行调用。您需要做的是声明一个ItemWriter类型的bean,并将邮政编码移到另一个步骤。例如:

@Bean
public JdbcCursorItemReader<User> jdbcCursorItemReader() {
    // define and return the reader
}

@Bean
public FlatFileItemWriter<User> writer(){
    log.info("start writing user ... ");
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.systemDefault());
    Instant instant = Instant.now();
    date = formatter.format(instant);
    date = date.replaceAll("[/,\\s,:]", "-");
    FlatFileItemWriter<User> writer = new FlatFileItemWriter<User>();
    filePath = config.getPath + date + ".csv";
    writer.setResource(new FileSystemResource (filePath));
    writer.setAppendAllowed(true);
    writer.setLineAggregator(new DelimitedLineAggregator<User>() {
        {
            setDelimiter(",");
            setFieldExtractor(new BeanWrapperFieldExtractor<User>() {
                {
                    setNames(new String[] { "Id", "Name" });
                }
            });
        }
    });

    return writer;
}

@Bean
public Step exportFile() {
    return stepBuilderFactory.get("exportFile")
            .chunk(100)
            .reader(jdbcCursorItemReader())
            .writer(writer())
            .build();
}

@Bean
public Step compressFile() {
    return stepBuilderFactory.get("compressFile")
            .tasklet(new Tasklet() {
                @Override
                public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                    try {
                        File file = new File(config.getPath() + "\\" + date + ".zip");
                        ZipOutputStream zop = new ZipOutputStream(new FileOutputStream (file));
                        ZipEntry entry = new ZipEntry(filePath);
                        zop.putNextEntry(entry);
                        zop.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return RepeatStatus.FINISHED;
                }
            });
}

然后按照正确的顺序定义这些步骤的工作。