1 Star 1 Fork 0

calms / MovieSystem-Server

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

MovieSystem-Server

电影推荐系统-后端

项目演示视频地址:https://www.bilibili.com/video/BV1tm4y1h7EQ/

系统设计

架构设计

image

数据处理流程

image

服务拆分

服务名 功能描述
搜索服务 使用ElasticSearch实现电影的多字段模糊搜索
推荐服务 使用Spark实现电影的离线推荐和实时推荐
电影详情服务 获取电影的具体信息、评论评分
用户服务 用户登录、注册
短信邮件服务 使用短信或邮件发送验证码
网关服务 请求分发、鉴权

数据库设计

image

数据库优化

MySQL-5.7.22

索引优化

movie_reviews

表数据量:1050170

对评论表中的douban_id字段加上普通索引,适用场景:用户查看某电影的评论

SELECT
	review_id,
	douban_id,
	…… 
FROM
	movie_recommendation.movie_reviews 
WHERE
	douban_id = 1291543
ORDER BY
	user_movie_rating_agree DESC,
	user_movie_rating_time DESC

image

使用索引前(10次平均/ms) 使用索引后(10次平均/ms)
58ms(第一次查询时4.434s) 18ms

第一次很慢的原因,全表扫描;

第二次之后快是由于MySQL将查询语句和结果放到了缓存中

movie_detail

表数据量:8715

对电影表中的title字段加上普通索引,适用场景:用户填写关键字,查询相关电影的详情

SELECT
	douban_id,
	title,
 	……
FROM
	movie_recommendation.movie_detail 
WHERE
	title like '星际%';
使用索引前(10次平均/ms) 使用索引后(10次平均/ms)
42ms 22ms

电影搜索优化——ElasticSearch

需求:

用户输入一些关键字去搜索,返回在电影名、演员、导演与之匹配的结果。

使用MySQL

SELECT
	douban_id,
	title,
 	……
FROM
	movie_recommendation.movie_detail 
WHERE
	title like '%星际%' or casts like '%星际%' or directors like '%星际%';

问题:

  • 平均用时:56ms
  • 使用以%开头的like查询,无法使用索引,走全表扫描
  • 返回的结果较多时,无法判断那个是更符合搜索条件的更优结果!

使用ES

{
  "query": {
    "multi_match": {
      "query": "星际",
      "minimum_should_match": "80%", //查询结果至少要包含【分次数*80%】向下取整的数量
      "fields": ["title^5","casts^3","directors^2"] // 查询字段并添加权重
    }
  }
}

image

结果:

  • 平均用时:7ms,提升巨大!

  • 优势:

    • 查询结果默认是Score降序的,即匹配度的降序;
    • 可以给字段设置权重,比如这里就给title设置较高的权重,说明匹配度中title字段的影响要稍大一些!

项目中使用ES存在的问题

问题:

  • ES主要用来做搜索,不适合做增删改,但是我们的表中的字段如评分(ratingScore)、评分数(ratingScore)是在不停变化的;
  • 评分一般都是在MySQL中操作,但我又不想格外写一段逻辑去操作ES!

解决方案:

  • 使用了Canal,MySQL中数据变化时增量同步到ES中
  • canal版本:1.1.5

配置Canal中遇到的问题:

  • 配置adapter的application.yml文件

    • outerAdapters的name要指明es6还是es7;

    • mode为rest时hosts要加http://

    • 注意yml文件格式

      image

  • 配置adapter的映射文件时:

    • dataSourceKey、outerAdapterKey、destination、groupId要和上面的application.yml对应;
    • _id和pk:_id是ES自动生成时需要指明pk为一个唯一的主键

image

大表优化

评分表(movie_user_ratings)和评论表(movie_reviews)中存放了所有电影的评分和评论,其中的数据在不断增长,并且很容易增长到一个很夸张的地步,因为一门热门电影就可能有几百万的评分数,如下所示。因此,只用单表存储数据量会非常之大。

image

简单查询

这里先往这两张表中各插入2000w条数据,先观察查询性能下降情况。

简单查询,能用到索引下推:

SELECT
	review_id,
	douban_id,
	…… 
