2011-03-08 64 views
1

作爲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> 

回答

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>