【问题标题】:spring batch : IncorrectTokenCountException春季批次:IncorrectTokenCountException
【发布时间】:2018-05-07 13:15:05
【问题描述】:

我有平面文件,.csv 文件它有 5 个五个字段。如果所有五个字段都存在,则文件处理运行良好。但是如果不小心,如果5个字段没有输入,如下所示

d,e,f,g,h a,b,c d,e,f,g,h

程序崩溃。我想继续阅读到下一行。

错误是org.springframework.batch.item.file.transform.IncorrectTokenCountException: Incorrect number of tokens found in record

下面是代码。

如何继续阅读到第三行?我希望读取第一行,跳过第二行,读取第三行。

FlatFileItemReader<CustomerBatch> reader = new FlatFileItemReader<>();
DelimitedLineTokenizer delimitedTokeniser = new DelimitedLineTokenizer();
delimitedTokeniser.setNames(new String[]{ "a", "b","c","d","e" });
DefaultLineMapper<CustomerBatch> lineMapper = new DefaultLineMapper<CustomerBatch>();
lineMapper.setLineTokenizer(delimitedTokeniser);
lineMapper.setFieldSetMapper(new BeanWrapperFieldSetMapper<CustomerBatch>() {{setTargetType(CustomerBatch.class);}});
ByteArrayResource y= new ByteArrayResource(batchFile.getBytes());
reader.setResource(y);
reader.setLineMapper(lineMapper);
reader.setLinesToSkip(1); //do not read the first line of the csv file.
reader.open(new ExecutionContext());

try {
        while ((newCustomerBatch = reader.read()) != null) {
        System.out.println("name:"+newCustomerBatch.getName());
        //some logic.
        }
    }catch(Exception e) {

    }
}

【问题讨论】:

标签: java spring-boot spring-batch


【解决方案1】:

您有一个名为 skipPolicy 的功能,它可以处理您愿意容忍多少 ParseException,直到批处理失败。

您应该实现 SkipPloicy 接口:

@Override
    public boolean shouldSkip(Throwable exception, int skipCount) throws SkipLimitExceededException {
        if (exception instanceof FileNotFoundException) {
            return false;
        } else if (exception instanceof FlatFileParseException && skipCount <= 5) {
            FlatFileParseException ffpe = (FlatFileParseException) exception;
            StringBuilder errorMessage = new StringBuilder();
            errorMessage.append("An error occured while processing the " + ffpe.getLineNumber()
                    + " line of the file. Below was the faulty " + "input.\n");
            errorMessage.append(ffpe.getInput() + "\n");
            logger.error("{}", errorMessage.toString());
            return true;
        } else {
            return false;
        }
    }

更多关于跳过异常的信息可以查看here

【讨论】:

  • 谢谢!我将如何将其集成到我的上述代码中?
猜你喜欢
  • 2014-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 2013-06-23
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多