瀏覽代碼

category 查询

wangyunpeng 10 月之前
父節點
當前提交
b3347fc2bd

+ 26 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/config/PiaoquanCrawlerMysqlConfiguration.java

@@ -0,0 +1,26 @@
+package com.tzld.longarticle.recommend.server.config;
+
+import com.zaxxer.hikari.HikariDataSource;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.jdbc.core.JdbcTemplate;
+
+
+@Configuration(proxyBeanMethods = false)
+public class PiaoquanCrawlerMysqlConfiguration {
+
+    @Bean(name = "piaoquanCrawlerJdbcTemplate")
+    public JdbcTemplate piaoquanCrawlerJdbcTemplate() {
+        DataSourceProperties dataSourceProperties = new DataSourceProperties();
+        dataSourceProperties.setUsername("crawler");
+        dataSourceProperties.setPassword("crawler123456@");
+        dataSourceProperties.setUrl("jdbc:mysql://rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com:3306/piaoquan-crawler?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull");
+        dataSourceProperties.setDriverClassName("com.mysql.jdbc.Driver");
+        HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
+        dataSource.setMinimumIdle(5);
+        dataSource.setMaximumPoolSize(10);
+        dataSource.setConnectionTestQuery("select 1");
+        return new JdbcTemplate(dataSource);
+    }
+}

+ 1 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/RecommendService.java

@@ -54,6 +54,7 @@ public class RecommendService {
 
     public RankParam convertToRankParam(RecommendParam param, RecallResult recallResult) {
         RankParam rankParam = new RankParam();
+        BeanUtils.copyProperties(param, rankParam);
         rankParam.setRecallResult(recallResult);
         return rankParam;
     }

+ 9 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recall/ContentCategory.java

@@ -0,0 +1,9 @@
+package com.tzld.longarticle.recommend.server.service.recall;
+
+import lombok.Data;
+
+@Data
+public class ContentCategory {
+    private String channelContentId;
+    private String category;
+}

+ 6 - 3
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recall/RecallService.java

@@ -45,9 +45,12 @@ public class RecallService implements ApplicationContextAware {
         List<Future<RecallResult.RecallData>> recallResultFutures = new ArrayList<>();
         for (final RecallStrategy strategy : strategies) {
             Future<RecallResult.RecallData> future = pool.submit(() -> {
-                List<Content> result = strategy.recall(param);
-                cdl.countDown();
-                return new RecallResult.RecallData(result);
+                try {
+                    List<Content> result = strategy.recall(param);
+                    return new RecallResult.RecallData(result);
+                } finally {
+                    cdl.countDown();
+                }
             });
             recallResultFutures.add(future);
         }

+ 33 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recall/strategy/DefaultRecallStrategy.java

@@ -5,13 +5,19 @@ import com.tzld.longarticle.recommend.server.remote.AIGCRemoteService;
 import com.tzld.longarticle.recommend.server.service.filter.FilterParam;
 import com.tzld.longarticle.recommend.server.service.filter.FilterResult;
 import com.tzld.longarticle.recommend.server.service.filter.FilterService;
+import com.tzld.longarticle.recommend.server.service.recall.ContentCategory;
 import com.tzld.longarticle.recommend.server.service.recall.FilterParamFactory;
 import com.tzld.longarticle.recommend.server.service.recall.RecallParam;
 import com.tzld.longarticle.recommend.server.service.recall.RecallStrategy;
+import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
+import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
 import org.springframework.stereotype.Component;
 
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 
 /**
@@ -24,11 +30,15 @@ public class DefaultRecallStrategy implements RecallStrategy {
     private FilterService filterService;
     @Autowired
     private AIGCRemoteService aigcRemoteService;
+    @Autowired
+    NamedParameterJdbcTemplate piaoquanCrawlerJdbcTemplate;
 
     @Override
     public List<Content> recall(RecallParam param) {
 
         List<Content> content = aigcRemoteService.getAllContent(param);
+        // category 查询
+        setContentCategory(content);
         // 处理 content
         FilterParam filterParam = FilterParamFactory.create(param, content);
         FilterResult filterResult = filterService.filter(filterParam);
@@ -38,4 +48,27 @@ public class DefaultRecallStrategy implements RecallStrategy {
                 .collect(Collectors.toList());
     }
 
+    private void setContentCategory(List<Content> contentList) {
+        List<String> channelContentIds = contentList.stream().map(Content::getCrawlerChannelContentId).collect(Collectors.toList());
+        List<ContentCategory> categoryList = getContentCategoryByChannelContentId(channelContentIds);
+        if (CollectionUtils.isEmpty(categoryList)) {
+            return;
+        }
+        Map<String, String> categoryMap = categoryList.stream().collect(Collectors.toMap(ContentCategory::getChannelContentId,
+                ContentCategory::getCategory));
+        for (Content content : contentList) {
+            content.setCategory(categoryMap.get(content.getCrawlerChannelContentId()));
+        }
+    }
+
+    private List<ContentCategory> getContentCategoryByChannelContentId(List<String> channelContentIds) {
+        MapSqlParameterSource parameters = new MapSqlParameterSource();
+        parameters.addValue("channelContentIds", channelContentIds);
+        List<ContentCategory> result = piaoquanCrawlerJdbcTemplate.query(
+                "select content_channel_id, category from cold_start_article_pool where content_channel_id in (:channelContentIds)",
+                parameters,
+                new BeanPropertyRowMapper<>(ContentCategory.class));
+        return result;
+    }
+
 }