IT/Live Coding

[springBoot] spring batch FlatFileItemWriter(ν…ŒμŠ€νŠΈ μ˜μƒ & μ†ŒμŠ€μ½”λ“œ 포함)

μ•Œ 수 μ—†λŠ” μ‚¬μš©μž 2023. 11. 16.

μ•„λž˜ ν¬μŠ€νŒ…μ—μ„œ 이어진 λ‚΄μš©μž…λ‹ˆλ‹€.

 

[springBoot] spring batch step startLimit

μ•„λž˜ ν¬μŠ€νŒ…μ—μ„œ 이어진 λ‚΄μš©μž…λ‹ˆλ‹€. [springBoot] spring batch JobScope, StepScope λͺ©μ°¨ μ•„λž˜ ν¬μŠ€νŒ…μ—μ„œ 이어진 λ‚΄μš©μž…λ‹ˆλ‹€. [springBoot] spring batch allowStartIfComplete λͺ©μ°¨ μ•„λž˜ ν¬μŠ€νŒ…μ—μ„œ 이어진 λ‚΄μš©μž…λ‹ˆλ‹€.

yaga.tistory.com


객체의 데이터λ₯Ό μ½μ–΄μ„œ delimited, formatted 방식을 μ μš©ν•΄μ„œ νŒŒμΌμ— μ“°λŠ” 예제

JobTestConfig

package com.dev.lsy.springbatchremind.batch;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.ExitStatus;
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.JobScope;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
import org.springframework.batch.item.json.JacksonJsonObjectMarshaller;
import org.springframework.batch.item.json.builder.JsonFileItemWriterBuilder;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Configuration
public class JobTestConfig {

    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;

    @Bean
    //job
    public Job job1() {
        return jobBuilderFactory.get("job1")
                //jobParameter μžλ™ 증감
                .incrementer(new RunIdIncrementer())
                .start(step1())
                .build();
    }

    @Bean
    //step
    public Step step1() {
        return stepBuilderFactory.get("step1")
                //chunk 기반 μž‘μ—…
                .<Person, Person>chunk(10)
                .reader(customReader())
                //μ—¬κΈ°λ§Œ λ‹€λ₯΄λ‹€.
                .writer(customWriter())
                .build();
    }

    //reader
    @Bean
    public ListItemReader customReader() {

        //ν…ŒμŠ€νŠΈ 리슀트(μ—¬κΈ°λŠ” 동일)
        List<Person> list = Arrays.asList(
                new Person(1L, "아이언맨", 51),
                new Person(2L, "ν† λ₯΄", 10000),
                new Person(3L, "헐크", 52)
            );
        ListItemReader<Person> reader = new ListItemReader<>(list);

        return reader;
    }

    @Bean
    public ItemWriter<? super Person>  customWriter() {
        return new FlatFileItemWriterBuilder<Person>()
                .name("flatFileWriter")
                .resource(new FileSystemResource("output/output.txt"))
                .formatted()
                //id 2μžλŠ” 숫자(d), name은 6자 문자(s), age도 숫자(d)
                .format("%-2d%-6s%-2d")
                //.delimited()		//κ΅¬λΆ„μž 방식은 이 μ˜΅μ…˜μ΄ μΆ”κ°€λœλ‹€.
                //.delimiter("#")	
                .names(new String[]{"id", "name", "age"})
                .build();
    }
}

개인 μŠ€ν„°λ”” 기둝을 λ©”λͺ¨ν•˜λŠ” 곡간이라 틀린점이 μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.

ν‹€λ¦° 점 μžˆμ„ 경우 λŒ“κΈ€ λΆ€νƒλ“œλ¦½λ‹ˆλ‹€.

reference: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EB%B0%B0%EC%B9%98


λ‹€μŒ λ‚΄μš©

 

[springBoot] spring batch jsonFileItemWriter

λͺ©μ°¨ μ•„λž˜ ν¬μŠ€νŒ…μ—μ„œ 이어진 λ‚΄μš©μž…λ‹ˆλ‹€. [springBoot] spring batch FlatFileItemWriter λͺ©μ°¨ μ•„λž˜ ν¬μŠ€νŒ…μ—μ„œ 이어진 λ‚΄μš©μž…λ‹ˆλ‹€. [springBoot] spring batch step startLimit μ•„λž˜ ν¬μŠ€νŒ…μ—μ„œ 이어진 λ‚΄μš©μž…λ‹ˆλ‹€. [spri

yaga.tistory.com

λŒ“κΈ€