Open 0JUUU opened 2 years ago
new FileSystemResource("resource/path/config.xml")
new ClassPathResource("classpath:path/config.xml")
,
)new Range(1)
: 1부터 이후까지 쭉~) @RequiredArgsConstructor
@Configuration
public class FlatFilesFixedLengthConfiguration {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job job() {
return jobBuilderFactory.get("batchJob")
.start(step1())
.next(step2())
.build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<String, String>chunk(3)
.reader(itemReader())
.writer(new ItemWriter() {
@Override
public void write(List items) throws Exception {
items.forEach(item -> System.out.println(item));
}
})
.build();
}
public FlatFileItemReader itemReader() {
return new FlatFileItemReaderBuilder<Customer>()
.name("flatFile")
.resource(new FileSystemResource(
"절대경로")) // 파일시스템 사용
.fieldSetMapper(new BeanWrapperFieldSetMapper<>())
.targetType(Customer.class)
.linesToSkip(1)
.fixedLength()
.addColumns(new Range(1, 5))
.addColumns(new Range(6, 9))
.addColumns(new Range(10, 11))
.names("name", "year", "age")
.build();
}
@Bean
public Step step2() {
return stepBuilderFactory.get("step2")
.tasklet((contribution, chunkContext) -> {
System.out.println("step2 has executed");
return RepeatStatus.FINISHED;
})
.build();
}
}
.addColumns(new Range(1))
.addColumns(new Range(6))
.addColumns(new Range(10))
name,year,age
user11974481
user2197547
user3197646
user4197745
user5197844
user6197943
user7198042
user8198141
user9198240
@Bean
public ItemReader<? extends Customer> customItemReader() {
return new JdbcCursorItemReaderBuilder<Customer>()
.name("jdbcCursorItemReader")
.fetchSize(CHUNK_SIZE)
.sql("select id, firstName, lastName, birthdate from customer where firstName like ? order by lastName, firstName")
.beanRowMapper(Customer.class) // 매핑할 클래스 타입
.queryArguments("A%")
.maxItemCount(3)
.currentItemCount(2)
.dataSource(dataSource)
.build();
}
.maxItemCount(3)
.currentItemCount(2)
EntityManagerFactory
객체가 필요 & 쿼리는 JPQL 사용@Bean
public ItemReader<? extends Customer> customItemReader() {
Map<String, Object> parameters = new HashMap<>();
parameters.put("firstname", "A%");
return new JpaCursorItemReaderBuilder<Customer>()
.name("jpaCursorItemReader")
.entityManagerFactory(entityManagerFactory)
.queryString("select c from Customer c where firstname like :firstname")
.parameterValues(parameters)
.build();
}
spring:
jpa:
hibernate:
ddl-auto: update
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
show-sql: true
properties:
hibernate.format_sql: true
@Bean
public ItemReader<Customer> customItemReader() {
return new JpaPagingItemReaderBuilder<Customer>()
.name("jpaPagingItemReader")
.entityManagerFactory(entityManagerFactory)
.pageSize(10)
.queryString("select c from Customer c")
.build();
}
.queryString("select c from Customer c join fetch c.address")
Chunk Size가 50이고 Page Size가 10이라고 가정했을 때, Chunk Size를 채우기 위해 5번의 Read가 발생 5번의 Read가 발생한 뒤에 itemProcessor로 넘기게 되는데 itemProcessor에서 만약 item의 LazyLoading이 발생한다면 이때가 문제
5번의 Read가 발생하는 동안 각각 트랜잭션이 초기화되기 때문 이 문제는 Page Size와 Chunk Size를 일치시키면 해결 가능