반응형
목차
아래 포스팅에서 이어진 내용입니다.
객체의 데이터를 읽어서 delimited, formatted 방식을 적용해서 파일에 쓰는 예제
JobTestConfig
package com.dev.lsy.springbatchremind.batch;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.ExitStatus;
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.JobScope;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
import org.springframework.batch.item.json.JacksonJsonObjectMarshaller;
import org.springframework.batch.item.json.builder.JsonFileItemWriterBuilder;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import java.util.Arrays;
import java.util.List;
@Slf4j
@RequiredArgsConstructor
@Configuration
public class JobTestConfig {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
//job
public Job job1() {
return jobBuilderFactory.get("job1")
//jobParameter 자동 증감
.incrementer(new RunIdIncrementer())
.start(step1())
.build();
}
@Bean
//step
public Step step1() {
return stepBuilderFactory.get("step1")
//chunk 기반 작업
.<Person, Person>chunk(10)
.reader(customReader())
//여기만 다르다.
.writer(customWriter())
.build();
}
//reader
@Bean
public ListItemReader customReader() {
//테스트 리스트(여기는 동일)
List<Person> list = Arrays.asList(
new Person(1L, "아이언맨", 51),
new Person(2L, "토르", 10000),
new Person(3L, "헐크", 52)
);
ListItemReader<Person> reader = new ListItemReader<>(list);
return reader;
}
@Bean
public ItemWriter<? super Person> customWriter() {
return new FlatFileItemWriterBuilder<Person>()
.name("flatFileWriter")
.resource(new FileSystemResource("output/output.txt"))
.formatted()
//id 2자는 숫자(d), name은 6자 문자(s), age도 숫자(d)
.format("%-2d%-6s%-2d")
//.delimited() //구분자 방식은 이 옵션이 추가된다.
//.delimiter("#")
.names(new String[]{"id", "name", "age"})
.build();
}
}
개인 스터디 기록을 메모하는 공간이라 틀린점이 있을 수 있습니다.
틀린 점 있을 경우 댓글 부탁드립니다.
reference: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EB%B0%B0%EC%B9%98
다음 내용
반응형
'IT > development' 카테고리의 다른 글
[springBoot] spring batch flowJob (feat.simpleFlow) (55) | 2023.11.18 |
---|---|
[springBoot] spring batch jsonFileItemWriter (55) | 2023.11.18 |
[springBoot] spring batch step startLimit (54) | 2023.11.16 |
[springBoot] spring batch JobScope, StepScope (55) | 2023.11.15 |
[springBoot] spring batch allowStartIfComplete (53) | 2023.11.15 |