如何在 Spring Batch 中使用 FlatFileItemWriter 写入标准输出?

发布于 2024-12-03 16:31:04 字数 1242 浏览 0 评论 0原文

我在 Spring Batch 项目的 beans 定义文件中配置了以下编写器:

<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>

现在,我希望输出转到标准输出,而不是写入 /path/to/somefile ,原因是我想通过启动此作业命令行启动器并将输出通过管道传输到另一个 UNIX 程序。

我尝试将资源属性设置为“file:/dev/stdout”,但随后出现异常: org.springframework.batch.item.ItemStreamException:无法创建文件:[/dev/stdout]

我试图看看是否有一个开箱即用的资源可以处理这个问题,但我有点不知道哪个可以完成工作...

感谢您的帮助。

编辑:以下是我根据您的建议提出的解决方案:

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 have the following writer configured in my beans definition file of a spring batch project :

<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.

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]

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));
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮倦 2024-12-10 16:31:04

我认为没有任何现成的东西可以满足您的需求。创建您自己的 ItemWriter 来执行简单的 System.out.println() 非常简单。然后,您可以使用 CompositeItemWriter 如果您仍然需要该文件,请将两者捆绑在一起。

或者,您可以使用自定义 ItemWriterListener 并在那里进行打印。

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(). You can then use the CompositeItemWriter to tie the two together if you still want the file.

Alternately, you can throw on a custom ItemWriterListener and do the printing there.

反话 2024-12-10 16:31:04

您可以使用另一种技巧来写入标准输出。本质上,您可以指定 /dev/stdout 作为输出文件资源。请务必在编写器中将 shouldDeleteIfExists 设置为 false 并将 appendAllowed 设置为 true,否则您将收到运行时错误。

There's another hack you can use to write to stdout. Essentially, you can specify /dev/stdout as the output file resource. Be sure to set shouldDeleteIfExists to false and appendAllowed to true within the writer or you'll get runtime errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文