简体   繁体   English

如何在Spring批处理中使用FlatFileItemWriter写入stdout?

[英]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. 一定要设置shouldDeleteIfExistsfalseappendAllowedtrue作家之内,否则你会得到运行时错误。

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

相关问题 Spring 批量 FlatFileItemWriter 从 Object 写入 csv - Spring batch FlatFileItemWriter write as csv from Object 为什么在春季批处理中不使用FlatFileItemWriter创建文件? - Why file not creating using FlatFileItemWriter in spring batch? Spring Batch-FlatFileItemWriter写入文件,关闭该文件,写入新文件 - Spring Batch - FlatFileItemWriter write to a file, close it, write to new file Spring Batch FlatFileItemWriter - 输出自定义 - Spring Batch FlatFileItemWriter - output customization 使用批处理作业的FlatFileItemReader和FlatFileItemWriter - FlatFileItemReader and FlatFileItemWriter Using Batch Job Spring Batch:无法访问flatfileitemwriter中的jobexecutionConext - Spring Batch: not able to access jobexecutionConext in flatfileitemwriter FlatFileItemWriter在Spring批处理中在异常时生成空白文件 - FlatFileItemWriter generating blank file on exception in Spring batch 我应该如何使用Spring Batch编写结果文件 - How should I write a results file using Spring Batch 当我在春季批处理中将FlatFileItemWriter配置为具有scpoe =“ step”的流时,java.lang.IllegalStateException - java.lang.IllegalStateException when i configure FlatFileItemWriter as stream with scpoe=“step” in spring batch 如何在使用 Spring Batch FlatFileItemWriter 写入 csv 文件时跳过空白行 - how to skip blank lines while writing to csv file with Spring Batch FlatFileItemWriter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM