SpringBatch 访问项目处理器中的行号

huangapple 未分类评论46阅读模式
英文:

SpringBatch accessing Line Number in Item processor

问题

我正在读取一个包含成千上万条记录的 CSV 文件,对其进行处理,并将记录保存在数据库中。我在 ItemPreocessor 中进行验证,并将错误写入文件。我需要按以下格式写入带有错误的记录的行号:

行号 字段 错误
4 年龄 负数
5 类型 无效
5 邮编 无效

我该如何在 ItemProcessor 中获取行号?非常感谢您的任何帮助。文件中没有唯一标识符,因此显示行号是必需的,它来自外部来源。

谢谢,
GAR

英文:

I am reading a CSV file with thousands of records, processing it and saving records in DB, I am doing validations in ItemPreocessor and writing errors to a file, I need to write line number of the record with the errors as below

LineNumber field error
4 age negative
5 type Invalid
5 postcode Invalid

How can I get hold of Line number in ItemProcessor, any help is much appreciated.There is no unique identifier in file so showing the line number is mandatory, it is coming from external source.

Thanks,
GAR

答案1

得分: 0

也许你可以检查所有可用的注解,并创建一个适合你需求的流程(链接:https://docs.spring.io/spring-batch/docs/current/api/org/springframework/batch/core/annotation/package-summary.html)。
例如,以下处理器将在分块处理数据之前将lineNumber初始化为0。然而,如果文件包含的内容超过了分块大小,你应该在执行上下文中存储已执行的分块数量,然后添加到lineNumber。

public class RecordProcessors implements ItemProcessor<Record, List<Record>> {
  private int lineNumber;
  
  @BeforeChunk
  public void init() {
    lineNumber = 0;
  }
  
  @Override
  public List<Record> process(Record record) {
    // 进行验证
    record.setLineNumber(lineNumber);
    return record;
  }
}
英文:

Maybe you can check all the annotation available and create a flow that fit your requirements (https://docs.spring.io/spring-batch/docs/current/api/org/springframework/batch/core/annotation/package-summary.html).
For example the following processor will init the lineNumber to 0 before the chunk proccess the data. However if the file contains more than the chunk size, you should store the number of chunk that have been executed in the execution context and then add to the lineNumber.

public class RecordProcessors implements ItemProcessor&lt;Record , List&lt;Record &gt;&gt; {
  private int lineNumber;
  @BeforeChunk
  public void init() {

    lineNumber = 0;
  }

  @Override
  public List&lt;Record&gt; process(Record record) {

    // do validation
    record.setLineNumber(lineNumber)
    return record;
  }
}

huangapple
  • 本文由 发表于 2020年7月25日 17:47:16
  • 转载请务必保留本文链接:https://java.coder-hub.com/63086847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定