Browse Source

aigc数据库连接 文章品类获取

wangyunpeng 8 months ago
parent
commit
a69b4d64a0

+ 65 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/config/db/AigcDBConfig.java

@@ -0,0 +1,65 @@
+package com.tzld.longarticle.recommend.server.config.db;
+
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.jdbc.DataSourceBuilder;
+import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
+import org.springframework.orm.jpa.JpaTransactionManager;
+import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+
+import javax.persistence.EntityManagerFactory;
+import javax.sql.DataSource;
+import java.util.HashMap;
+import java.util.Map;
+
+@Configuration
+@EnableTransactionManagement
+@EnableJpaRepositories(
+        basePackages = "com.tzld.longarticle.recommend.server.repository.aigc",
+        entityManagerFactoryRef = "aigcEntityManagerFactory",
+        transactionManagerRef = "aigcTransactionManager"
+)
+public class AigcDBConfig {
+
+    @Value("${spring.jpa.aigc.hibernate.ddl-auto}")
+    private String ddlAuto;
+    @Value("${spring.jpa.aigc.database}")
+    private String database;
+
+    public Map<String, Object> hibernateProperties() {
+        Map<String, Object> properties = new HashMap<>();
+        properties.put("hibernate.ddl-auto", ddlAuto);
+        properties.put("database", database);
+        return properties;
+    }
+
+    @Bean(name = "aigcDataSource")
+    @ConfigurationProperties(prefix = "spring.datasource.aigc")
+    public DataSource aigcDataSource() {
+        return DataSourceBuilder.create().build();
+    }
+
+    @Bean(name = "aigcEntityManagerFactory")
+    public LocalContainerEntityManagerFactoryBean aigcEntityManagerFactory(EntityManagerFactoryBuilder builder,
+                                                                                @Qualifier("aigcDataSource") DataSource dataSource) {
+        return builder
+                .dataSource(dataSource)
+                .packages("com.tzld.longarticle.recommend.server.repository.entity.aigc") // 实体类包路径
+                .persistenceUnit("aigc")
+                .properties(hibernateProperties())
+                .build();
+    }
+
+    @Bean(name = "aigcTransactionManager")
+    public PlatformTransactionManager aigcTransactionManager(
+            @Qualifier("aigcEntityManagerFactory") EntityManagerFactory aigcEntityManagerFactory) {
+        return new JpaTransactionManager(aigcEntityManagerFactory);
+    }
+}
+

+ 35 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/config/mybatis/AigcMybatisConfig.java

@@ -0,0 +1,35 @@
+package com.tzld.longarticle.recommend.server.config.mybatis;
+
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.mybatis.spring.SqlSessionFactoryBean;
+import org.mybatis.spring.SqlSessionTemplate;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
+
+import javax.sql.DataSource;
+
+@Configuration
+@MapperScan(basePackages = "com.tzld.longarticle.recommend.server.repository.mapper.aigc",
+        sqlSessionFactoryRef = "aigcSqlSessionFactory")
+public class AigcMybatisConfig {
+
+    @Bean(name = "aigcSqlSessionFactory")
+    public SqlSessionFactory aigcSqlSessionFactory(@Qualifier("aigcDataSource") DataSource dataSource) throws Exception {
+        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
+        sessionFactory.setDataSource(dataSource);
+        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
+                .getResources("classpath*:mapper/**/*.xml"));
+        sessionFactory.setTypeAliasesPackage("com.tzld.longarticle.recommend.server");
+        sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
+        sessionFactory.getObject().getConfiguration().setUseGeneratedKeys(true);
+        return sessionFactory.getObject();
+    }
+
+    @Bean(name = "aigcSqlSessionTemplate")
+    public SqlSessionTemplate aigcSqlSessionTemplate(@Qualifier("aigcSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
+        return new SqlSessionTemplate(sqlSessionFactory);
+    }
+}

+ 14 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/aigc/CrawlerMetaArticleRepository.java

@@ -0,0 +1,14 @@
+package com.tzld.longarticle.recommend.server.repository.aigc;
+
+import com.tzld.longarticle.recommend.server.repository.entity.aigc.CrawlerMetaArticle;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+@Repository
+public interface CrawlerMetaArticleRepository extends JpaRepository<CrawlerMetaArticle, Long> {
+
+    List<CrawlerMetaArticle> getByChannelContentIdIn(List<String>channelContentIds);
+
+}

+ 53 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/entity/aigc/CrawlerMetaArticle.java

@@ -0,0 +1,53 @@
+package com.tzld.longarticle.recommend.server.repository.entity.aigc;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import java.io.Serializable;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Entity
+@Table(name = "crawler_meta_article")
+public class CrawlerMetaArticle implements Serializable {
+
+    @Id
+    @Column(name = "article_id")
+    private Long article_id;
+    @Column(name = "platform")
+    private String platform;
+    @Column(name = "mode")
+    private String mode;
+    @Column(name = "category")
+    private String category;
+    @Column(name = "out_account_id")
+    private String outAccountId;
+    @Column(name = "title")
+    private String title;
+    @Column(name = "link")
+    private String link;
+    @Column(name = "read_cnt")
+    private Integer readCnt;
+    @Column(name = "like_cnt")
+    private Integer likeCnt;
+    @Column(name = "description")
+    private String description;
+    @Column(name = "publish_time")
+    private Long publishTime;
+    @Column(name = "crawler_time")
+    private Long crawlerTime;
+    @Column(name = "score")
+    private Double score;
+    @Column(name = "status")
+    private Integer status;
+    @Column(name = "channel_content_id")
+    private String channelContentId;
+    @Column(name = "unique_index")
+    private String uniqueIndex;
+}

+ 5 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/mapper/aigc/AigcBaseMapper.java

@@ -0,0 +1,5 @@
+package com.tzld.longarticle.recommend.server.repository.mapper.aigc;
+
+public interface AigcBaseMapper {
+
+}

+ 8 - 7
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recall/strategy/ColdStartBackupRecallStrategy.java

@@ -2,7 +2,8 @@ package com.tzld.longarticle.recommend.server.service.recall.strategy;
 
 import com.tzld.longarticle.recommend.server.model.Content;
 import com.tzld.longarticle.recommend.server.remote.AIGCRemoteService;
-import com.tzld.longarticle.recommend.server.repository.mapper.crawler.CrawlerBaseMapper;
+import com.tzld.longarticle.recommend.server.repository.aigc.CrawlerMetaArticleRepository;
+import com.tzld.longarticle.recommend.server.repository.entity.aigc.CrawlerMetaArticle;
 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;
