简体   繁体   English

在Spring Batch中更新FlatFileItemReader之后的文件

[英]Update file after FlatFileItemReader in Spring Batch

I currently have the following processing in a Spring Batch job: 我目前在Spring Batch作业中进行以下处理:

  • FlatFileItemReader reads a CSV file FlatFileItemReader读取CSV文件
  • Processor does some work 处理器做一些工作
  • FlatFileItemWriter creates a mirror of the read file, but updates the file to reflect processing FlatFileItemWriter创建读取文件的镜像,但更新文件以反映处理

I don't want to write to a new file, but I want to update the same file that is being read during processing. 我不想写入新文件,但是我想更新在处理过程中正在读取的相同文件。

My question is, is there a typical method in Spring to use the FlatFileItemReader and then update that same file per row in the processor at runtime? 我的问题是,Spring是否有一种典型的方法来使用FlatFileItemReader,然后在运行时在处理器中每行更新同一文件?

Thanks for any help. 谢谢你的帮助。

You can always write a custom writer in spring batch, just like an example below. 您始终可以在春季批处理中编写自定义编写器,就像下面的示例一样。 Where you read data form the file into memory, and then update the same file with the data that you are intended to. 您在哪里读取数据,将文件形成到内存中,然后使用所需的数据更新同一文件。

https://github.com/pkainulainen/spring-batch-examples/tree/master/spring/src/main/java/net/petrikainulainen/springbatch/custom/in https://github.com/pkainulainen/spring-batch-examples/tree/master/spring/src/main/java/net/petrikainulainen/springbatch/custom/in

more than that FlatFileItemReader is not thread safe. 不仅如此,FlatFileItemReader还不是线程安全的。 Of course there are hacks to achieve thread safety but not a good practice to use such hacks, its always good to create custom writer. 当然,有一些实现线程安全的技巧,但是使用此类技巧不是一个好习惯,这对于创建自定义编写器总是很好的。

Short answer is no, SB doesn't allow you to overwrite the same file you are reading from. 简短的答案是不,SB不允许您覆盖正在读取的文件。

A better pratice is to write an intermediate file and then perform a delete/rename. 更好的做法是编写一个中间文件,然后执行删除/重命名。

Write a temporary file is not a 'bad thing' especially if you are working with huge input file and OutOfMemoryException is round the corner; 编写一个临时文件不是一件坏事,特别是如果您正在使用巨大的输入文件并且OutOfMemoryException即将来临; also using a temporary file can make your step restartable and allow you to manually retrive translated file also if delete/rename process fails. 此外,如果删除/重命名过程失败,则使用临时文件还可以使您的步骤可重新启动,并允许您手动检索已翻译的文件。

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

相关问题 Spring 使用 FlatFileItemReader 批量空文件 xML - Spring Batch empty file using FlatFileItemReader xML Spring Batch:如何使用FlatFileItemReader读取CSV文件的页脚和验证 - Spring Batch : How to read footer of CSV file and validation using FlatFileItemReader 读取 CSV 中的换行符,这些换行符在 spring 批处理的 FlatfileItemReader 中的文件中引用 - Reading line breaks in CSV which are quoted in the file in FlatfileItemReader of spring batch Spring 批处理 FlatFileItemReader 令牌错误 - Spring batch FlatFileItemReader token error Spring Retry可以与Spring Batch FlatFileItemReader一起使用吗 - Can Spring Retry be used with Spring Batch FlatFileItemReader Spring Batch FlatFileItemReader 继续使用错误数量的令牌 - Spring Batch FlatFileItemReader continue on incorrect number of tokens Spring批处理FlatfileitemReader读取格式错误的行 - Spring batch FlatfileitemReader to read malformed lines Spring Batch FlatFileItemReader获取iput行 - Spring Batch FlatFileItemReader get iput line Spring Batch-用于不同分隔文件的FlatFileItemReader - Spring Batch - FlatFileItemReader for different delimited files 在 spring 批处理步骤中配置 openCSV 而不是 FlatFileItemReader - Configuring openCSV instead of FlatFileItemReader in spring batch step
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM