|
7 | 7 | import com.going.server.domain.word.entity.Word; |
8 | 8 | import com.going.server.domain.word.repository.WordRepository; |
9 | 9 | import jakarta.annotation.PostConstruct; |
| 10 | +import java.util.ArrayList; |
10 | 11 | import lombok.RequiredArgsConstructor; |
11 | 12 | import lombok.extern.slf4j.Slf4j; |
12 | 13 | import org.springframework.beans.factory.annotation.Value; |
@@ -54,52 +55,51 @@ public String callFastApi() { |
54 | 55 |
|
55 | 56 | @Profile("!test") |
56 | 57 | public void setCluster() { |
57 | | - // FastAPI 요청 데이터 (필요시 변경) |
58 | 58 | Map<String, Object> requestData = Map.of("input_text", "클러스터링할 데이터 예제"); |
59 | 59 |
|
60 | | - // FastAPI 응답 받기 |
61 | 60 | Map<String, Object> response = webClient.post() |
62 | | - .uri(baseUrl + "/api/cluster") // FastAPI 클러스터링 엔드포인트 |
| 61 | + .uri(baseUrl + "/api/cluster") |
63 | 62 | .bodyValue(requestData) |
64 | 63 | .retrieve() |
65 | 64 | .bodyToMono(Map.class) |
66 | 65 | .block(); |
67 | 66 |
|
68 | | - //모든 클러스터링 결과 저장 |
69 | 67 | List<Map<String, Object>> clusters = (List<Map<String, Object>>) response.get("clusters"); |
70 | | - //클러스터링 결과 이미지 저장 |
71 | 68 | String imageUrl = response.get("image_url").toString(); |
72 | 69 |
|
| 70 | + // 병렬처리를 위한 리스트 |
| 71 | + List<Cluster> clusterEntities = new ArrayList<>(); |
| 72 | + List<Word> wordEntities = new ArrayList<>(); |
| 73 | + List<Sentence> sentenceEntities = new ArrayList<>(); |
| 74 | + |
73 | 75 | for (Map<String, Object> cluster : clusters) { |
74 | 76 | Map<String, List<String>> wordSentences = (Map<String, List<String>>) cluster.get("word_sentences"); |
75 | 77 |
|
76 | | - if (wordSentences.isEmpty()) continue; // 빈 클러스터 예외 처리 |
| 78 | + if (wordSentences.isEmpty()) continue; |
77 | 79 |
|
78 | | - //첫 번째 단어를 대표 어휘로 설정 |
79 | 80 | String representWord = wordSentences.keySet().iterator().next(); |
80 | | - //엔티티 저장 |
81 | 81 | Cluster clusterEntity = Cluster.toEntity(representWord, imageUrl); |
82 | | - //클러스터 결과 DB에 저장 |
83 | | - Cluster saveCluster = clusterRepository.save(clusterEntity); |
| 82 | + clusterEntities.add(clusterEntity); |
84 | 83 |
|
85 | 84 | for (Map.Entry<String, List<String>> entry : wordSentences.entrySet()) { |
86 | | - //단어 |
87 | | - String word = entry.getKey(); |
88 | | - //문장들 |
89 | | - List<String> sentences = entry.getValue(); |
90 | | - //Word 엔티티 생성 |
91 | | - Word wordEntity = Word.toEntity(word, saveCluster); |
92 | | - //DB에 저장 |
93 | | - Word saveWord = wordRepository.save(wordEntity); |
94 | | - for (String sentence : sentences) { |
95 | | - //Sentence 엔티티 생성 |
96 | | - Sentence sententEntity = Sentence.toEntity(sentence, saveWord); |
97 | | - sentenceRepository.save(sententEntity); |
| 85 | + Word wordEntity = Word.toEntity(entry.getKey(), clusterEntity); |
| 86 | + wordEntities.add(wordEntity); |
| 87 | + |
| 88 | + for (String sentence : entry.getValue()) { |
| 89 | + sentenceEntities.add(Sentence.toEntity(sentence, wordEntity)); |
98 | 90 | } |
99 | 91 | } |
100 | 92 | } |
| 93 | + |
| 94 | + // 1. 클러스터 저장 |
| 95 | + clusterRepository.saveAll(clusterEntities); |
| 96 | + |
| 97 | + // 2. 클러스터 저장 후 Word에 연결된 객체들을 저장 |
| 98 | + wordRepository.saveAll(wordEntities); |
| 99 | + sentenceRepository.saveAll(sentenceEntities); |
101 | 100 | } |
102 | 101 |
|
| 102 | + |
103 | 103 | public Word testWord(String word) { |
104 | 104 | Word wordEntity = Word.toEntity(word, null); |
105 | 105 | return wordRepository.save(wordEntity); |
|
0 commit comments