@@ -27,7 +28,7 @@ public class ColdStartBackupRecallStrategy implements RecallStrategy {
     @Autowired
     private AIGCRemoteService aigcRemoteService;
     @Autowired
-    CrawlerBaseMapper crawlerBaseMapper;
+    CrawlerMetaArticleRepository crawlerMetaArticleRepository;
 
     @Override
     public RecallResult.RecallData recall(RecallParam param) {
@@ -50,22 +51,22 @@ public class ColdStartBackupRecallStrategy implements RecallStrategy {
 
     private void setContentCategory(List<Content> contentList) {
         List<String> channelContentIds = contentList.stream().map(Content::getCrawlerChannelContentId).collect(Collectors.toList());
-        List<ContentCategory> categoryList = getContentCategoryByChannelContentId(channelContentIds);
+        List<CrawlerMetaArticle> categoryList = getContentCategoryByChannelContentId(channelContentIds);
         if (CollectionUtils.isEmpty(categoryList)) {
             return;
         }
-        Map<String, List<String>> categoryMap = categoryList.stream().collect(Collectors.groupingBy(ContentCategory::getContentChannelId,
-                Collectors.mapping(ContentCategory::getCategory, Collectors.toList())));
+        Map<String, List<String>> categoryMap = categoryList.stream().collect(Collectors.groupingBy(CrawlerMetaArticle::getChannelContentId,
+                Collectors.mapping(CrawlerMetaArticle::getCategory, Collectors.toList())));
         for (Content content : contentList) {
             content.setCategory(categoryMap.get(content.getCrawlerChannelContentId()));
         }
     }
 
-    private List<ContentCategory> getContentCategoryByChannelContentId(List<String> channelContentIds) {
+    private List<CrawlerMetaArticle> getContentCategoryByChannelContentId(List<String> channelContentIds) {
         if (CollectionUtils.isEmpty(channelContentIds)) {
             return new ArrayList<>();
         }
-        return crawlerBaseMapper.getContentCategory(channelContentIds);
+        return crawlerMetaArticleRepository.getByChannelContentIdIn(channelContentIds);
     }
 
 }

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

@@ -2,7 +2,8 @@ package com.tzld.longarticle.recommend.server.service.recall.strategy;
 
 import com.tzld.longarticle.recommend.server.model.Content;
 import com.tzld.longarticle.recommend.server.remote.AIGCRemoteService;
-import com.tzld.longarticle.recommend.server.repository.mapper.crawler.CrawlerBaseMapper;
+import com.tzld.longarticle.recommend.server.repository.aigc.CrawlerMetaArticleRepository;
+import com.tzld.longarticle.recommend.server.repository.entity.aigc.CrawlerMetaArticle;
 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;
@@ -27,7 +28,7 @@ public class DefaultRecallStrategy implements RecallStrategy {
     @Autowired
     private AIGCRemoteService aigcRemoteService;
     @Autowired
-    CrawlerBaseMapper crawlerBaseMapper;
+    CrawlerMetaArticleRepository crawlerMetaArticleRepository;
 
     @Override
     public RecallResult.RecallData recall(RecallParam param) {
@@ -48,22 +49,22 @@ public class DefaultRecallStrategy implements RecallStrategy {
 
     private void setContentCategory(List<Content> contentList) {
         List<String> channelContentIds = contentList.stream().map(Content::getCrawlerChannelContentId).collect(Collectors.toList());
-        List<ContentCategory> categoryList = getContentCategoryByChannelContentId(channelContentIds);
+        List<CrawlerMetaArticle> categoryList = getContentCategoryByChannelContentId(channelContentIds);
         if (CollectionUtils.isEmpty(categoryList)) {
             return;
         }
-        Map<String, List<String>> categoryMap = categoryList.stream().collect(Collectors.groupingBy(ContentCategory::getContentChannelId,
-                Collectors.mapping(ContentCategory::getCategory, Collectors.toList())));
+        Map<String, List<String>> categoryMap = categoryList.stream().collect(Collectors.groupingBy(CrawlerMetaArticle::getChannelContentId,
+                Collectors.mapping(CrawlerMetaArticle::getCategory, Collectors.toList())));
         for (Content content : contentList) {
             content.setCategory(categoryMap.get(content.getCrawlerChannelContentId()));
         }
     }
 
-    private List<ContentCategory> getContentCategoryByChannelContentId(List<String> channelContentIds) {
+    private List<CrawlerMetaArticle> getContentCategoryByChannelContentId(List<String> channelContentIds) {
         if (CollectionUtils.isEmpty(channelContentIds)) {
             return new ArrayList<>();
         }
-        return crawlerBaseMapper.getContentCategory(channelContentIds);
+        return crawlerMetaArticleRepository.getByChannelContentIdIn(channelContentIds);
     }
 
 }

+ 15 - 0
long-article-recommend-service/src/main/resources/application-dev.yml

@@ -36,6 +36,17 @@ spring:
         maximum-pool-size: 10
         auto-commit: true
         idle-timeout: 30000
+    aigc:
+      jdbc-url: jdbc:mysql://rm-t4na9qj85v7790tf84o.mysql.singapore.rds.aliyuncs.com:3306/aigc-admin-prod?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
+      username: crawler_admin
+      password: cyber#crawler_2023
+      driver-class-name: com.mysql.jdbc.Driver
+      hikari:
+        connection-timeout: 30000
+        minimum-idle: 5
+        maximum-pool-size: 10
+        auto-commit: true
+        idle-timeout: 30000
   jpa:
     crawler:
       hibernate:
@@ -45,6 +56,10 @@ spring:
       hibernate:
         ddl-auto: validate
       database: mysql
+    aigc:
+      hibernate:
+        ddl-auto: validate
+      database: mysql
 
 apollo:
   meta: http://devapolloconfig-internal.piaoquantv.com

+ 15 - 0
long-article-recommend-service/src/main/resources/application-pre.yml

@@ -106,6 +106,17 @@ spring:
         maximum-pool-size: 10
         auto-commit: true
         idle-timeout: 30000
+    aigc:
+      jdbc-url: jdbc:mysql://rm-t4na9qj85v7790tf84o.mysql.singapore.rds.aliyuncs.com:3306/aigc-admin-prod?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
+      username: crawler_admin
+      password: cyber#crawler_2023
+      driver-class-name: com.mysql.jdbc.Driver
+      hikari:
+        connection-timeout: 30000
+        minimum-idle: 5
+        maximum-pool-size: 10
+        auto-commit: true
+        idle-timeout: 30000
   jpa:
     crawler:
       hibernate:
@@ -115,6 +126,10 @@ spring:
       hibernate:
         ddl-auto: validate
       database: mysql
+    aigc:
+      hibernate:
+        ddl-auto: validate
+      database: mysql
 
 xxl:
   job:

+ 15 - 0
long-article-recommend-service/src/main/resources/application-prod.yml

@@ -25,6 +25,17 @@ spring:
         maximum-pool-size: 10
         auto-commit: true
         idle-timeout: 30000
+    aigc:
+      jdbc-url: jdbc:mysql://rm-t4na9qj85v7790tf84o.mysql.singapore.rds.aliyuncs.com:3306/aigc-admin-prod?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
+      username: crawler_admin
+      password: cyber#crawler_2023
+      driver-class-name: com.mysql.jdbc.Driver
+      hikari:
+        connection-timeout: 30000
+        minimum-idle: 5
+        maximum-pool-size: 10
+        auto-commit: true
+        idle-timeout: 30000
   jpa:
     crawler:
       hibernate:
@@ -34,6 +45,10 @@ spring:
       hibernate:
         ddl-auto: validate
       database: mysql
+    aigc:
+      hibernate:
+        ddl-auto: validate
+      database: mysql
 
 apollo:
   meta: http://apolloconfig-internal.piaoquantv.com

+ 15 - 0
long-article-recommend-service/src/main/resources/application-test.yml

@@ -96,6 +96,17 @@ spring:
         maximum-pool-size: 10
         auto-commit: true
         idle-timeout: 30000
+    aigc:
+      jdbc-url: jdbc:mysql://rm-t4na9qj85v7790tf84o.mysql.singapore.rds.aliyuncs.com:3306/aigc-admin-prod?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
+      username: crawler_admin
+      password: cyber#crawler_2023
+      driver-class-name: com.mysql.jdbc.Driver
+      hikari:
+        connection-timeout: 30000
+        minimum-idle: 5
+        maximum-pool-size: 10
+        auto-commit: true
+        idle-timeout: 30000
   jpa:
     crawler:
       hibernate:
@@ -105,6 +116,10 @@ spring:
       hibernate:
         ddl-auto: validate
       database: mysql
+    aigc:
+      hibernate:
+        ddl-auto: validate
+      database: mysql
 
 xxl:
   job:

+ 6 - 0
long-article-recommend-service/src/main/resources/mapper/aigc/AigcBaseMapper.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.tzld.longarticle.recommend.server.repository.mapper.aigc.AigcBaseMapper">
+
+
+</mapper>