ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 19. ItemWriter (Json)
    BackEnd/Spring Batch 2021. 12. 31. 17:55
    반응형

    JsonFileItemWriter

    Architecture

      객체를 받아 JSON String으로 변환하는 역할을 합니다.

     

    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.json.JacksonJsonObjectMarshaller;
    import org.springframework.batch.item.json.JsonFileItemWriter;
    import org.springframework.batch.item.json.builder.JsonFileItemWriterBuilder;
    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 JsonConfiguration {
    
        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 JsonFileItemWriter<Customer> customItemWriter() {
            return new JsonFileItemWriterBuilder<Customer>()
                    .jsonObjectMarshaller(new JacksonJsonObjectMarshaller<>())
                    .resource(new ClassPathResource("customer.json"))
                    .name("customerJsonFileItemWriter")
                    .build();
        }
    }

     

    [참고자료]

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

    반응형

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

    21. ItemWriterAdapter  (0) 2021.12.31
    20. ItemWriter (DB)  (0) 2021.12.31
    18. ItemWriter (XML)  (0) 2021.12.31
    17. ItemWriter (File)  (0) 2021.12.31
    16. ItemReaderAdapter  (0) 2021.12.31

    댓글

Designed by Tistory.