ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 14. ItemReader (Json)
    BackEnd/Spring Batch 2021. 12. 30. 22:30
    반응형

    JsonItemReader

      Json 데이터의 Parsing과 Binding을 JsonObjectReader 인터페이스 구현체에 위임하여 처리하는 ItemReader입니다. JacksonJsonObjectReaderGsonJsonObjectReader 두 가지 구현체를 제공합니다.

    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.json.JacksonJsonObjectReader;
    import org.springframework.batch.item.json.JsonItemReader;
    import org.springframework.batch.item.json.builder.JsonItemReaderBuilder;
    import org.springframework.batch.item.xml.StaxEventItemReader;
    import org.springframework.batch.item.xml.builder.StaxEventItemReaderBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.oxm.xstream.XStreamMarshaller;
    
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    @RequiredArgsConstructor
    @Configuration
    public class JsonConfiguration {
    
        private final JobBuilderFactory jobBuilderFactory;
        private final StepBuilderFactory stepBuilderFactory;
    
        @Bean
        public Job job() {
            return jobBuilderFactory.get("batchJob")
                    .incrementer(new RunIdIncrementer())
                    .start(step1())
                    .build();
        }
    
        @Bean
        public Step step1() {
            return stepBuilderFactory.get("step1")
                    .<Customer, Customer>chunk(3)
                    .reader(customItemReader())
                    .writer(customItemWriter())
                    .build();
        }
    
        @Bean
        public JsonItemReader<Customer> customItemReader(){
            return new JsonItemReaderBuilder<Customer>()
                    .jsonObjectReader(new JacksonJsonObjectReader<>(Customer.class))
                    .resource(new ClassPathResource("customer.json"))
                    .name("jsonItemReader")
                    .build();
        }
    
        @Bean
        public ItemWriter<Customer> customItemWriter() {
            return items -> {
                for (Customer item : items) {
                    System.out.println(item.toString());
                }
            };
        }
    }

     

    [참고자료]

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

    반응형

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

    16. ItemReaderAdapter  (0) 2021.12.31
    15. ItemReader (DB)  (0) 2021.12.31
    13. ItemReader (XML)  (0) 2021.12.30
    12. ItemReader (File)  (0) 2021.12.30
    11. 스프링 배치 청크 프로세스 (Chunk)  (0) 2021.12.24

    댓글

Designed by Tistory.