티스토리 뷰
반응형
Spring Batch를 사용하여 CSV 파일을 읽고, 데이터를 처리한 다음, 데이터베이스에 저장하는 기본적인 구조입니다.
✅ 기술 스택
- Spring Boot
- Spring Batch
- H2 (In-Memory DB)
- CSV 파일 Reader
- Maven
📁 프로젝트 구조
spring-batch-demo/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com.example.batch/
│ │ │ ├── SpringBatchDemoApplication.java
│ │ │ ├── config/BatchConfig.java
│ │ │ ├── model/Person.java
│ │ │ ├── processor/PersonItemProcessor.java
│ │ │ └── writer/ConsoleItemWriter.java
│ │ └── resources/
│ │ ├── application.yml
│ │ └── sample-data.csv
1. Person 모델 클래스
package com.example.batch.model;
public class Person {
private String firstName;
private String lastName;
// getters, setters, toString
}
2. PersonItemProcessor - 데이터 가공
package com.example.batch.processor;
import com.example.batch.model.Person;
import org.springframework.batch.item.ItemProcessor;
public class PersonItemProcessor implements ItemProcessor<Person, Person> {
@Override
public Person process(Person person) {
person.setFirstName(person.getFirstName().toUpperCase());
person.setLastName(person.getLastName().toUpperCase());
return person;
}
}
3. ConsoleItemWriter - 결과 출력
package com.example.batch.writer;
import com.example.batch.model.Person;
import org.springframework.batch.item.ItemWriter;
import java.util.List;
public class ConsoleItemWriter implements ItemWriter<Person> {
@Override
public void write(List<? extends Person> items) {
items.forEach(System.out::println);
}
}
4. BatchConfig 설정
package com.example.batch.config;
import com.example.batch.model.Person;
import com.example.batch.processor.PersonItemProcessor;
import com.example.batch.writer.ConsoleItemWriter;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.*;
import org.springframework.batch.item.file.*;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.mapping.*;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.context.annotation.*;
import org.springframework.core.io.ClassPathResource;
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Bean
public FlatFileItemReader<Person> reader() {
return new FlatFileItemReaderBuilder<Person>()
.name("personItemReader")
.resource(new ClassPathResource("sample-data.csv"))
.delimited()
.names("firstName", "lastName")
.fieldSetMapper(new BeanWrapperFieldSetMapper<>() {{
setTargetType(Person.class);
}})
.build();
}
@Bean
public PersonItemProcessor processor() {
return new PersonItemProcessor();
}
@Bean
public ConsoleItemWriter writer() {
return new ConsoleItemWriter();
}
@Bean
public Job importUserJob(JobBuilderFactory jobBuilders, Step step1) {
return jobBuilders.get("importUserJob")
.start(step1)
.build();
}
@Bean
public Step step1(StepBuilderFactory stepBuilders) {
return stepBuilders.get("step1")
.<Person, Person>chunk(5)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
}
}
5. application.yml
spring:
batch:
job:
enabled: true
initialize-schema: always
6. sample-data.csv
John,Doe
Jane,Smith
7. 메인 클래스
package com.example.batch;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBatchDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBatchDemoApplication.class, args);
}
}
▶ 실행 결과
Person{firstName='JOHN', lastName='DOE'}
Person{firstName='JANE', lastName='SMITH'}
반응형
'배움 > JAVA' 카테고리의 다른 글
| Eclipse JAVA, Gradle, JPA 설정 (0) | 2025.06.27 |
|---|---|
| Srping boot + gradle + jpa 설정 (0) | 2025.06.25 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- eclipse
- WSL2
- jp:a
- PYTHON
- 설치
- mysql
- OpenAI GPT
- facades
- eloquent
- laravel 11
- Laravel
- flask
- ob_get_contents
- swagger
- 설정
- createfromformat
- array_combine
- laravel 12
- wsl
- privatechannel
- uniqid
- php
- ubuntu
- 비동기
- curl_multi_init
- 명령어
- laravel 테스트
- fromArray
- call_user_func
- reflectionclass
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
글 보관함
