问题描述
批处理作业是:
-
从csv文件中读取
Reading from a csv file
为csv中的每个记录(行)创建一个xml文件,名称为Patent.ID.xml(其中ID是csv中的字段,Patent是我的模型类),例如:1.xml,2 .xml
Create an xml file for every record(line) in the csv with name Patent.ID.xml(where ID is a field in the csv and Patent is my model class), example: 1.xml, 2.xml
问题是我找不到动态设置文件名到每个ID的方法来自csv文件
The problem is that I can't find a way to dynamically set the file-name to each ID from the csv file
这是我的配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- My beans -->
<bean id="patent" class="com.example.model.Patent" scope="prototype" />
<bean id="xmlsuffix" class="com.example.filename.PatentFileSuffixCreator"/>
<bean id="noInputException" class="com.example.listener.NoWorkFoundStepExecutionListener"/>
<batch:job id="csvtoxml">
<batch:step id="step1">
<batch:tasklet>
<batch:chunk reader="fileReader" writer="multiResourceItemWriter" commit-interval="1">
</batch:chunk>
<batch:listeners>
<batch:listener ref="noInputException"/>
</batch:listeners>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="multiResourceItemWriter"
class="org.springframework.batch.item.file.MultiResourceItemWriter">
<property name="resource" value="file:xml/#{patent.ID}" />
<property name="delegate" ref="XMLwriter"/>
<property name="itemCountLimitPerResource" value="1"/>
<property name="resourceSuffixCreator" ref="xmlsuffix"/>
</bean>
<bean id="XMLwriter"
class="org.springframework.batch.item.xml.StaxEventItemWriter">
<property name="marshaller" ref="patentUnmarshaller" />
<property name="rootTagName" value="Patents" />
</bean>
<bean id="patentUnmarshaller"
class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<map>
<entry key="Patent"
value="com.example.model.Patent" />
</map>
</property>
</bean>
<bean id="fileReader"
class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="lineMapper" ref="lineMapper"/>
<property name="resource" value="file:dbbrev_sample_m.csv"/>
<property name="linesToSkip" value="1"/>
</bean>
<bean id="lineMapper"
class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="fieldSetMapper" ref="fieldSetMapper"/>
<property name="lineTokenizer" ref="lineTokenizer"/>
</bean>
<bean id="lineTokenizer"
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="delimiter" value=","/>
<property name="names" value="ID,TYPE,NOPUBLICATION" />
</bean>
<bean id="fieldSetMapper"
class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="targetType" value="com.example.model.Patent"/>
</bean>
</beans>
推荐答案
创建 ItemWriteListener
,注入 multiResourceItemWriter
bean并将监听器绑定到你的步骤。
In ItemWriteListener.beforeWrite( )
使用您要写入的项目创建一个新的 ResourceSuffixCreator
对象,以创建资源扩展名(后缀)。
MultiResourceItemWriter.resource
可能需要更改为 file:xml /
,因为1.xml,2.xml和所以将使用为您正在编写的每个项目创建的自定义 ResourceSuffixCreator
附加。
Create a ItemWriteListener
, inject the multiResourceItemWriter
bean and bound the listener to your step.
In ItemWriteListener.beforeWrite()
create a new ResourceSuffixCreator
object using the item your are going to write as base to create resource extension (suffix).MultiResourceItemWriter.resource
probably need to be changed as file:xml/
because 1.xml, 2.xml and so on will be appended using custom ResourceSuffixCreator
dinamically created for every item you are writing.
此解决方案很脏, (可能)由于 commit-interval = 1
而起作用;如果你改变我的答案(可能)会失败。
This solution is dirty and (probably) works due to commit-interval=1
; if you change my answer (probably) will fail.
我希望我很清楚,英语不是我的母语。
I hope I was clear, English is not my native language.
这篇关于如何从Spring Batch中的ItemReader动态设置MultiResourceItemWriter的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!