Spring Batch - 基于字段值的多个编写器 [英] Spring Batch - more than one writer based on field value
问题描述
我正在使用Spring批处理,对于当前使用FlatFileItemWriter的编写器。
I am working on spring batch, for writer currently using FlatFileItemWriter.
我想根据某些字段将输入文件内容写入多个平面文件值。 Spring批处理默认是支持任何类型的功能。[类似于CompositeItemWriter]
I would like to write my input file content to more than one flat file based on some field value. Is Spring batch support any kind of functionality by default.[something similar to CompositeItemWriter]
例如,我的输入文件内容是这样的。
For example, my input file content is something like this.
john,35,retail,10000
joe,34,homeloan,20000
Amy,23,retail,2000
现在我想基于第三列写两个不同的文件,这意味着第1行和第3行应该去file1和row2应该转到file2。
Now i would like to write two different files based on third column, it means row 1 and row 3 should go to file1 and row 2 should go to file2.
我的编写器配置是:
<bean id="fileWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:C:/output.dat"/>
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value="|" />
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="field1,field2...." />
</bean>
</property>
</bean>
</property>
</bean>
推荐答案
看看 ClassifierCompositeItemWriter
。这个 ItemWriter
实现允许你定义一个分类器
,它选择哪个定义的委托 ItemWriter
委托的实例。在您的情况下,您将创建一个基于field4决定的分类器
并将写入委托给 FlatFileItemWriter的适当实例
。
Take a look at the ClassifierCompositeItemWriter
. This ItemWriter
implementation allows you to define a Classifier
that chooses which of the defined delegate ItemWriter
instances to delegate to. In your case, you'd create a Classifier
that decided based on field4 and delegate the writing to the appropriate instance of the FlatFileItemWriter
.
您可以在此处的文档中阅读有关 ClassifierCompositeItemWriter
的更多信息: http://docs.spring.io/spring-batch/ trunk / apidocs / org / springframework / batch / item / support / ClassifierCompositeItemWriter.html
You can read more about the ClassifierCompositeItemWriter
in the documentation here: http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/support/ClassifierCompositeItemWriter.html
这篇关于Spring Batch - 基于字段值的多个编写器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!