From 93b32640342073889ebe3b93c302bdbdb7b3dee2 Mon Sep 17 00:00:00 2001 From: Song-EunJu Date: Thu, 17 Nov 2022 18:12:25 +0900 Subject: [PATCH 1/2] =?UTF-8?q?mod:=20cors=20=ED=97=88=EC=9A=A9=20url=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../java/com/example/goorm/CsvFieldDto.java | 10 ++ .../goorm/FileItemReaderJobConfig.java | 93 +++++++++++++++++++ .../com/example/goorm/GoormApplication.java | 4 +- .../java/com/example/goorm/WebMvcConfig.java | 4 +- .../com/example/goorm/oreum/CsvReader.java | 22 +++++ .../com/example/goorm/oreum/CsvWriter.java | 21 +++++ .../com/example/goorm/oreum/OreumService.java | 4 - 8 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/example/goorm/CsvFieldDto.java create mode 100644 src/main/java/com/example/goorm/FileItemReaderJobConfig.java create mode 100644 src/main/java/com/example/goorm/oreum/CsvReader.java create mode 100644 src/main/java/com/example/goorm/oreum/CsvWriter.java diff --git a/build.gradle b/build.gradle index 19a7d0d..4924076 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,7 @@ dependencies { annotationProcessor 'org.projectlombok:lombok' implementation 'mysql:mysql-connector-java' testImplementation 'org.springframework.boot:spring-boot-starter-test' + implementation 'org.springframework.boot:spring-boot-starter-batch' } tasks.named('test') { diff --git a/src/main/java/com/example/goorm/CsvFieldDto.java b/src/main/java/com/example/goorm/CsvFieldDto.java new file mode 100644 index 0000000..acd516b --- /dev/null +++ b/src/main/java/com/example/goorm/CsvFieldDto.java @@ -0,0 +1,10 @@ +package com.example.goorm; + +public class CsvFieldDto { + private String name; + private String type; + private String pos; +// String name = lineArr[0]; +// String type = lineArr[1].split("/")[1]; +// String pos = lineArr[2]; +} diff --git a/src/main/java/com/example/goorm/FileItemReaderJobConfig.java b/src/main/java/com/example/goorm/FileItemReaderJobConfig.java new file mode 100644 index 0000000..d55e782 --- /dev/null +++ b/src/main/java/com/example/goorm/FileItemReaderJobConfig.java @@ -0,0 +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.repository.OreumRepository; +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 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(); + } + + @Bean + public ItemProcessor csvProcessor() { + return 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]); + + 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; + }; + } + + // csvFileItemReaderStep 이라는 이름의 Step 생성 + @Bean + public Step csvFileItemReaderStep() { + return stepBuilderFactory.get("csvFileItemReaderStep") + .chunk(chunkSize) + // Reader 에서 읽어올 타입 - String, Writer에 넘겨줄 타입이 Oreum + .reader(csvReader.csvFileItemReader()) // 일단 csv에서 String 읽기 + .processor(csvProcessor()) // String을 가공 + .writer(csvWriter) + .build(); + } +} diff --git a/src/main/java/com/example/goorm/GoormApplication.java b/src/main/java/com/example/goorm/GoormApplication.java index 7da0f63..701c59a 100644 --- a/src/main/java/com/example/goorm/GoormApplication.java +++ b/src/main/java/com/example/goorm/GoormApplication.java @@ -1,13 +1,13 @@ package com.example.goorm; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication +@EnableBatchProcessing public class GoormApplication { - public static void main(String[] args) { SpringApplication.run(GoormApplication.class, args); } - } diff --git a/src/main/java/com/example/goorm/WebMvcConfig.java b/src/main/java/com/example/goorm/WebMvcConfig.java index aefe418..d55f0a8 100644 --- a/src/main/java/com/example/goorm/WebMvcConfig.java +++ b/src/main/java/com/example/goorm/WebMvcConfig.java @@ -11,7 +11,7 @@ public class WebMvcConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") - .allowedOrigins("https://9oorm-oreum-frontend.vercel.app/") - .allowedMethods("OPTIONS", "GET", "POST", "PUT", "DELETE"); + .allowedOrigins("http://localhost:3000/", "https://9oorm-oreum-frontend.vercel.app/") + .allowedMethods("GET", "POST"); } } diff --git a/src/main/java/com/example/goorm/oreum/CsvReader.java b/src/main/java/com/example/goorm/oreum/CsvReader.java new file mode 100644 index 0000000..345f701 --- /dev/null +++ b/src/main/java/com/example/goorm/oreum/CsvReader.java @@ -0,0 +1,22 @@ +package com.example.goorm.oreum; + +import lombok.RequiredArgsConstructor; +import org.springframework.batch.item.file.FlatFileItemReader; +import org.springframework.batch.item.file.mapping.DefaultLineMapper; +import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; + +@Configuration +@RequiredArgsConstructor +public class CsvReader { + @Bean + public FlatFileItemReader csvFileItemReader() { + /* file read */ + FlatFileItemReader flatFileItemReader = new FlatFileItemReader<>(); + flatFileItemReader.setResource(new ClassPathResource("C:\\Users\\Windows10\\Desktop\\오름.txt")); + flatFileItemReader.setEncoding("UTF-8"); // encoding + return flatFileItemReader; + } +} \ No newline at end of file diff --git a/src/main/java/com/example/goorm/oreum/CsvWriter.java b/src/main/java/com/example/goorm/oreum/CsvWriter.java new file mode 100644 index 0000000..c86bcb3 --- /dev/null +++ b/src/main/java/com/example/goorm/oreum/CsvWriter.java @@ -0,0 +1,21 @@ +package com.example.goorm.oreum; + +import com.example.goorm.oreum.repository.OreumRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.batch.item.ItemWriter; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; + +@Configuration +@RequiredArgsConstructor +public class CsvWriter implements ItemWriter { + + private final OreumRepository oreumRepository; + + @Override + public void write(List list) throws Exception { + oreumRepository.saveAll(new ArrayList(list)); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/goorm/oreum/OreumService.java b/src/main/java/com/example/goorm/oreum/OreumService.java index c099fe6..c377404 100644 --- a/src/main/java/com/example/goorm/oreum/OreumService.java +++ b/src/main/java/com/example/goorm/oreum/OreumService.java @@ -6,12 +6,10 @@ import com.example.goorm.oreum.repository.OreumRepository; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; import java.io.*; -import java.util.List; @Service @RequiredArgsConstructor @@ -52,9 +50,7 @@ public void readCsv(){ int monthCheck = 1; int dayCheck = 1; - int cnt =0 ; while((line = br.readLine()) != null) { - System.out.println(cnt++); String[] lineArr = line.split(","); String name = lineArr[0]; String type = lineArr[1].split("/")[1]; From ff6fab6e4c78ca004e35ecc2601b84634096ad47 Mon Sep 17 00:00:00 2001 From: Song-EunJu Date: Thu, 17 Nov 2022 18:13:23 +0900 Subject: [PATCH 2/2] =?UTF-8?q?del:=20=ED=95=84=EC=9A=94=EC=97=86=EB=8A=94?= =?UTF-8?q?=20=ED=8C=8C=EC=9D=BC=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/goorm/CsvFieldDto.java | 10 - .../goorm/FileItemReaderJobConfig.java | 186 +++++++++--------- .../com/example/goorm/oreum/CsvReader.java | 44 ++--- .../com/example/goorm/oreum/CsvWriter.java | 42 ++-- 4 files changed, 136 insertions(+), 146 deletions(-) delete mode 100644 src/main/java/com/example/goorm/CsvFieldDto.java diff --git a/src/main/java/com/example/goorm/CsvFieldDto.java b/src/main/java/com/example/goorm/CsvFieldDto.java deleted file mode 100644 index acd516b..0000000 --- a/src/main/java/com/example/goorm/CsvFieldDto.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.example.goorm; - -public class CsvFieldDto { - private String name; - private String type; - private String pos; -// String name = lineArr[0]; -// String type = lineArr[1].split("/")[1]; -// String pos = lineArr[2]; -} diff --git a/src/main/java/com/example/goorm/FileItemReaderJobConfig.java b/src/main/java/com/example/goorm/FileItemReaderJobConfig.java index d55e782..a51184b 100644 --- a/src/main/java/com/example/goorm/FileItemReaderJobConfig.java +++ b/src/main/java/com/example/goorm/FileItemReaderJobConfig.java @@ -1,93 +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.repository.OreumRepository; -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 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(); - } - - @Bean - public ItemProcessor csvProcessor() { - return 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]); - - 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; - }; - } - - // csvFileItemReaderStep 이라는 이름의 Step 생성 - @Bean - public Step csvFileItemReaderStep() { - return stepBuilderFactory.get("csvFileItemReaderStep") - .chunk(chunkSize) - // Reader 에서 읽어올 타입 - String, Writer에 넘겨줄 타입이 Oreum - .reader(csvReader.csvFileItemReader()) // 일단 csv에서 String 읽기 - .processor(csvProcessor()) // String을 가공 - .writer(csvWriter) - .build(); - } -} +//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.repository.OreumRepository; +//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 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(); +// } +// +// @Bean +// public ItemProcessor csvProcessor() { +// return 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]); +// +// 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; +// }; +// } +// +// // csvFileItemReaderStep 이라는 이름의 Step 생성 +// @Bean +// public Step csvFileItemReaderStep() { +// return stepBuilderFactory.get("csvFileItemReaderStep") +// .chunk(chunkSize) +// // Reader 에서 읽어올 타입 - String, Writer에 넘겨줄 타입이 Oreum +// .reader(csvReader.csvFileItemReader()) // 일단 csv에서 String 읽기 +// .processor(csvProcessor()) // String을 가공 +// .writer(csvWriter) +// .build(); +// } +//} diff --git a/src/main/java/com/example/goorm/oreum/CsvReader.java b/src/main/java/com/example/goorm/oreum/CsvReader.java index 345f701..e23145f 100644 --- a/src/main/java/com/example/goorm/oreum/CsvReader.java +++ b/src/main/java/com/example/goorm/oreum/CsvReader.java @@ -1,22 +1,22 @@ -package com.example.goorm.oreum; - -import lombok.RequiredArgsConstructor; -import org.springframework.batch.item.file.FlatFileItemReader; -import org.springframework.batch.item.file.mapping.DefaultLineMapper; -import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.ClassPathResource; - -@Configuration -@RequiredArgsConstructor -public class CsvReader { - @Bean - public FlatFileItemReader csvFileItemReader() { - /* file read */ - FlatFileItemReader flatFileItemReader = new FlatFileItemReader<>(); - flatFileItemReader.setResource(new ClassPathResource("C:\\Users\\Windows10\\Desktop\\오름.txt")); - flatFileItemReader.setEncoding("UTF-8"); // encoding - return flatFileItemReader; - } -} \ No newline at end of file +//package com.example.goorm.oreum; +// +//import lombok.RequiredArgsConstructor; +//import org.springframework.batch.item.file.FlatFileItemReader; +//import org.springframework.batch.item.file.mapping.DefaultLineMapper; +//import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +//import org.springframework.core.io.ClassPathResource; +// +//@Configuration +//@RequiredArgsConstructor +//public class CsvReader { +// @Bean +// public FlatFileItemReader csvFileItemReader() { +// /* file read */ +// FlatFileItemReader flatFileItemReader = new FlatFileItemReader<>(); +// flatFileItemReader.setResource(new ClassPathResource("C:\\Users\\Windows10\\Desktop\\오름.txt")); +// flatFileItemReader.setEncoding("UTF-8"); // encoding +// return flatFileItemReader; +// } +//} \ No newline at end of file diff --git a/src/main/java/com/example/goorm/oreum/CsvWriter.java b/src/main/java/com/example/goorm/oreum/CsvWriter.java index c86bcb3..c6f868a 100644 --- a/src/main/java/com/example/goorm/oreum/CsvWriter.java +++ b/src/main/java/com/example/goorm/oreum/CsvWriter.java @@ -1,21 +1,21 @@ -package com.example.goorm.oreum; - -import com.example.goorm.oreum.repository.OreumRepository; -import lombok.RequiredArgsConstructor; -import org.springframework.batch.item.ItemWriter; -import org.springframework.context.annotation.Configuration; - -import java.util.ArrayList; -import java.util.List; - -@Configuration -@RequiredArgsConstructor -public class CsvWriter implements ItemWriter { - - private final OreumRepository oreumRepository; - - @Override - public void write(List list) throws Exception { - oreumRepository.saveAll(new ArrayList(list)); - } -} \ No newline at end of file +//package com.example.goorm.oreum; +// +//import com.example.goorm.oreum.repository.OreumRepository; +//import lombok.RequiredArgsConstructor; +//import org.springframework.batch.item.ItemWriter; +//import org.springframework.context.annotation.Configuration; +// +//import java.util.ArrayList; +//import java.util.List; +// +//@Configuration +//@RequiredArgsConstructor +//public class CsvWriter implements ItemWriter { +// +// private final OreumRepository oreumRepository; +// +// @Override +// public void write(List list) throws Exception { +// oreumRepository.saveAll(new ArrayList(list)); +// } +//} \ No newline at end of file