【问题标题】:MultiResourceItemReader - Skip entire file if header is invalidMultiResourceItemReader - 如果标题无效,则跳过整个文件
【发布时间】:2020-01-28 02:59:36
【问题描述】:

我的 Spring Batch 作业读取包含两种类型标头的 csv 文件列表。如果文件的标题与两种可能的标题类型之一不匹配,我希望读者跳过整个文件。

我看过Spring Boot batch - MultiResourceItemReader : move to next file on error。 但我看不到如何验证标头令牌以确保它们在计数和内容上匹配

【问题讨论】:

    标签: java spring-batch


    【解决方案1】:

    我可以通过以下方式解决这个问题,

    public FlatFileItemReader<RawFile> reader() {
        return new FlatFileItemReaderBuilder<RawFile>()
                .skippedLinesCallback(line -> {
                    // Verify file header is what we expect
                    if (!StringUtils.equals(line, header)) {
                        throw new IllegalArgumentException(String.format("Bad header!", line));
                    }
                })
                .name( "myReader" )
                .linesToSkip( 1 )
                .lineMapper( new DefaultLineMapper() {
                    {
                        setLineTokenizer( lineTokenizer );
                        setFieldSetMapper( fieldSetMapper );
                    }} )
                .build();
    }
    

    在我的MultiResourceItemReader 中设置委托时,我调用了reader() 方法。

    注意 headerlineTokenizerfieldSetMapper 都是我设置的变量,具体取决于我的作业应该读取的文件类型(以及相应的标题集) .

    【讨论】:

      【解决方案2】:

      我们可以在基于 XML 的配置中做到这一点吗?

      【讨论】:

      • 你好!我看到这是您对问题的第一个答案。当您对作者有疑问时,最好对问题发表评论而不给出答案。当您知道问题的解决方案时,答案是最好的。欢迎并感谢您!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 2022-11-15
      • 2019-03-04
      • 1970-01-01
      • 2021-12-01
      • 2013-10-08
      相关资源
      最近更新 更多