반응형
목차
아래 포스팅에서 이어진 내용입니다.
구분자 없는 텍스트파일의 데이터를 읽어서 로그 출력하는 단순 예제
TextJob 😃
package com.lsy.sample.springbatchvideo.batch;
import com.lsy.sample.springbatchvideo.dto.OneDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
@Slf4j
@RequiredArgsConstructor
@Configuration
public class TextJob {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
private static final int chunkSize = 5;
@Bean
public Job textjob1_batchBuild() {
return jobBuilderFactory.get("textJob1")
.start(textJob1_batchStep1())
.build();
}
@Bean
public Step textJob1_batchStep1() {
return stepBuilderFactory.get("testJob1_batchStep1")
.<OneDto, OneDto>chunk(chunkSize)
.reader(textJob1_FileReader())
.writer(OneDto -> OneDto.stream().forEach(i -> {
log.debug(i.toString());
}))
.build();
}
// text reader
@Bean
public FlatFileItemReader<OneDto> textJob1_FileReader() {
FlatFileItemReader<OneDto> flatFileItemReader = new FlatFileItemReader<>();
flatFileItemReader.setResource(new ClassPathResource("sample/textJob1_input.txt"));
flatFileItemReader.setLineMapper((line, lineNumber) -> new OneDto(lineNumber + " == " + line));
return flatFileItemReader;
}
}
개인 스터디 기록을 메모하는 공간이라 틀린점이 있을 수 있습니다.
틀린 점 있을 경우 댓글 부탁드립니다.
reference: https://www.youtube.com/watch?v=wy99cPHlMlA&list=PLogzC_RPf25HRSG9aO7qKrwbT-EecUMMR
다음 내용
반응형
'IT > development' 카테고리의 다른 글
[springBoot] spring batch simple csvFileReader (feat. file) (51) | 2023.11.11 |
---|---|
[springBoot] spring batch write to new file (feat. file) (55) | 2023.11.10 |
[springBoot] spring batch simpleReader insert other table( feat. DB) (54) | 2023.11.10 |
[springBoot] spring batch jpa simpleDbReader (feat. DB) (56) | 2023.11.10 |
[springBoot] spring batch simple tasklet (feat. tasklet) (57) | 2023.11.09 |