[英]Update file after FlatFileItemReader in Spring Batch
I currently have the following processing in a Spring Batch job: 我目前在Spring Batch作业中进行以下处理:
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.