FROM
	movie_recommendation.movie_reviews 
WHERE
	douban_id = 1291543
ORDER BY	
	user_movie_rating_agree DESC,
	user_movie_rating_time DESC
数据量:105w 数据量:2100w
18ms 21ms

可见性能并没有下降的很厉害,我认为原因是:IO次数并没有增加!

对于二级索引,B+树中非叶子节点中占20+6+4=30字节,叶子节点占20+4=24字节,每页可装161024/24=682条记录,可装161024/30=546个页号,3层B+树可装:546546642=1.9e,远大于2000w的数据量,3次IO查到主键,在回表查询完整记录。

对于主键索引,每条记录约210字节,主键+指针=20+6=26字节,每页可装161024/26=630个页号,每页可装161024/210=78条记录,3层B+树可装63063078=3000w,大于目前的2000w数据量,也就是还可以3次IO查到完整数据,和原来100w时的数据量一样。如果最终数据量超过了3000w,那么需要4层B+树来存储,会增加IO次数,性能才会下降。

复杂查询

SELECT
	douban_id,
	sum( CASE WHEN user_movie_rating > 40 THEN 1 ELSE 0 END ) / count( douban_id ) AS positive
FROM
	movie_reviews 
GROUP BY
	douban_id;
数据量:105w 数据量:2100w
16.3s 498s

使用mycat2对评分表(movie_user_ratings)和评论表(movie_reviews)进行分库和分表

分库分表策略:

使用mycat的mod_hash方法,以douban_id作为分库键和分表键。

分成2个库,分别在两个虚拟机的MySQL中(配置了主从复制和读写分离),每个库中10张表。

使用mycat分库分表后,计算好评率:

SELECT
	douban_id,
	sum( CASE WHEN user_movie_rating > 40 THEN 1 ELSE 0 END ) / count( douban_id ) AS positive
FROM
	movie_reviews 
GROUP BY
	douban_id;
数据量:105w 数据量:2100w
1.462s 未测试,磁盘空间不够

推荐算法设计

离线推荐

​ 离线推荐算法计算的是用户的所有历史数据,十分耗时,难以做到实时响应,因此需要离线运行,并且需要按时运行以更新推荐结果集。离线推荐算法反映的是用户整个历史的电影喜好。

基于ALS(交替最小二乘法)的隐语义模型推荐算法

​ 先要找出该用户的所有未评分电影,两两计算此电影与任一未评分电影间的相似度,得到电影间的相似度列表并将其降序排列,选取相似度较高的未评分电影即可作为该电影的相似电影列表。

image

image

实时推荐

实时计算与离线计算在推荐系统上的不同之处在于实时计算推荐结果反映最近一段时间用户近期的偏好,而离线推荐结果反映用户历史总体的偏好

设计实时推荐算法有三点原因:

  1. 用户的喜好并不是一成不变的,可能用户在一段时间内喜欢“爱情”类电影,在另一段时间内认为“爱情”类电影电影枯燥且乏味,转而喜欢“科幻”类电影。如果继续使用离线推荐算法,系统仍会为用户推送许多“爱情”类电影。因此,设计实时推荐算法能让系统只关注用户近期的电影喜好,使推荐结果更加人性化、精准化。
  2. 由于本系统的用户评分表的数据量十分庞大,有100多万条用户评分数据,如果继续使用离线推荐算法,每来一条评分数据就要重新计算并生成新的ALS模型。这无疑会造成巨大的资源浪费和冗长的计算耗时,难以满足用户在秒级或毫米级获取新推荐列表的需求,是得不偿失的。
  3. 用户在短时间内一般只会对极少量电影评分,当用户的评分矩阵没有多大变化时,如果继续使用离线推荐算法,得出的推荐结果也会与之前相差无几。这样就会给用户造成推荐结果一直没有变化的假象,大大降低了用户的使用体验。

image

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

电影推荐系统-后端 展开 收起
Java 等 2 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/calms7/movie-system-server.git
git@gitee.com:calms7/movie-system-server.git
calms7
movie-system-server
MovieSystem-Server
master

搜索帮助