简体   繁体   English

如何在 spring 批处理中的单个项目阅读器中读取逗号分隔和 pipe 行分隔的 csv 文件

[英]how to read both comma separated and pipe line separated csv file in a single item reader in spring batch

I am new to sprig batch.我是新来的小枝批。 I have a folder which contain multiple csv file, I have implemented MultiResourceItemReader () to read those file.我有一个包含多个 csv 文件的文件夹,我已经实现了 MultiResourceItemReader () 来读取这些文件。 It is working only if all csv file are pipe line ("|") separated.只有当所有 csv 文件都被 pipe 行(“|”)分隔时,它才有效。

I want to read both comma (",") separated csv and pipe line separated csv using single reader.我想使用单个阅读器读取逗号 (",") 分隔的 csv 和 pipe 行分隔的 csv 。 Is it possible?可能吗? if yes how?如果是的话怎么办?

Here is my code这是我的代码

@Bean
@StepScope
public MultiResourceItemReader<Person> multiResourceItemReader(@Value("#{jobParameters[x]}") String x,@Value("#{jobParameters[y]}") String y,@Value("#{jobParameters[z]}") String z) {

    Resource[] resourcessss = null;
    ClassLoader cl = this.getClass().getClassLoader();
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
    try {
        resourcessss = resolver.getResources("file:" + z);
    }catch(Exception e) {

    }
    MultiResourceItemReader<Person> resourceItemReader = new MultiResourceItemReader<Person>();
    resourceItemReader.setResources(resourcessss);
    resourceItemReader.setDelegate(reader());

    return resourceItemReader;
}



@Bean
public FlatFileItemReader<Person> reader() {
    FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
    reader.setLineMapper(new DefaultLineMapper() {
        {
            setLineTokenizer(new DelimitedLineTokenizer() {
                {
                    setNames(new String[]{"Id","postCode"});
                }
                {
                    setDelimiter("|");
                }
            });
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {
                {
                    setTargetType(Person.class);
                }
            });
        }
    });
    return reader;
}

Take a look at the PatternMatchingCompositeLineTokenizer .看看PatternMatchingCompositeLineTokenizer There, you can use a Pattern to identify what records get parsed by what LineTokenizer .在那里,您可以使用Pattern来识别LineTokenizer解析了哪些记录。 In your case, you'd have one Pattern that identifies comma delimited records and map them to the tokenizer that parses via commas.在您的情况下,您将有一个Pattern来标识逗号分隔的记录,并将 map 它们发送到通过逗号解析的标记器。 You'd also have a Pattern that identifies records delimited by pipes and maps those to the appropriate LineTokenizer .您还将有一个Pattern来识别由管道分隔的记录并将它们映射到适当的LineTokenizer It would look something like this:它看起来像这样:

    @Bean
    public LineTokenizer compositeLineTokenizer() throws Exception {
        DelimitedLineTokenizer commaTokenizer = new DelimitedLineTokenizer();

        commaTokenizer.setNames("a", "b", "c");
        commaTokenizer.setDelimiter(",");
        commaTokenizer.afterPropertiesSet();

        DelimitedLineTokenizer pipeTokenizer = new DelimitedLineTokenizer();

        pipeTokenizer.setNames("a", "b", "c");
        pipeTokenizer.setDelimiter("|");

        pipeTokenizer.afterPropertiesSet();

        // I have not tested the patterns here so they may need to be adjusted
        Map<String, LineTokenizer> tokenizers = new HashMap<>(2);
        tokenizers.put("*,*", commaTokenizer);
        tokenizers.put("*|*", pipeTokenizer);

        PatternMatchingCompositeLineTokenizer lineTokenizer = new PatternMatchingCompositeLineTokenizer();

        lineTokenizer.setTokenizers(tokenizers);

        return lineTokenizer;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java Eclipse中读取Excel CSV文件(逗号分隔值)? - How to read a excel CSV file (comma separated value) in Java Eclipse? 如何用逗号将逗号分隔的行(CSV)解析为某些项目? - How to parse a comma separated line (CSV) with some items in quotation marks? 当几列中有行分隔值而不是逗号分隔时,如何拆分 csv 文件 - How to split csv file when there is line seperated value in few columns instead of comma separated 春季批处理中如何使用FlatFileItemReader处理以空格分隔的项目(单行中有多个记录)? - How to use FlatFileItemReader in spring batch for items separated by space (multiple records on single line)? 如何从命令行作为参数输入的文本文件中读取逗号分隔的字符串? - How do I read in comma separated string from text file that is inputted from command-line as argument? 如何在Java中解析CSV(excel,不用逗号分隔)文件? - how can I parse CSV(excel,not separated by comma) file in Java ? 从用逗号分隔的文件中读取int - Read int from file separated by comma 如何在java中读取逗号分隔的整数输入 - How to read comma separated integer inputs in java 如何将文件名输入到 spring 批次的项目阅读器或项目处理器中? - How to get File Name in to the Item Reader or Item Processor of spring batch? 如何使用 map 读取 CSV 文件,其中列表带有 Java 中的分隔元素 - How to read the CSV file with a map with lists with separated elements in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM