MULTI FILE TO DB - 다중파일 예제

2018. 11. 1. 22:57Spring Batch

반응형

MultiFileReaderToDb.xml 작성


- Job 설정

- Reader 설정

- Writer 설정은 FILE TO DB - Delimiter 예제 와 동일


※ 예제파일은 기존 CSV 파일은 1개 이상의 파일로 나누어서 예제파일을 만든다.


▩ Job ID 설정
<job id="multiFileReaderToDbJob" xmlns="http://www.springframework.org/schema/batch">
  <step id="step1">
    <tasklet>
      <chunk reader="multiFileDbReader"
             writer="multiFileDbWriter"
             commit-interval="1000"/>
    </tasklet>
  </step>
</job>a


▩ Reader 설정
<!-- Reader Task 작성 -->
<bean id="multiFileDbReader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
<property name="resources" value="file:{파일경로}/fifa_ranking_delimiter_*.csv" /> <property name="delegate" ref="delegateReader" /> </bean> <bean id="delegateReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step"> <property name="lineMapper"> <bean class="egovframework.rte.bat.core.item.file.mapping.EgovDefaultLineMapper"> <property name="lineTokenizer"> <bean class="egovframework.rte.bat.core.item.file.transform.EgovDelimitedLineTokenizer"> <property name="delimiter" value=","/> </bean> </property> <property name="objectMapper"> <bean class="egovframework.rte.bat.core.item.file.mapping.EgovObjectMapper"> <property name="type" value="com.batchguide.dto.FifaRankingDTO" /> <property name="names" value="rank,country_full,country_abrv,total_points,previous_points,rank_change,cur_year_avg, cur_year_avg_weighted,last_year_avg,last_year_avg_weighted,two_year_ago_avg,two_year_ago_weighted,three_year_ago_avg, three_year_ago_weighted,confederation,rank_date" /> </bean> </property> </bean> </property> </bean>

멀티파일 처리인 경우 MultiResourceItemReader를 통하여서 1개이상의 파일을 처리한다. 나머지 부분은 앞선 예제와 동일하다.


▩ 전체설정파일

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd"> <job id="multiFileReaderToDbJob" xmlns="http://www.springframework.org/schema/batch"> <step id="step1"> <tasklet> <chunk reader="multiFileDbReader" writer="multiFileDbWriter"
commit-interval="1000"/> </tasklet> </step> </job> <!-- Reader 설정 --> <bean id="multiFileDbReader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step"> <property name="resources" value="file:{파일경로}/fifa_ranking_delimiter_*.csv" /> <property name="delegate" ref="delegateReader" /> </bean> <bean id="delegateReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step"> <property name="lineMapper"> <bean class="egovframework.rte.bat.core.item.file.mapping.EgovDefaultLineMapper"> <property name="lineTokenizer"> <bean class="egovframework.rte.bat.core.item.file.transform.EgovDelimitedLineTokenizer"> <property name="delimiter" value=","/> </bean> </property> <property name="objectMapper"> <bean class="egovframework.rte.bat.core.item.file.mapping.EgovObjectMapper"> <property name="type" value="com.batchguide.dto.FifaRankingDTO" /> <property name="names" value="rank,country_full,country_abrv,total_points,previous_points,rank_change,cur_year_avg, cur_year_avg_weighted,last_year_avg,last_year_avg_weighted,two_year_ago_avg,two_year_ago_weighted,three_year_ago_avg, three_year_ago_weighted,confederation,rank_date" /> </bean> </property> </bean> </property> </bean> <!-- Writer 설정 --> <bean id="multiFileDbWriter" class="org.mybatis.spring.batch.MyBatisBatchItemWriter">
<property name="statementId" value="insertFifaRanking" /> <property name="sqlSessionTemplate" ref="batchSqlSessionTemplate" /> </bean> </beans>

나머지 설정 및 실행 은 이전 예제 FILE TO DB - Delimiter 예제 와 동일



[소스레파지토리]

☞ https://github.com/roopy1210/springbatch/blob/master/spring_batch_tutorial





반응형

'Spring Batch' 카테고리의 다른 글

CompositeItemWriter 예제 - 설정  (0) 2018.12.02
DB To File 예제  (0) 2018.11.14
FILE TO DB - Fixed Length 예제  (0) 2018.10.22
FILE TO DB - Delimiter 예제  (0) 2018.10.14
Spring+myBatis 환경설정  (0) 2018.10.13