Skip to content

Commit

Permalink
feat: csv 파일 DB에 푸시하는 코드 batch 로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Song-EunJu committed Nov 17, 2022
1 parent 0c2634e commit 58ce3e2
Show file tree
Hide file tree
Showing 19 changed files with 170 additions and 263 deletions.
189 changes: 93 additions & 96 deletions src/main/java/com/example/goorm/FileItemReaderJobConfig.java
Original file line number Diff line number Diff line change
@@ -1,96 +1,93 @@
//package com.example.goorm;
//
//import com.example.goorm.oreum.CsvReader;
//import com.example.goorm.oreum.CsvWriter;
//import com.example.goorm.oreum.Oreum;
//import com.example.goorm.oreum.Test;
//import com.example.goorm.oreum.repository.OreumRepository;
//import com.example.goorm.oreum.repository.TestRepository;
//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.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//@Slf4j
//@Configuration
//@RequiredArgsConstructor
//public class FileItemReaderJobConfig {
// private final JobBuilderFactory jobBuilderFactory;
// private final StepBuilderFactory stepBuilderFactory;
//
// private final TestRepository testRepository;
// private final CsvReader csvReader;
// private final CsvWriter csvWriter;
//
// private static final int chunkSize = 366;
//
// private static int monthCheck = 1;
// private static int dayCheck = 1;
//
// // csvFileItemReaderJob 라는 이름의 job 생성
// @Bean
// public Job csvFileItemReaderJob() {
// return jobBuilderFactory.get("csvFileItemReaderJob")
// .start(csvFileItemReaderStep())
// .build();
// }
//
// // csvFileItemReaderStep 이라는 이름의 Step 생성
// @Bean
// public Step csvFileItemReaderStep() {
// return stepBuilderFactory.get("csvFileItemReaderStep")
// .<String, Test>chunk(chunkSize)
// // Reader 에서 읽어올 타입 - String, Writer에 넘겨줄 타입이 Oreum
// .reader(csvReader.csvFileItemReader()) // 일단 csv에서 String 읽기
// .processor(csvProcessor()) // String을 가공
// .writer(csvWriter)
// .build();
// }
// @Bean
// public ItemProcessor<String, Test> csvProcessor() {
// return oreumStr -> {
// System.out.println(oreumStr);
// String[] lineArr = oreumStr.split(",");
// String name = lineArr[0];
// String type = lineArr[1].split("/")[1];
// String pos = lineArr[2];
// System.out.println(name+" "+type+" "+pos);
// double x = Double.parseDouble(pos.split(" ")[0]);
// double y = Double.parseDouble(pos.split(" ")[1]);
// double z = Double.parseDouble(pos.split(" ")[2]);
//
// Test test = Test.builder()
// .name(name)
// .xPos(x)
// .yPos(y)
// .zPos(z)
// .month(monthCheck)
// .day(dayCheck)
// .build();
//
// test.toTypeEnum(type);
// testRepository.save(test);
// if((monthCheck == 1 || monthCheck == 3 || monthCheck == 5 || monthCheck == 7
// || monthCheck == 8 || monthCheck == 10 || monthCheck ==12) && dayCheck == 31) {
// monthCheck++;
// dayCheck = 0;
// }
// else if(monthCheck == 2 && dayCheck == 29){
// monthCheck++;
// dayCheck = 0;
// }
// else if((monthCheck == 4 || monthCheck == 6 || monthCheck ==9 || monthCheck == 11) && dayCheck == 30){
// monthCheck++;
// dayCheck = 0;
// }
// dayCheck++;
// return test;
// };
// }
//
//}
package com.example.goorm;

import com.example.goorm.oreum.file.CsvReader;
import com.example.goorm.oreum.file.CsvWriter;
import com.example.goorm.oreum.domain.Oreum;
import com.example.goorm.oreum.dto.CsvReaderDto;
import com.example.goorm.oreum.repository.OreumRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Test;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class FileItemReaderJobConfig {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;

private final OreumRepository oreumRepository;
private final CsvReader csvReader;
private final CsvWriter csvWriter;

private static final int chunkSize = 366;

private static int monthCheck = 1;
private static int dayCheck = 1;

// csvFileItemReaderJob 라는 이름의 job 생성
@Bean
public Job csvFileItemReaderJob() {
return jobBuilderFactory.get("csvFileItemReaderJob")
.start(csvFileItemReaderStep())
.build();
}

// csvFileItemReaderStep 이라는 이름의 Step 생성
@Bean
public Step csvFileItemReaderStep() {
return stepBuilderFactory.get("csvFileItemReaderStep")
.<String, Test>chunk(chunkSize)
// Reader 에서 읽어올 타입 - String, Writer에 넘겨줄 타입이 Oreum
.reader(csvReader.csvFileItemReader()) // 일단 csv에서 String 읽기
.processor(csvProcessor()) // String을 가공
.writer(csvWriter)
.build();
}
@Bean
public ItemProcessor<CsvReaderDto, Oreum> csvProcessor() {
return csvReaderDto -> {
String name = csvReaderDto.getName();
String type = csvReaderDto.getType().split("/")[1];
String pos = csvReaderDto.getPos();
double x = Double.parseDouble(pos.split(" ")[0]);
double y = Double.parseDouble(pos.split(" ")[1]);
double z = Double.parseDouble(pos.split(" ")[2]);

Oreum oreum = Oreum.builder()
.name(name)
.xPos(x)
.yPos(y)
.zPos(z)
.month(monthCheck)
.day(dayCheck)
.build();

oreum.toTypeEnum(type);
oreumRepository.save(oreum);
if((monthCheck == 1 || monthCheck == 3 || monthCheck == 5 || monthCheck == 7
|| monthCheck == 8 || monthCheck == 10 || monthCheck ==12) && dayCheck == 31) {
monthCheck++;
dayCheck = 0;
}
else if(monthCheck == 2 && dayCheck == 29){
monthCheck++;
dayCheck = 0;
}
else if((monthCheck == 4 || monthCheck == 6 || monthCheck ==9 || monthCheck == 11) && dayCheck == 30){
monthCheck++;
dayCheck = 0;
}
dayCheck++;
return oreum;
};
}

}
20 changes: 0 additions & 20 deletions src/main/java/com/example/goorm/oreum/CsvReader.java

