์์์ ETL ๊ธฐ๋ณธ ์ค์ ๊ณผ Spring batch Job์ ์์๋ณด์๋ค.
Scheduler๋ฅผ ํตํด Job์ ์ฃผ๊ธฐ์ ์ผ๋ก ์คํ์ํฌ ๊ฒ์ด๋ค.
๋ณด๋ค ์์ธํ Scheduler์ ๋ณด๋ ์ฌ๊ธฐ์ ํฌ์คํ
ํด๋์๋ค.
Spring batch์์ ํน์ Job์ ์คํ์ํค๊ธฐ ์ํด์๋ Job Parameter๋ฅผ ์ค์ ํด์ผํ๋ค.
Job Parameter๋ @JobScope
๋ @StepScope
์ ํจ๊ป ์ฌ์ฉํ๋ค.
@JobScope
: Step๊ณผ ํจ๊ป ์ฌ์ฉํ๋ค.
@StepScope
: tasklet์ด๋ ItemReader, ItemProcessor, ItemWriter์ ํจ๊ป ์ฌ์ฉํ๋ค.
๋ค์ ์์ ๋ Job Parameter๋ก requestDate
๋ฅผ ๋ฐ์ ItemProcessor
์์ ์ถ๋ ฅํ๋ ์์ ์ด๋ค.
@Bean
@StepScope
public ItemProcessor<Product, TransProduct> processor(@Value("#{jobParameters[requestDate]}") String requestDate) {
return product -> {
String name = product.getName();
Long price = product.getPrice();
LocalDateTime created = product.getCreated();
System.out.println(requestDate);
if( price > 2500 ) {
return new TransProduct(name, price - 500L, created, true);
} else {
return new TransProduct(name, price, created, false);
}
};
}
etlJob
์ ์คํํ๊ณ Job Parameter์ธ requestDate
์ ๊ฐ์ ํ ๋นํด์ค๋ค.
./gradlew build -x test
java -jar ./build/libs/*.jar --job.name=etlJob requestDate=20210511
์ด์ 2. Execute์์ ๋ง๋ jar
๋ฅผ ์ฃผ๊ธฐ์ ์ผ๋ก ์คํ์์ผ์ฃผ์.
๋ณธ ๋ฌธ์์์๋ Jenkins๋ฅผ ์ด์ฉํ์ฌ Scheduling์ ํ ๊ฒ์ด๋ค.
์ด ๊ณณ์ Jenkins ์
ํ
์ ๋ช
์ํด ๋์๋ค.
์ฌ๊ธฐ์ jar
๋ฅผ ์คํํ๋ ๋ถ๋ถ์ Job Parameter๋ฅผ ๊ฐ์ด ๋๊ฒจ์ฃผ๋ฉด ์์
์๋ฃ์ด ์๋ฃ๋๋ค.
# requestDate๋ก ํ์ฌ ์๊ฐ์ ๋๊ฒจ์ค๋ค.
java -jar $PWD/demo-0.0.1-SNAPSHOT.jar --job.name=etlJob requestDate=$(date | tr -d " ")
Build History
์์ 1๋ถ๋ง๋ค Batch๊ฐ ์คํ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
๋ชจ๋ ์์ค๋ Github์ ์ฌ๋ ค๋์๋ค.