1

作为 Spring Batch 的新手,我想从一些简单的事情开始......读取一个 csv 文件并将相同的对象(记录)写入另一个文件。很简单,不是吗?但我找不到工作样本。经过一段时间的研究,我发现了一些几乎可以工作的东西......我要写入的文件总是空的。是因为我使用了 ressourcelesstransactionmanager 吗?我是否需要在某处声明一些可选属性来刷新硬盘上的东西?顺便说一句,对于初学者来说,我发现有关该主题的文档非常轻松且令人困惑。也许是因为必须获得 spring Batch ......这是让我发疯的邪恶但非常简单的代码。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:\AppPerso\csvManager\client.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:\AppPerso\csvManager\client-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>
4

1 回答 1

2

对不起大家。我的错。我对分隔符感到困惑......缺少正确配置的 log4j 也无济于事。

我把工作配置放在这里。

<?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:\AppPerso\springBatch\csvManager\client.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:\AppPerso\springBatch\csvManager\client-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>
于 2011-03-08T17:54:25.580 回答