文件归档基础知识

时间:2011-03-08 16:31:14

标签: spring-batch

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

1 个答案:

答案 0 :(得分: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>