简体   繁体   English

Spring Batch FlatFileItemReader 继续使用错误数量的令牌

[英]Spring Batch FlatFileItemReader continue on incorrect number of tokens

I'm using Spring Batch FlatFileItemReader to parse csv files.我正在使用 Spring Batch FlatFileItemReader来解析 csv 文件。 Every now and then I get an ill-formated line and the application completely crashes with:我时不时地得到一条格式错误的行,应用程序完全崩溃:

Caused by: org.springframework.batch.item.file.transform.IncorrectTokenCountException: Incorrect number of tokens found in record: expected 11 actual 18

Is there any way to tell the FlatFileItemReader to continue (throw exception and continue or ignore and continue) without it completely exiting the application.有没有办法告诉FlatFileItemReader继续(抛出异常并继续或忽略并继续)而不完全退出应用程序。

I'm guessing I may need to extend the FlatFileItemReader to make this happen as there does not appear to be any setting for this.我猜我可能需要扩展 FlatFileItemReader 来实现这一点,因为似乎没有任何设置。 Any suggestions on how best to proceed and make this happen?关于如何最好地进行并实现这一目标的任何建议?

You can configure SkipLogic for your batch jobs Here's a link to doc您可以为批处理作业配置 SkipLogic 这是文档链接

Basically, if you are using Java Config to manage your Batch Job you can do something like this基本上,如果您使用 Java Config 来管理您的批处理作业,您可以执行以下操作

stepBuilderFactory.get("step1")
                .<Person, Person>chunk(10)
                .reader(reader)
                .writer(writer)
                .processor(processor)
                .faultTolerant()
                .skipLimit(10)
                .skip(RuntimeException.class)
                .listener(skipListener) // if you want to add
                .build();

I was able to solve this by creating a class that extended the DefaultLineMapper that gets injected into the FlatFileItemReader .我能够通过创建一个扩展DefaultLineMapper的类来解决这个问题,该类被注入到FlatFileItemReader

I then overrode mapLine method like this:然后我像这样覆盖了 mapLine 方法:

@Override
public T mapLine(String line, int lineNumber) throws Exception {
    T t = null;

    try {
        t = super.mapLine(line, lineNumber);
    } catch (Exception e) {
        log.error("Unable to parse line number <<{}>> with line <<{}>>.", lineNumber, line);
    }

    return t;
}

I'm not sure if this is the same as your issue, but I was getting the same error when parsing a caret "^" delimited file that contained quote " characters.我不确定这是否与您的问题相同,但是在解析包含引号字符的插入符号“^”分隔文件时,我遇到了同样的错误。

The " character is intended to be used to to extend a field across line endings or to enclose a String which contains the delimiter . So my line that looks like this: "字符旨在用于跨行结尾扩展字段或包含包含分隔符的字符串。所以我的行看起来像这样:

^3500 LCF Gas:  109" Wheelbase, Reg Cab^Chevrolet^3500 LCF Gas^109" Wheelbase, Reg Cab^L

would be parsed like it has only 2 fields:将被解析为只有 2 个字段:

  1. 3500 LCF Gas: 109" Wheelbase, Reg Cab^Chevrolet^3500 LCF Gas^109" Wheelbase, Reg Cab 3500 LCF Gas:109" 轴距,Reg Cab^Chevrolet^3500 LCF Gas^109" 轴距,Reg Cab
  2. L

What I really wanted is 5 fields:我真正想要的是 5 个字段:

  1. 3500 LCF Gas: 109" Wheelbase, Reg Cab 3500 LCF 汽油:109" 轴距,Reg 驾驶室
  2. Chevrolet雪佛兰
  3. 3500 LCF Gas 3500 LCF 气体
  4. 109" Wheelbase, Reg Cab 109" 轴距,Reg 驾驶室
  5. L

My file didn't contain any ampersands so I changed the DelimitedLinetokenizer default quoteCharacter from " to & .我的文件不包含任何 & 符号,所以我将DelimitedLinetokenizer默认quoteCharacter 从"更改为&

lineTokenizer.setQuoteCharacter('&');

Which fixed my issue that resulted in the same error when using Spring Batch.这解决了我在使用 Spring Batch 时导致相同错误的问题。

我设法通过将DefaultLineMapper的属性“ lineTokenizer ”的属性“ strict ”设置为false来解决这个问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM