IT/Live Coding

[springBoot] spring batch JsonReader Filter Write (테스트 영상 & 소스코드 포함)

알 수 없는 사용자 2023. 11. 11.

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

 

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

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

yaga.tistory.com


JSON 형식의 데이터를 읽어서 필터링을 해서 새로운 JSON 데이터를 만드는 예제

JsonJob2 🙂

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.ItemProcessor;
import org.springframework.batch.item.json.JacksonJsonObjectMarshaller;
import org.springframework.batch.item.json.JacksonJsonObjectReader;
import org.springframework.batch.item.json.JsonFileItemWriter;
import org.springframework.batch.item.json.JsonItemReader;
import org.springframework.batch.item.json.builder.JsonFileItemWriterBuilder;
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;
import org.springframework.core.io.FileSystemResource;

@Slf4j
@RequiredArgsConstructor
@Configuration
public class JsonJob2 {

    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;
    private static final int chunkSize = 5;

    //job을 만들고 한번에 성공하자 이번엔
    @Bean
    public Job JsonJob2_batchBuild() {
        return jobBuilderFactory.get("JsonJob2_batchBuild").start(JsonJob2_batchStep1()).build();
    }

    //step
    @Bean
    public Step JsonJob2_batchStep1() {
        return stepBuilderFactory.get("JsonJob2_batchStep2")
                .<MarketDto, MarketDto>chunk(chunkSize)
                .reader(JsonJob2_jsonReader())
                //필터링 해서 writer에서 넘김
                .processor(jsonJob2_processor())
                .writer((jsonJob2_jsonWriter()))
                .build();
    }

    //필터링할 프로세서
    private ItemProcessor<MarketDto, MarketDto> jsonJob2_processor() {
        return MarketDto -> {
            //조건 처리
            if (MarketDto.getMarket().startsWith("USDT-")) {
                //새로 생성해서 반환
                return new MarketDto(MarketDto.getMarket(), MarketDto.getKorean_name(), MarketDto.getEnglish_name());
            }  else {
                return null;
            }
        };
    }


    //json reader
    @Bean
    public JsonItemReader<MarketDto> JsonJob2_jsonReader() {
        return new JsonItemReaderBuilder<MarketDto>()
                .jsonObjectReader(new JacksonJsonObjectReader<>(MarketDto.class))
                .resource(new ClassPathResource("sample/jsonJob1_input.json"))
                .name("JsonJob2_jsonReader")
                .build();
    }

    //json writer
    @Bean
    public JsonFileItemWriter<MarketDto> jsonJob2_jsonWriter() {
        return new JsonFileItemWriterBuilder<MarketDto>()
                .jsonObjectMarshaller(new JacksonJsonObjectMarshaller<>())
                //쓸 경로
                .resource(new FileSystemResource("output/jsonJob2_output.json"))
                .name("jsonJob2_jsonWriter")
                .build();
    }
}

동영상에서 설명은 잘못됨(로그 출력하는거로 했음)


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

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

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


다음 내용

 

[springBoot] spring scheduler simpleBatch (feat. scheduler)

목차 Batch1 🙂 package com.dev.lsy.springbatchlog.batch; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.co

yaga.tistory.com

댓글