在 spring 批处理步骤中配置 openCSV 而不是 FlatFileItemReader [英] Configuring openCSV instead of FlatFileItemReader in spring batch step

查看:47
本文介绍了在 spring 批处理步骤中配置 openCSV 而不是 FlatFileItemReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 spring 批处理中的 reader() 步骤中配置 openCSV,以直接将从 CSV 文件读取的记录转换为 JAVA POJO.但是我遇到了如何使用 openCSV 正确设置 lineMapper 的问题.

I am trying to configure openCSV in the reader() step in the spring batch to directly convert a record read from a CSV file into a JAVA POJO. But I am running into the issue of how to correctly set the lineMapper with the openCSV.

正如此处链接的帖子中所建议的 如何替换 flatFileItemReader在春季批次中使用 openCSV,我正在尝试如下:

As suggested in the post linked here How to replace flatFileItemReader with openCSV in spring batch, I am trying as below:

public Event reader() throws IOException {
        FlatFileItemReader<Event> itemReader = new FlatFileItemReader<Event>();
        itemReader.setLineMapper(lineMapper());
        itemReader.setLinesToSkip(1);
        itemReader.setResource(new FileSystemResource(inputFilePath));
        return itemReader;
    }

但我不知道如何配置 lineMapper:

But I am not able to figure out how to configure the lineMapper:

    public LineMapper<Event> lineMapper() throws IOException {
       DefaultLineMapper<Event> lineMapper = new DefaultLineMapper<Event>();
       DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer("\t");
       BeanWrapperFieldSetMapper<Event> fieldSetMapper = new BeanWrapperFieldSetMapper<Event>();
       fieldSetMapper.setTargetType(Event.class);
       lineMapper.setLineTokenizer(???);
       lineMapper.setFieldSetMapper(???);

我有代码来读取文件并将其转换为所需的 POJO 但是把它放在哪里:

I have the code to read the file and convert it to the desired POJO but where to put it:

        try (
                Reader reader = Files.newBufferedReader(Paths.get(inputFilePath));
        ) {
            CsvToBean<Event> csvToBean = new CsvToBeanBuilder(reader)
                    .withSkipLines(1)
                    .withType(Event.class)
                    .withIgnoreLeadingWhiteSpace(true)
                    .build();
            return csvToBean.iterator().next();
        }

非常感谢为我指明正确方向的任何帮助.

Any help to point me in the right direction is highly appreciated.

推荐答案

您正在使用 DefaultLineMapper 并尝试设置 LineTokenizerFieldSetMapper> 在其中,但这不是您分享的链接中提到的内容.

You are using the DefaultLineMapper and trying to set a LineTokenizer and FieldSetMapper in it, but this is not what is mentioned in the link you shared.

您需要一个基于 OpenCSV 的 LineMapper 接口的自定义实现:

You need a custom implementation of the LineMapper interface that is based on OpenCSV:

public class OpenCSVLineMapper<T> implements LineMapper<T> {
    
    @Override
    public T mapLine(String line, int lineNumber) throws Exception {
        // TODO use OpenCSV to map a line to a POJO of type T
        return null;
    }
}

OpenCSV 提供 API 来读取文件并将数据映射到对象.您不需要读取部分,因为这将由 Spring Batch 的 FlatFileItemReader 完成,您只需要将 OpenCSV 用于映射部分.

OpenCSV provides APIs to both read the file and map data to objects. You don't need the reading part as this will be done by the FlatFileItemReader from Spring Batch, you only need to use OpenCSV for the mapping part.

一旦到位,您就可以在 FlatFileItemReader 上设置基于 OpenCSV 的线映射器实现:

Once this in place, you can set your OpenCSV based line mapper implementation on the FlatFileItemReader:

public FlatFileItemReader<Event> reader() throws IOException {
   FlatFileItemReader<Event> itemReader = new FlatFileItemReader<Event>();
   itemReader.setResource(new FileSystemResource(inputFilePath));
   itemReader.setLinesToSkip(1);
   itemReader.setLineMapper(new OpenCSVLineMapper<>());
   return itemReader;
}

这篇关于在 spring 批处理步骤中配置 openCSV 而不是 FlatFileItemReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