-
17. ItemWriter (File)BackEnd/Spring Batch 2021. 12. 31. 17:00반응형
FlatFileItemWriter
2차원 데이터(표)로 표현된 유형의 파일을 처리하는 ItemWriter입니다. 고정 위치로 정의된 데이터 필드나 특수 문자에 의해 구별된 데이터의 행을 기록합니다. Resource와 LineAggregator 두 가지 요소가 필요합니다.
LineAggregator
Item을 받아서 String으로 변환하여 리턴합니다. FieldExtractor를 사용해서 처리할 수 있으며, 구현체로는 PassThroughLineAggregator, DelimitedLineAggregator, FormatterLineAggregator가 있습니다.
FieldExtractor
전달 받은 Item 객체의 필드를 배열로 만들고 배열을 합쳐서 문자열을 만들도록 구현하도록 제공하는 인터페이스입니다. 구현체로는 BeanWrapperFieldExtractor, PassThroughFieldExtractor가 있습니다.
API
DelimitedLineAggregator
객체의 필드 사이에 구분자를 삽입해서 한 문자열로 반환합니다.
package io.springbatch.springbatchlecture; import lombok.RequiredArgsConstructor; 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.core.launch.support.RunIdIncrementer; import org.springframework.batch.item.file.FlatFileItemWriter; import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder; import org.springframework.batch.item.support.ListItemReader; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import java.util.Arrays; import java.util.List; @RequiredArgsConstructor @Configuration public class FlatFilesDelimitedWriteConfiguration { private final JobBuilderFactory jobBuilderFactory; private final StepBuilderFactory stepBuilderFactory; @Bean public Job job() throws Exception { return jobBuilderFactory.get("batchJob") .incrementer(new RunIdIncrementer()) .start(step1()) .build(); } @Bean public Step step1() throws Exception { return stepBuilderFactory.get("step1") .<String,String>chunk(1) .reader(customItemReader()) .writer(customItemWriter()) .build(); } @Bean public ListItemReader customItemReader() { List<Customer> customers = Arrays.asList(new Customer(1, "hong gil dong1", 41), new Customer(2, "hong gil dong2", 42), new Customer(3, "hong gil dong3", 43)); ListItemReader<Customer> reader = new ListItemReader<>(customers); return reader; } @Bean public FlatFileItemWriter<Customer> customItemWriter() throws Exception { return new FlatFileItemWriterBuilder<Customer>() .name("customerWriter") .resource(new FileSystemResource("C:\\jsw\\inflearn\\spring-batch-lecture\\src\\main\\resources\\customer.csv")) .append(true) .delimited() .delimiter(",") .names(new String[] {"id", "name", "age"}) .build(); } /*@Bean public FlatFileItemWriter<Customer> customItemWriter() throws Exception { BeanWrapperFieldExtractor<Customer> fieldExtractor = new BeanWrapperFieldExtractor<>(); fieldExtractor.setNames(new String[] {"id","name","age"}); fieldExtractor.afterPropertiesSet(); DelimitedLineAggregator<Customer> lineAggregator = new DelimitedLineAggregator<>(); lineAggregator.setDelimiter(","); lineAggregator.setFieldExtractor(fieldExtractor); return new FlatFileItemWriterBuilder<Customer>() .name("CustomerWriter") .resource(new ClassPathResource("customer.csv")) .lineAggregator(lineAggregator) .build(); }*/ }
FormatterLineAggregator
객체의 필드를 사용자가 설정한 Formatter 구문을 통해 문자열로 변환합니다.
package io.springbatch.springbatchlecture; import lombok.RequiredArgsConstructor; import org.springframework.batch.core.*; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.launch.support.RunIdIncrementer; import org.springframework.batch.item.ItemWriter; import org.springframework.batch.item.adapter.ItemReaderAdapter; import org.springframework.batch.item.database.*; import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder; import org.springframework.batch.item.database.builder.JdbcPagingItemReaderBuilder; import org.springframework.batch.item.database.builder.JpaCursorItemReaderBuilder; import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder; import org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean; import org.springframework.batch.item.file.FlatFileItemWriter; import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder; import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor; import org.springframework.batch.item.file.transform.DelimitedLineAggregator; import org.springframework.batch.item.json.JacksonJsonObjectReader; import org.springframework.batch.item.json.JsonItemReader; import org.springframework.batch.item.json.builder.JsonItemReaderBuilder; import org.springframework.batch.item.support.ListItemReader; import org.springframework.batch.item.xml.StaxEventItemReader; import org.springframework.batch.item.xml.builder.StaxEventItemReaderBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.CustomAutowireConfigurer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.oxm.xstream.XStreamMarshaller; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import java.sql.Types; import java.util.*; @RequiredArgsConstructor @Configuration public class FlatFilesFormattedConfiguration { private final JobBuilderFactory jobBuilderFactory; private final StepBuilderFactory stepBuilderFactory; @Bean public Job job() throws Exception { return jobBuilderFactory.get("batchJob") .incrementer(new RunIdIncrementer()) .start(step1()) .build(); } @Bean public Step step1() throws Exception { return stepBuilderFactory.get("step1") .<String,String>chunk(10) .reader(customItemReader()) .writer(customItemWriter()) .build(); } @Bean public ListItemReader customItemReader() { List<Customer> customers = Arrays.asList(new Customer(1, "hong gil dong1", 41), new Customer(2, "hong gil dong2", 42), new Customer(3, "hong gil dong3", 43)); ListItemReader<Customer> reader = new ListItemReader<>(customers); return reader; } @Bean public FlatFileItemWriter<Customer> customItemWriter() throws Exception { return new FlatFileItemWriterBuilder<Customer>() .name("customerWriter") .resource(new ClassPathResource("customer.csv")) .formatted() .format("%-2s%-15s%-2d") .names(new String[] {"id", "name", "age"}) .build(); } }
[참고자료]
반응형'BackEnd > Spring Batch' 카테고리의 다른 글
19. ItemWriter (Json) (0) 2021.12.31 18. ItemWriter (XML) (0) 2021.12.31 16. ItemReaderAdapter (0) 2021.12.31 15. ItemReader (DB) (0) 2021.12.31 14. ItemReader (Json) (0) 2021.12.30