[Spring Batch] batch process : 테이블 to 테이블
2024. 9. 17. 00:32ㆍ프로그래밍(Backend)/Spring Batch
1. 테이블 생성
BeforeEntity, BeforeRepository
@Data
@Entity
public class BeforeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
}
@Repository
public interface BeforeRepository extends JpaRepository<BeforeEntity, Long> {
}
AfterEntity, AfterRepository
@Data
@Entity
public class AfterEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
}
@Repository
public interface AfterRepository extends JpaRepository<AfterEntity, Long> {
}
테이블 생성 확인
테이블에 실습 데이터 삽입
Spring Batch 모식도
하나의 배치 Job을 정의할 클래스 FirstBatch생성 및 Jop, Step, ItemReader, ItemProcessor, ItemWriter 순으로 작성
Job
@Configuration
@RequiredArgsConstructor
public class FirstBatch {
private final JobRepository jobRepository;
private final PlatformTransactionManager platformTransactionManager;
private final AfterRepository afterRepository;
private final BeforeRepository beforeRepository;
@Bean
public Job firstJob() {
return new JobBuilder("firstJob", jobRepository)
.start(firstStep())
.build();
}
}
Setp
@Bean
public Step firstStep(){
return new StepBuilder("firstStep", jobRepository)
.<BeforeEntity, AfterEntity> chunk(10, platformTransactionManager)
.reader(beforeReader())
.processor(middleProcessor())
.writer(afterWriter())
.build();
}
ItemReader
// JPA를 통한 쿼리를 수행하기 때문에 RepositoryItemReader를 사용
@Bean
public RepositoryItemReader<BeforeEntity> beforeReader() {
return new RepositoryItemReaderBuilder<BeforeEntity>()
.name("beforeReader")
.pageSize(10)
.methodName("findAll")
.repository(beforeRepository)
.sorts(Map.of("id", Sort.Direction.ASC))
.build();
}
ItemProcessor
@Bean
public ItemProcessor<BeforeEntity, AfterEntity> middleProcessor() {
return new ItemProcessor<BeforeEntity, AfterEntity>() {
@Override
public AfterEntity process(BeforeEntity item) throws Exception {
AfterEntity afterEntity = new AfterEntity();
afterEntity.setUsername(item.getUsername());
return afterEntity;
}
};
}
ItemWriter
@Bean
public RepositoryItemWriter<AfterEntity> afterWriter() {
return new RepositoryItemWriterBuilder<AfterEntity>()
.repository(afterRepository)
.methodName("save")
.build();
}
FirstBatch 전체코드
@Configuration
@RequiredArgsConstructor
public class FirstBatch {
private final JobRepository jobRepository;
private final PlatformTransactionManager platformTransactionManager;
private final AfterRepository afterRepository;
private final BeforeRepository beforeRepository;
@Bean
public Job firstJob() {
return new JobBuilder("firstJob", jobRepository)
.start(firstStep())
.build();
}
@Bean
public Step firstStep(){
return new StepBuilder("firstStep", jobRepository)
.<BeforeEntity, AfterEntity> chunk(10, platformTransactionManager)
.reader(beforeReader())
.processor(middleProcessor())
.writer(afterWriter())
.build();
}
// JPA를 통한 쿼리를 수행하기 때문에 RepositoryItemReader를 사용
@Bean
public RepositoryItemReader<BeforeEntity> beforeReader() {
return new RepositoryItemReaderBuilder<BeforeEntity>()
.name("beforeReader")
.pageSize(10)
.methodName("findAll")
.repository(beforeRepository)
.sorts(Map.of("id", Sort.Direction.ASC))
.build();
}
@Bean
public ItemProcessor<BeforeEntity, AfterEntity> middleProcessor() {
return new ItemProcessor<BeforeEntity, AfterEntity>() {
@Override
public AfterEntity process(BeforeEntity item) throws Exception {
AfterEntity afterEntity = new AfterEntity();
afterEntity.setUsername(item.getUsername());
return afterEntity;
}
};
}
@Bean
public RepositoryItemWriter<AfterEntity> afterWriter() {
return new RepositoryItemWriterBuilder<AfterEntity>()
.repository(afterRepository)
.methodName("save")
.build();
}
}
'프로그래밍(Backend) > Spring Batch' 카테고리의 다른 글
[Spring Batch] 배치 처리2 : 테이블 조건 (0) | 2024.09.18 |
---|---|
[Spring Batch] 배치 처리 : 실행 및 스케쥴 (0) | 2024.09.18 |
[Spring Batch] MetaData 테이블 (2) | 2024.09.16 |
[Spring Batch] DB 연결 Config 클래스 작성 (1) | 2024.09.16 |
[Spring Batch] 프로젝트 생성 (0) | 2024.09.16 |