FILE TO DB - Fixed Length 예제

2018. 10. 22. 22:20Spring Batch

반응형

FILE TO DB - Delimiter 예제 와 다른 점은 Job 설정, Reader설정, DTO 파일만 다르므로 이 두파일 만 작성 후 실행 


FileFixedLengthToDbJob.xml 작성


- Job 설정

- Reader 설정

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


▩ Job ID 설정

<job id="fileFixedLengthToDbJob" xmlns="http://www.springframework.org/schema/batch">
  <step id="step1">
    <tasklet>
      <chunk reader="fileFixedLengthToDbReader"
             writer="fileFixedLengthToDbWriter"
             commit-interval="1000"/>
    </tasklet>
  </step>
</job>


▩ Reader 설정

<bean id="fileFixedLengthToDbReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" value="file:{파일경로}\fifa_ranking_fixed_length.csv" /> <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.EgovFixedLengthTokenizer"> <property name="columns" value="1,11,42,52,62,72,82,92,102,112,122,132,142,152,162,172"/> </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>

1.EgovFixedLengthTokenizer 클래스를 이용하여서 Fixed Length 파싱 처리

  - Columns의 Value는 Tab으로 구분한 컬럼의 시작 위치를 의미

    1---------11-----------------------------42.....

    1             Germany                                 GER       0         57        0


2.EgovObjectMapper 클래스를 이용하여 파싱결과를 DTO 클래스 맵핑 처리


▩ FifaRankingDTO 작성

package com.batchguide.dto;

public class FifaRankingDTO {

private String rank;
	
	private String rank_date;
	
	private String country_full;
	
	private String country_abrv;
	
	private double total_points;
	
	private int previous_points;
	
	private int rank_change;
	
	private double cur_year_avg;
	
	private double cur_year_avg_weighted;
	
	private double last_year_avg;
	
	private double last_year_avg_weighted;
	
	private double two_year_ago_avg;
	
	private double two_year_ago_weighted;
	
	private double three_year_ago_avg;
	
	private double three_year_ago_weighted;
	
	private String confederation;

	/**
	 * @return the rank
	 */
	public String getRank() {
		return rank;
	}

	/**
	 * @param rank the rank to set
	 */
	public void setRank(String rank) {
		this.rank = rank.trim();
	}

	/**
	 * @return the rank_date
	 */
	public String getRank_date() {
		return rank_date;
	}

	/**
	 * @param rank_date the rank_date to set
	 */
	public void setRank_date(String rank_date) {
		this.rank_date = rank_date.replace("-", "").trim();
	}

	/**
	 * @return the country_full
	 */
	public String getCountry_full() {
		return country_full;
	}

	/**
	 * @param country_full the country_full to set
	 */
	public void setCountry_full(String country_full) {
		this.country_full = country_full.trim();
	}

	/**
	 * @return the country_abrv
	 */
	public String getCountry_abrv() {
		return country_abrv;
	}

	/**
	 * @param country_abrv the country_abrv to set
	 */
	public void setCountry_abrv(String country_abrv) {
		this.country_abrv = country_abrv.trim();
	}

	/**
	 * @return the total_points
	 */
	public double getTotal_points() {
		return total_points;
	}

	/**
	 * @param total_points the total_points to set
	 */
	public void setTotal_points(double total_points) {
		this.total_points = total_points;
	}

	/**
	 * @return the previous_points
	 */
	public int getPrevious_points() {
		return previous_points;
	}

	/**
	 * @param previous_points the previous_points to set
	 */
	public void setPrevious_points(int previous_points) {
		this.previous_points = previous_points;
	}

	/**
	 * @return the rank_change
	 */
	public int getRank_change() {
		return rank_change;
	}

	/**
	 * @param rank_change the rank_change to set
	 */
	public void setRank_change(int rank_change) {
		this.rank_change = rank_change;
	}

	/**
	 * @return the cur_year_avg
	 */
	public double getCur_year_avg() {
		return cur_year_avg;
	}

	/**
	 * @param cur_year_avg the cur_year_avg to set
	 */
	public void setCur_year_avg(double cur_year_avg) {
		this.cur_year_avg = cur_year_avg;
	}

	/**
	 * @return the cur_year_avg_weighted
	 */
	public double getCur_year_avg_weighted() {
		return cur_year_avg_weighted;
	}

	/**
	 * @param cur_year_avg_weighted the cur_year_avg_weighted to set
	 */
	public void setCur_year_avg_weighted(double cur_year_avg_weighted) {
		this.cur_year_avg_weighted = cur_year_avg_weighted;
	}

