ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 18. ItemWriter (XML)
    BackEnd/Spring Batch 2021. 12. 31. 17:30
    반응형

    StaxEventItemWriter

    Architecture

      XML 쓰는 과정은 읽기 과정에 대칭적입니다. StaxEventItemWriter는 Resource, marshaller, rootTagName가 필요합니다.

     

    API

    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.database.*;
    import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
    import org.springframework.batch.item.xml.StaxEventItemWriter;
    import org.springframework.batch.item.xml.builder.StaxEventItemWriterBuilder;
    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.oxm.xstream.XStreamMarshaller;
    
    import javax.sql.DataSource;
    import java.util.*;
    
    @RequiredArgsConstructor
    @Configuration
    public class XMLConfiguration {
    
        private final JobBuilderFactory jobBuilderFactory;
        private final StepBuilderFactory stepBuilderFactory;
        private final DataSource dataSource;
    
        @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")
                    .<Customer, Customer>chunk(10)
                    .reader(customItemReader())
                    .writer(customItemWriter())
                    .build();
        }
    
        @Bean
        public JdbcPagingItemReader<Customer> customItemReader() {
    
            JdbcPagingItemReader<Customer> reader = new JdbcPagingItemReader<>();
    
            reader.setDataSource(this.dataSource);
            reader.setFetchSize(10);
            reader.setRowMapper(new CustomerRowMapper());
    
            MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
            queryProvider.setSelectClause("id, firstName, lastName, birthdate");
            queryProvider.setFromClause("from customer");
            queryProvider.setWhereClause("where firstname like :firstname");
    
            Map<String, Order> sortKeys = new HashMap<>(1);
    
            sortKeys.put("id", Order.ASCENDING);
            queryProvider.setSortKeys(sortKeys);
            reader.setQueryProvider(queryProvider);
    
            HashMap<String, Object> parameters = new HashMap<>();
            parameters.put("firstname", "A%");
    
            reader.setParameterValues(parameters);
    
            return reader;
        }
    
        @Bean
        public StaxEventItemWriter customItemWriter() {
            return new StaxEventItemWriterBuilder<Customer>()
                    .name("customersWriter")
                    // Marshaller: XMLEventWriter를 사용해서 StartDocumnet와 EndDocument 이벤트를 필터링해서 Resources에 쓴다.
                    .marshaller(itemMarshaller())
                    // 작성할 파일을 나타내는 스프링 Resource
                    .resource(new FileSystemResource("customer.xml"))
                    // Root Element Name
                    .rootTagName("customer")
                    // 이미 존재하는 파일을 덮어쓸 것인지 결정
                    .overwriteOutput(true)
                    .build();
        }
    
        @Bean
        public XStreamMarshaller itemMarshaller() {
        	// StaxEventItemReader와 동일한 설정이다.
            // Map으로 alias를 지정
            // 첫번쨰 키는 조각의 루트 엘리먼트, 값은 바인딩할 객체 타입
            // 두번째 부터는 하위 엘리먼트와 각 클래스 타입
            Map<String, Class<?>> aliases = new HashMap<>();
            aliases.put("customer", Customer.class);
            aliases.put("id", Long.class);
            aliases.put("firstName", String.class);
            aliases.put("lastName", String.class);
            aliases.put("birthdate", Date.class);
            XStreamMarshaller xStreamMarshaller = new XStreamMarshaller();
            xStreamMarshaller.setAliases(aliases);
            return xStreamMarshaller;
        }
    }

     

    [참고자료]

    인프런-스프링 배치 - Spring Boot 기반으로 개발하는 Spring Batch

    반응형

    'BackEnd > Spring Batch' 카테고리의 다른 글

    20. ItemWriter (DB)  (0) 2021.12.31
    19. ItemWriter (Json)  (0) 2021.12.31
    17. ItemWriter (File)  (0) 2021.12.31
    16. ItemReaderAdapter  (0) 2021.12.31
    15. ItemReader (DB)  (0) 2021.12.31

    댓글

Designed by Tistory.