我们可以在需要跳过第一行和最后一行的文件上使用Spring批处理项目读取器和写入器吗? [英] Can we use Spring batch Item Reader and Writer on a file which needs to skip first and last line?

查看:93
本文介绍了我们可以在需要跳过第一行和最后一行的文件上使用Spring批处理项目读取器和写入器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一些验证后,我有一个文件要写入db.该文件将包含Header和Trailer,需要先进行验证,然后再跳过它们,并且如果满足验证条件,则应将两者之间的所有行都映射并加载到db中.我可以使用Item Reader和Writer来做到这一点吗?下面是一个示例文件数据,该文件数据具有标题行,尾部行,并且在它们之间的行包含要加载到db的实际数据.感谢您的帮助.

I have a file to written to db after doing some validations. The file will have Header and Trailer which needs to be validated and then skipped, and all the lines in between should be mapped and loaded to a db if validations are met. Can I use Item Reader and Writer to do this? Below is a sample file data which has a header line, a trailer line and in between them the lines with actual data to be loaded to db. Any help is appreciated.

HEADER | xxxxx | 20190405T143025Z线数据|线数据|线数据|线数据| | |线数据|线数据|线数据|线数据|线数据|线数据|线数据|线数据|线数据|线数据|线数据|线数据|线数据|线数据|线数据|线数据|线数据|线数据|线数据linedata | linedata

HEADER|xxxxx|20190405T143025Z linedata|linedata|linedata|linedata|||linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata

TRAILER | 20190405T143025Z | 1

TRAILER|20190405T143025Z|1

p.s:我是IIB开发人员,这是我第一次使用spring batch.

p.s: I am an IIB Developer, this is my first time using spring batch.

推荐答案

您可以分两步分解需求:

You can break down the requirement in two steps:

  • 第1步:执行验证逻辑的简单Tasklet(查找标题/尾部记录并对其进行验证).此步骤的成功是下一步的前提.
  • 第2步:仅在第1步成功时才触发面向块的Tasklet,并使用 FlatFileItemReader.setLinesToSkip(1)跳过标头,并使用过滤记录开始的处理器跳过预告片使用 TRAILER .
  • Step 1: a simple tasklet that does the validation logic (looks for header/trailer records and validates them). The success of this step is a pre-condition for the next step.
  • Step 2: a chunk-oriented tasklet that is triggered only if step 1 succeeds, and which skips the header with FlatFileItemReader.setLinesToSkip(1) and skips the trailer with a processor that filters records starting with TRAILER.

这篇关于我们可以在需要跳过第一行和最后一行的文件上使用Spring批处理项目读取器和写入器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