English 中文(简体)
档案
原标题:File to file basics

Being new to Spring Batch, I wanted to begin with something simple....Reading a csv file and writing the same objects(records) to another one. Simple, isn t it ? But I was unable to find a working sample. After some time of research, I found some things which almost work....The file I want to write to is allways empty. Is it because I use a ressourcelesstransactionmanager ? Do I need to declare some optional property somewhere to flush the thing on my hard drive ? By the way, I find the documentation on the subject very light and confusing, for a beginner. Maybe it s because one must earn spring Batch... Here s the evil but very simple code that s driving me crazy. TIA.

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/batch 
http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
        http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-2.1.xsd">

    <bean id="jobRepository"
        class="org.springframework.batch.core.repository.support.SimpleJobRepository">
        <constructor-arg>
            <bean
                class="org.springframework.batch.core.repository.dao.MapJobInstanceDao"/>
        </constructor-arg>
        <constructor-arg>
            <bean
                class="org.springframework.batch.core.repository.dao.MapJobExecutionDao"/>
        </constructor-arg>
        <constructor-arg>
            <bean
                class="org.springframework.batch.core.repository.dao.MapStepExecutionDao"/>
        </constructor-arg>
        <constructor-arg>
            <bean
                class="org.springframework.batch.core.repository.dao.MapExecutionContextDao"/>
        </constructor-arg>
    </bean>

    <bean id="jobRepository-transactionManager"
        class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
    <bean id="jobLauncher"
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher"
        p:jobRepository-ref="jobRepository"/>
    <!-- Les bean projet -->
    <bean id="csvReader"
        class="org.springframework.batch.item.file.FlatFileItemReader"
        p:resource="file:c:AppPersocsvManagerclient.csv">
        <property name="lineMapper">
            <bean
                class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <property name="lineTokenizer">
                    <bean
                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"
                        p:delimiter=";"
                        p:names="client_id;client_status;client_name;client_surname;client_dnais;client_addr1;client_addr2;client_addr3;client_addr4"/>
                </property>
                <property name="fieldSetMapper">
                    <bean
                        class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"
                        p:targetType="com.bigmac.spring.batch.csv.Client"/>
                </property>
            </bean>
        </property>
    </bean>

    <bean id="csvWriter"
        class="org.springframework.batch.item.file.FlatFileItemWriter">
        <property name="resource" value="file:c:AppPersocsvManagerclient-reformat.csv"/>
        <property name="shouldDeleteIfExists" value="true"/>
        <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="client_id;client_status;client_name;client_surname;client_dnais;client_addr1;client_addr2;client_addr3;client_addr4"/>
                    </bean>
                </property>
            </bean>
        </property>
    </bean> 

    <batch:job id="CSVManager" job-repository="jobRepository">
        <batch:step id="step1">
            <batch:tasklet
                transaction-manager="jobRepository-transactionManager">
                <batch:chunk 
                    reader="csvReader" 
                    writer="csvWriter" 
                    commit-interval="10"/>
            </batch:tasklet>
        </batch:step>
    </batch:job>

</beans>
问题回答

Sorry guys. My mistake. I was getting confused with delimiters...The lack of properly configured log4j wasn t helping either.

我将工作组合放在这里。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/batch 
http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
        http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-2.1.xsd">

    <bean id="jobRepository"
        class="org.springframework.batch.core.repository.support.SimpleJobRepository">
        <constructor-arg>
            <bean
                class="org.springframework.batch.core.repository.dao.MapJobInstanceDao"/>
        </constructor-arg>
        <constructor-arg>
            <bean
                class="org.springframework.batch.core.repository.dao.MapJobExecutionDao"/>
        </constructor-arg>
        <constructor-arg>
            <bean
                class="org.springframework.batch.core.repository.dao.MapStepExecutionDao"/>
        </constructor-arg>
        <constructor-arg>
            <bean
                class="org.springframework.batch.core.repository.dao.MapExecutionContextDao"/>
        </constructor-arg>
    </bean>

    <bean id="jobRepository-transactionManager"
        class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
    <bean id="jobLauncher"
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher"
        p:jobRepository-ref="jobRepository"/>
    <!-- Les bean projet -->
    <bean id="csvReader"
        class="org.springframework.batch.item.file.FlatFileItemReader"
        p:resource="file:c:AppPersospringBatchcsvManagerclient.csv">
        <property name="lineMapper">
            <bean
                class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <property name="lineTokenizer">
                    <bean
                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"
                        p:delimiter=";"
                        p:names="client_id,client_status,client_name,client_surname,client_dnais,client_addr1,client_addr2,client_addr3,client_addr4"/>
                </property>
                <property name="fieldSetMapper">
                    <bean
                        class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"
                        p:targetType="com.bigmac.spring.batch.csv.Client"/>
                </property>
            </bean>
        </property>
    </bean>

    <bean id="csvWriter"
        class="org.springframework.batch.item.file.FlatFileItemWriter">
        <property name="resource" value="file:c:AppPersospringBatchcsvManagerclient-reformat.csv"/>
        <property name="shouldDeleteIfExists" value="true"/>
        <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="client_id,client_status,client_name,client_surname,client_dnais,client_addr1,client_addr2,client_addr3,client_addr4"/>
                    </bean>
                </property>
            </bean>
        </property>
    </bean> 

    <batch:job id="CSVManager" job-repository="jobRepository">
        <batch:step id="step1">
            <batch:tasklet
                transaction-manager="jobRepository-transactionManager">
                <batch:chunk 
                    reader="csvReader" 
                    writer="csvWriter" 
                    commit-interval="10"/>
            </batch:tasklet>
        </batch:step>
    </batch:job>

</beans>




相关问题
Spring batch JMS writer/reader example

Anybody know of a good resource for a detailed (more so than the Spring Batch docs) look at the uses of JMS Item Writer/Reader in Spring Batch? Specifically, and because I m being tasked with trying ...

Ways to reduce memory churn

Background I have a Spring batch program that reads a file (example file I am working with is ~ 4 GB in size), does a small amount of processing on the file, and then writes it off to an Oracle ...

How to set up multi-threading in Spring Batch?

I ve successfully set up a tutorial Spring Batch project. I d really like to know if it s possible to make it multi-threaded at the "Spring level". The basic idea of what I want is to make a list ...

Spring Batch resourceless JobRepository

I m using Spring Batch for a system that does a lot of batch operations. I m using SimpleJobRepository with in memory DAOs. I would like to know if there is a way to avoid using a JobRepository? ...

热门标签