	/**
	 * @return the last_year_avg
	 */
	public double getLast_year_avg() {
		return last_year_avg;
	}

	/**
	 * @param last_year_avg the last_year_avg to set
	 */
	public void setLast_year_avg(double last_year_avg) {
		this.last_year_avg = last_year_avg;
	}

	/**
	 * @return the last_year_avg_weighted
	 */
	public double getLast_year_avg_weighted() {
		return last_year_avg_weighted;
	}

	/**
	 * @param last_year_avg_weighted the last_year_avg_weighted to set
	 */
	public void setLast_year_avg_weighted(double last_year_avg_weighted) {
		this.last_year_avg_weighted = last_year_avg_weighted;
	}

	/**
	 * @return the two_year_ago_avg
	 */
	public double getTwo_year_ago_avg() {
		return two_year_ago_avg;
	}

	/**
	 * @param two_year_ago_avg the two_year_ago_avg to set
	 */
	public void setTwo_year_ago_avg(double two_year_ago_avg) {
		this.two_year_ago_avg = two_year_ago_avg;
	}

	/**
	 * @return the two_year_ago_weighted
	 */
	public double getTwo_year_ago_weighted() {
		return two_year_ago_weighted;
	}

	/**
	 * @param two_year_ago_weighted the two_year_ago_weighted to set
	 */
	public void setTwo_year_ago_weighted(double two_year_ago_weighted) {
		this.two_year_ago_weighted = two_year_ago_weighted;
	}

	/**
	 * @return the three_year_ago_avg
	 */
	public double getThree_year_ago_avg() {
		return three_year_ago_avg;
	}

	/**
	 * @param three_year_ago_avg the three_year_ago_avg to set
	 */
	public void setThree_year_ago_avg(double three_year_ago_avg) {
		this.three_year_ago_avg = three_year_ago_avg;
	}

	/**
	 * @return the three_year_ago_weighted
	 */
	public double getThree_year_ago_weighted() {
		return three_year_ago_weighted;
	}

	/**
	 * @param three_year_ago_weighted the three_year_ago_weighted to set
	 */
	public void setThree_year_ago_weighted(double three_year_ago_weighted) {
		this.three_year_ago_weighted = three_year_ago_weighted;
	}

	/**
	 * @return the confederation
	 */
	public String getConfederation() {
		return confederation;
	}

	/**
	 * @param confederation the confederation to set
	 */
	public void setConfederation(String confederation) {
		this.confederation = confederation;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Ftd001VO [rank=" + rank + ", rank_date=" + rank_date + ", country_full=" + country_full
				+ ", country_abrv=" + country_abrv + ", total_points=" + total_points + ", previous_points="
				+ previous_points + ", rank_change=" + rank_change + ", cur_year_avg=" + cur_year_avg
				+ ", cur_year_avg_weighted=" + cur_year_avg_weighted + ", last_year_avg=" + last_year_avg
				+ ", last_year_avg_weighted=" + last_year_avg_weighted + ", two_year_ago_avg=" + two_year_ago_avg
				+ ", two_year_ago_weighted=" + two_year_ago_weighted + ", three_year_ago_avg=" + three_year_ago_avg
				+ ", three_year_ago_weighted=" + three_year_ago_weighted + ", confederation=" + confederation + "]";
	}

}

Fixed Length 파싱 후 DTO 맵핑 시 불필요한 스페이스까지 파싱 처리 되므로 문자열의 경우 trim() 처리 하여준다.


▩ 전체설정파일

<?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="fileFixedLengthToDbJob" xmlns="http://www.springframework.org/schema/batch"> <step id="FileFixedLengthToDbStep"> <tasklet> <chunk reader="fileFixedLengthToDbReader" writer="fileFixedLengthToDbWriter" commit-interval="1000"/> </tasklet> </step> </job> <!-- READER 설정 --> <bean id="fileFixedLengthToDbReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step"> <property name="resource" value="file:{파일경로}\fifa_ranking_fixed_length.csv" /> <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.EgovFixedLengthTokenizer"> <property name="columns" value="1,11,42,52,62,72,82,92,102,112,122,132,142,152,162,172"/> </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="fileFixedLengthToDbWriter" 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' 카테고리의 다른 글

DB To File 예제  (0) 2018.11.14
MULTI FILE TO DB - 다중파일 예제  (0) 2018.11.01
FILE TO DB - Delimiter 예제  (0) 2018.10.14
Spring+myBatis 환경설정  (0) 2018.10.13
Hello Friends 예제 - 2  (0) 2018.09.29