[英]How can I write to stdout using FlatFileItemWriter in spring batch?
I have the following writer configured in my beans definition file of a spring batch project : 我在spring批处理项目的bean定义文件中配置了以下编写器:
<bean id="writer" class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:/path/to/somefile"/>
<property name="lineAggregator">
<bean class="MyCustomLineAggregator"/>
</property>
</bean>
Now, instead of writing to /path/to/somefile, I want the output to go to the stdout, reason being that I want to launch this job through the command-line launcher and pipe the output to another unix program. 现在,我不希望写入/ path / to / somefile,而是希望输出转到stdout,原因是我想通过命令行启动器启动此作业并将输出传递给另一个unix程序。
I tried by setting the resource property to "file:/dev/stdout", but then I get the exception : org.springframework.batch.item.ItemStreamException: Unable to create file: [/dev/stdout] 我尝试将资源属性设置为“file:/ dev / stdout”,但后来我得到了异常:org.springframework.batch.item.ItemStreamException:无法创建文件:[/ dev / stdout]
I tried to see if there was an out of the box resource that could handle this, but I'm a bit clueless on which one could do the job... 我试着看看是否有一个开箱即用的资源可以解决这个问题,但我对于哪个人可以完成这项工作有点无能为力......
Thanks for your help. 谢谢你的帮助。
EDIT : Below is the solution I came up with, following your advise : 编辑:以下是我提出的解决方案,遵循您的建议:
import java.util.List;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.transform.LineAggregator;
public class StdoutWriter<T> implements ItemWriter<T> {
LineAggregator<T> lineAggregator;
public void setLineAggregator(LineAggregator<T> lineAggregator) {
this.lineAggregator = lineAggregator;
}
@Override
public void write(List<? extends T> items) throws Exception {
for (T item : items) {
System.out.println(lineAggregator.aggregate(item));
}
}
}
I don't think there is anything out of the box that does what you are looking for. 我不认为开箱即用可以满足您的需求。 It is simple enough to create your own
ItemWriter
that does a simple System.out.println()
. 创建一个简单的
System.out.println()
创建自己的ItemWriter
非常简单。 You can then use the CompositeItemWriter
to tie the two together if you still want the file. 然后,如果您仍然需要该文件,则可以使用
CompositeItemWriter
将两者绑定在一起。
Alternately, you can throw on a custom ItemWriterListener
and do the printing there. 或者,您可以抛出自定义
ItemWriterListener
并在那里进行打印。
There's another hack you can use to write to stdout. 你可以使用另一个hack写入stdout。 Essentially, you can specify
/dev/stdout
as the output file resource. 实际上,您可以将
/dev/stdout
指定为输出文件资源。 Be sure to set shouldDeleteIfExists
to false
and appendAllowed
to true
within the writer or you'll get runtime errors. 一定要设置
shouldDeleteIfExists
以false
和appendAllowed
到true
作家之内,否则你会得到运行时错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.