This file was deleted.

23 changes: 0 additions & 23 deletions src/main/java/com/example/goorm/oreum/CsvWriter.java

This file was deleted.

11 changes: 2 additions & 9 deletions src/main/java/com/example/goorm/oreum/OreumController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.goorm.oreum;

import com.example.goorm.oreum.dto.BirthDayRequest;
import com.example.goorm.oreum.dto.BirthdayRequest;
import com.example.goorm.oreum.dto.OreumResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -12,18 +12,11 @@
@CrossOrigin(origins = "*")
@RequiredArgsConstructor
public class OreumController {

private final OreumService oreumService;

@PostMapping("/oreums")
public ResponseEntity<String> pushData(){
oreumService.readCsv();
return ResponseEntity.ok().body("DB 푸시 완료");
}

// 생일에 따른 오름 정보 보여주기 (입력하고 디비에 저장하는 거까지)
@PostMapping("/oreum")
public ResponseEntity<OreumResponse> getOreum(@RequestBody BirthDayRequest request){
public ResponseEntity<OreumResponse> getOreum(@RequestBody BirthdayRequest request){
return ResponseEntity.ok().body(oreumService.getOreum(request));
}

Expand Down
59 changes: 4 additions & 55 deletions src/main/java/com/example/goorm/oreum/OreumService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.goorm.oreum;

import com.example.goorm.oreum.dto.BirthDayRequest;
import com.example.goorm.oreum.domain.MyOreum;
import com.example.goorm.oreum.domain.Oreum;
import com.example.goorm.oreum.dto.BirthdayRequest;
import com.example.goorm.oreum.dto.OreumResponse;
import com.example.goorm.oreum.repository.MyOreumRepository;
import com.example.goorm.oreum.repository.OreumRepository;
Expand All @@ -10,16 +12,14 @@
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import java.io.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class OreumService {
private final OreumRepository oreumRepository;
private final MyOreumRepository myOreumRepository;

public OreumResponse getOreum(BirthDayRequest request){
public OreumResponse getOreum(BirthdayRequest request){
int month = request.getMonth();
int day = request.getDay();
Oreum oreum = oreumRepository.findByBirthday(month, day); // 저장된 오름을 찾아서
Expand All @@ -44,55 +44,4 @@ public OreumResponse getMyOreum(Long myOreumId){
log.info("Oreum id: "+oreumInfo.getId());
return OreumResponse.ofOthers(oreumInfo, myOreum);
}

public void readCsv(){
File csv = new File("C:\\Users\\Windows10\\Desktop\\오름.txt");
BufferedReader br = null;
String line = "";

try {
br = new BufferedReader(new FileReader(csv));
int monthCheck = 1;
int dayCheck = 1;

while((line = br.readLine()) != null) {
String[] lineArr = line.split(",");
String name = lineArr[0];
String type = lineArr[1].split("/")[1];
String pos = lineArr[2];
Double x = Double.parseDouble(pos.split(" ")[0]);
Double y = Double.parseDouble(pos.split(" ")[1]);
Double z = Double.parseDouble(pos.split(" ")[2]);

Oreum oreum = Oreum.builder()
.name(name)
.xPos(x)
.yPos(y)
.zPos(z)
.month(monthCheck)
.day(dayCheck)
.build();

oreum.toTypeEnum(type);
oreumRepository.save(oreum);
if((monthCheck == 1 || monthCheck == 3 || monthCheck == 5 || monthCheck == 7
|| monthCheck == 8 || monthCheck == 10 || monthCheck ==12) && dayCheck == 31) {
monthCheck++;
dayCheck = 0;
}
else if(monthCheck == 2 && dayCheck == 29){
monthCheck++;
dayCheck = 0;
}
else if((monthCheck == 4 || monthCheck == 6 || monthCheck ==9 || monthCheck == 11) && dayCheck == 30){
monthCheck++;
dayCheck = 0;
}
dayCheck++;
}

} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
39 changes: 0 additions & 39 deletions src/main/java/com/example/goorm/oreum/Test.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.goorm;
package com.example.goorm.oreum.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
Expand Down
Loading

0 comments on commit 58ce3e2

Please sign in to comment.