IT/development

[springBoot] spring batch JsonReader logPrint (feat. JSON)

알 수 없는 사용자 2023. 11. 11. 16:08
반응형

목차

    아래 포스팅에서 이어진 내용입니다.

     

    [springBoot] spring batch csvFileReader write new File (feat. file)

    목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch simple csvFileReader (feat. file) 목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch write to new file (feat. file) 아래 포스팅에서

    yaga.tistory.com


    JSON 형식의 데이터를 읽어서 로그에 출력하는 간단한 예제

    JsonJob1 😃

    package com.test.lsy.batchsimpledbreader.batch;
    
    import com.test.lsy.batchsimpledbreader.dto.MarketDto;
    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    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.item.json.JacksonJsonObjectReader;
    import org.springframework.batch.item.json.JsonItemReader;
    import org.springframework.batch.item.json.builder.JsonItemReaderBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.ClassPathResource;
    
    @Slf4j
    @RequiredArgsConstructor
    @Configuration
    public class JsonJob1 {
    
        private final JobBuilderFactory jobBuilderFactory;
        private final StepBuilderFactory stepBuilderFactory;
        private static final int chunkSize = 5;
    
        //아이고 잡을 안 만들었네
        @Bean
        public Job jsonJob1_batchBuild() {
            return jobBuilderFactory.get("jsonJob1_batchBuild")
                    .start(jsonJob1_batchStep1())
                    .build();
        }
    
        // step
        @Bean
        public Step jsonJob1_batchStep1() {
            return stepBuilderFactory.get("jsonJob1_batchStep1")
                    .<MarketDto, MarketDto>chunk(chunkSize)
                    .reader(jsonJob1_jsonReader())
                    //쓰는 건 단순히 로그에 출력
                    .writer(MarketDto -> MarketDto.forEach(i -> {
                        log.debug(MarketDto.toString());
                    }))
                    .build();
        }
    
        // reader
        @Bean
        public JsonItemReader<MarketDto> jsonJob1_jsonReader() {
            return new JsonItemReaderBuilder<MarketDto>()
                    .jsonObjectReader(new JacksonJsonObjectReader<>(MarketDto.class))
                    //json파일 가져올 경로
                    .resource(new ClassPathResource("sample/jsonJob1_input.json"))
                    .name("jsonJob1_jsonReader")
                    .build();
        }
    }

    MarketDto 🙂

    package com.test.lsy.batchsimpledbreader.dto;
    
    import lombok.*;
    
    @Getter
    @Setter
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    public class MarketDto {
    
        private String market;
        private String korean_name;
        private String english_name;
    
    }

    jsonJob1_input.json 🤗

    jsonJob1_input.json
    0.03MB


    개인 스터디 기록을 메모하는 공간이라 틀린점이 있을 수 있습니다.

    틀린 점 있을 경우 댓글 부탁드립니다.

    reference: https://www.youtube.com/watch?v=wy99cPHlMlA&list=PLogzC_RPf25HRSG9aO7qKrwbT-EecUMMR

    다음 내용

     

    [springBoot] spring batch JsonReader Filter Write (feat. JSON)

    목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch JsonReader logPrint (feat. JSON) 목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch csvFileReader write new File (feat. file) 목차 아래

    yaga.tistory.com

     

    반응형