为了解决信息过载和用户无明确需求的问题,找到用户感兴趣的物品,才有了个性化推荐系统。
其实,解决信息过载的问题,代表性的解决方案是分类目录和搜索引擎,如hao123,电商首页的分类目录以及百度,360搜索等。
不过分类目录和搜索引擎只能解决用户主动查找信息的需求,即用户知道自己想要什么,并不能解决用户没用明确需求很随便的问题。
经典语录是:你想吃什么,随便!面对这种很随便又得罪不起的用户(女友和上帝),只能通过分析用户的历史行为给用户的兴趣建模,从而主动给用户推荐能够满足他们兴趣和需求的信息。比如问问女友的闺蜜,她一般什么时候喜欢吃什么。
京东的推荐:
推荐系统广泛存在于各类网站中,作为一个应用为用户提供个性化的推荐。它需要一些用户的历史数据,一般由三个部分组成:基础数据、推荐算法系统、前台展示。
迄今为止,在个性化推荐系统中,协同过滤技术是应用最成功的技术。目前国内外有许多大型网站应用这项技术为用户更加智能(个性化、千人千面)的推荐内容。
核心思想:
协同过滤一般是在海量的用户中发掘出一小部分和你品位比较类似的,在协同过滤中,这些用户成为邻居,然后根据他们喜欢的其他东西组织成一个排序的目彔作为推荐给你。
对于用户A,根据用户的历史偏好,这里只计算得到一个邻居–用户C,然后将用户C 喜欢的物品D 推荐给用户A。
基于用户的协同过滤算法先计算的是用户与用户的相似度(兴趣相投,物以类聚人以群分),然后将相似度比较接近的用户A购买的物品推荐给用户B,专业的说法是该算法用最近邻居(nearest-neighbor)算法找出一个用户的邻居集合,该集合的用户和该用户有相似的喜好,算法根据邻居的偏好对该用户进行预测。
ALS 是交替最小二乘 (alternating least squares)的简称。在机器学习的上下文中,ALS 特指使用交替最小二乘求解的一个协同推荐算法。它通过观察到的所有用户给产品的打分,来推断每个用户的喜好并向用户推荐适合的产品。从协同过滤的分类来说,ALS算法属于User-Item CF,也叫做混合CF。它同时考虑了User和Item两个方面。
用户和商品的关系,可以抽象为如下的三元组:
196 242 3 881250949
186 302 3 891717742
22 377 1 878887116
244 51 2 880606923
166 346 1 886397596
298 474 4 884182806
115 265 2 881171488
253 465 5 891628467
305 451 3 886324817
6 86 3 883603013
62 257 2 879372434
286 1014 5 879781125
200 222 5 876042340
210 40 3 891035994
................
对于好友的推荐,需要找出每个用户之间的相似性,具体规则如下:
字段 | 权重分 | |||
---|---|---|---|---|
年龄差 | 0-2岁 30分 | 3-5 20分 | 5-10岁 10分 | 10岁以上 0分 |
性别 | 异性 30分 | 同性 0分 | ||
位置 | 同城 20分 | 不同 0分 | ||
学历 | 相同 20分 | 不同 0分 |
#拉取镜像
docker pull registry.cn-hangzhou.aliyuncs.com/itcast/tanhua-spark-recommend-user:1.0.1
#创建容器
docker create --name tanhua-spark-recommend-user \
--env MONGODB_HOST=192.168.31.81 \
--env MONGODB_PORT=27017 \
--env MONGODB_USERNAME=tanhua \
--env MONGODB_PASSWORD=l3SCjl0HvmSkTtiSbN0Swv40spYnHhDV \
--env MONGODB_DATABASE=tanhua \
--env MONGODB_COLLECTION=recommend_user \
--env JDBC_URL="jdbc:mysql://192.168.31.81:3306/mytanhua?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false" \
--env JDBC_DRIVER=com.mysql.jdbc.Driver \
--env JDBC_USER=root \
--env JDBC_PASSWORD=root \
--env JDBC_TABLE=tb_user_info \
--env SCHEDULE_PERIOD=30 \
registry.cn-hangzhou.aliyuncs.com/itcast/tanhua-spark-recommend-user:1.0.1
#参数说明
#MONGODB_HOST mongodb服务的地址
#MONGODB_PORT mongodb服务的端口
#MONGODB_USERNAME mongodb服务的认证用户名
#MONGODB_PASSWORD mongodb服务的认证密码
#MONGODB_DATABASE mongodb连接的数据库
#MONGODB_COLLECTION 操作表
#JDBC_URL mysql数据库连接地址
#JDBC_DRIVER jdbc驱动
#JDBC_USER 数据库连接用户名
#JDBC_PASSWORD 数据库连接密码
#JDBC_TABLE 数据库表名
#SCHEDULE_PERIOD 下次执行时间间隔,但是为分,默认为10分钟
#启动服务
docker start tanhua-spark-recommend-user
#查看日志
docker logs -f tanhua-spark-recommend-user
执行完成后,可以看到MongoDB中的recommend_user表中数据已经重新生成了。
在圈子功能中,针对于用户发布的动态信息,系统可以根据用户的发布、浏览、点赞等操作,对动态信息做计算,然后对每个用户进行不同的推荐。
流程说明:
核心推荐逻辑:
my-tanhua-server增加依赖:
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-clientartifactId>
dependency>
配置文件:
# RocketMQ相关配置
rocketmq.name-server=192.168.31.81:9876
rocketmq.producer.group=tanhua
package com.tanhua.server.service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.User;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Publish;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class QuanziMQService {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Reference(version = "1.0.0")
private QuanZiApi quanZiApi;
/**
* 发布动态消息
*
* @param publishId
* @return
*/
public Boolean publishMsg(String publishId) {
return this.sendMsg(publishId, 1);
}
/**
* 浏览动态消息
*
* @param publishId
* @return
*/
public Boolean queryPublishMsg(String publishId) {
return this.sendMsg(publishId, 2);
}
/**
* 点赞动态消息
*
* @param publishId
* @return
*/
public Boolean likePublishMsg(String publishId) {
return this.sendMsg(publishId, 3);
}
/**
* 取消点赞动态消息
*
* @param publishId
* @return
*/
public Boolean disLikePublishMsg(String publishId) {
return this.sendMsg(publishId, 6);
}
/**
* 喜欢动态消息
*
* @param publishId
* @return
*/
public Boolean lovePublishMsg(String publishId) {
return this.sendMsg(publishId, 4);
}
/**
* 取消喜欢动态消息
*
* @param publishId
* @return
*/
public Boolean disLovePublishMsg(String publishId) {
return this.sendMsg(publishId, 7);
}
/**
* 评论动态消息
*
* @param publishId
* @return
*/
public Boolean commentPublishMsg(String publishId) {
return this.sendMsg(publishId, 5);
}
/**
* 发送圈子操作相关的消息
*
* @param publishId
* @param type 1-发动态,2-浏览动态, 3-点赞, 4-喜欢, 5-评论,6-取消点赞,7-取消喜欢
* @return
*/
private Boolean sendMsg(String publishId, Integer type) {
try {
User user = UserThreadLocal.get();
Publish publish = this.quanZiApi.queryPublishById(publishId);
//构建消息
Map<String, Object> msg = new HashMap<>();
msg.put("userId", user.getId());
msg.put("date", System.currentTimeMillis());
msg.put("publishId", publishId);
msg.put("pid", publish.getPid());
msg.put("type", type);
this.rocketMQTemplate.convertAndSend("tanhua-quanzi", msg);
} catch (Exception e) {
log.error("发送消息失败! publishId = " + publishId + ", type = " + type, e);
return false;
}
return true;
}
}
在QuanZiService完成发送消息方法调用。
package com.tanhua.server.service;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.service.PicUploadService;
import com.tanhua.common.utils.RelativeDateFormat;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.PicUploadResult;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.api.VisitorsApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.dubbo.server.pojo.Visitors;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.CommentVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.QuanZiVo;
import com.tanhua.server.vo.VisitorsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;
@Service
public class QuanZiService {
@Reference(version = "1.0.0")
private QuanZiApi quanZiApi;
@Reference(version = "1.0.0")
private VisitorsApi visitorsApi;
@Autowired
private UserService userService;
@Autowired
private UserInfoService userInfoService;
@Autowired
private PicUploadService picUploadService;
@Autowired
private QuanziMQService quanziMQService;
public PageResult queryPublishList(Integer page, Integer pageSize) {
//分析:通过dubbo中的服务查询用户的好友动态
//通过mysql查询用户的信息,回写到结果对象中(QuanZiVo)
PageResult pageResult = new PageResult();
pageResult.setPage(page);
pageResult.setPagesize(pageSize);
//直接从ThreadLocal中获取对象
User user = UserThreadLocal.get();
//通过dubbo查询数据
PageInfo<Publish> pageInfo = this.quanZiApi.queryPublishList(user.getId(), page, pageSize);
List<Publish> records = pageInfo.getRecords();
if (CollUtil.isEmpty(records)) {
return pageResult;
}
pageResult.setItems(this.fillQuanZiVo(records));
return pageResult;
}
/**
* 填充用户信息
*
* @param userInfo
* @param quanZiVo
*/
private void fillUserInfoToQuanZiVo(UserInfo userInfo, QuanZiVo quanZiVo) {
BeanUtil.copyProperties(userInfo, quanZiVo, "id");
quanZiVo.setGender(userInfo.getSex().name().toLowerCase());
quanZiVo.setTags(StringUtils.split(userInfo.getTags(), ','));
//当前用户
User user = UserThreadLocal.get();
quanZiVo.setCommentCount(0); //TODO 评论数
quanZiVo.setDistance("1.2公里"); //TODO 距离
quanZiVo.setHasLiked(this.quanZiApi.queryUserIsLike(user.getId(), quanZiVo.getId()) ? 1 : 0); //是否点赞(1是,0否)
quanZiVo.setLikeCount(Convert.toInt(this.quanZiApi.queryLikeCount(quanZiVo.getId()))); //点赞数
quanZiVo.setHasLoved(this.quanZiApi.queryUserIsLove(user.getId(), quanZiVo.getId()) ? 1 : 0); //是否喜欢(1是,0否)
quanZiVo.setLoveCount(Convert.toInt(this.quanZiApi.queryLoveCount(quanZiVo.getId()))); //喜欢数
}
/**
* 根据查询到的publish集合填充QuanZiVo对象
*
* @param records
* @return
*/
private List<QuanZiVo> fillQuanZiVo(List<Publish> records) {
List<QuanZiVo> quanZiVoList = new ArrayList<>();
records.forEach(publish -> {
QuanZiVo quanZiVo = new QuanZiVo();
quanZiVo.setId(publish.getId().toHexString());
quanZiVo.setTextContent(publish.getText());
quanZiVo.setImageContent(publish.getMedias().toArray(new String[]{}));
quanZiVo.setUserId(publish.getUserId());
quanZiVo.setCreateDate(RelativeDateFormat.format(new Date(publish.getCreated())));
quanZiVoList.add(quanZiVo);
});
//查询用户信息
List<Object> userIds = CollUtil.getFieldValues(records, "userId");
List<UserInfo> userInfoList = this.userInfoService.queryUserInfoByUserIdList(userIds);
for (QuanZiVo quanZiVo : quanZiVoList) {
//找到对应的用户信息
for (UserInfo userInfo : userInfoList) {
if (quanZiVo.getUserId().longValue() == userInfo.getUserId().longValue()) {
this.fillUserInfoToQuanZiVo(userInfo, quanZiVo);
break;
}
}
}
return quanZiVoList;
}
/**
* 发布动态
*
* @param textContent
* @param location
* @param latitude
* @param longitude
* @param multipartFile
* @return
*/
public String savePublish(String textContent,
String location,
String latitude,
String longitude,
MultipartFile[] multipartFile) {
//查询当前的登录信息
User user = UserThreadLocal.get();
Publish publish = new Publish();
publish.setUserId(user.getId());
publish.setText(textContent);
publish.setLocationName(location);
publish.setLatitude(latitude);
publish.setLongitude(longitude);
publish.setSeeType(1);
List<String> picUrls = new ArrayList<>();
//图片上传
for (MultipartFile file : multipartFile) {
PicUploadResult picUploadResult = this.picUploadService.upload(file);
picUrls.add(picUploadResult.getName());
}
publish.setMedias(picUrls);
String publishId = this.quanZiApi.savePublish(publish);
if(StrUtil.isNotEmpty(publishId)){
//发送消息
this.quanziMQService.publishMsg(publishId);
}
return publishId;
}
public PageResult queryRecommendPublishList(Integer page, Integer pageSize) {
//分析:通过dubbo中的服务查询系统推荐动态
//通过mysql查询用户的信息,回写到结果对象中(QuanZiVo)
PageResult pageResult = new PageResult();
pageResult.setPage(page);
pageResult.setPagesize(pageSize);
//直接从ThreadLocal中获取对象
User user = UserThreadLocal.get();
//通过dubbo查询数据
PageInfo<Publish> pageInfo = this.quanZiApi.queryRecommendPublishList(user.getId(), page, pageSize);
List<Publish> records = pageInfo.getRecords();
if (CollUtil.isEmpty(records)) {
return pageResult;
}
pageResult.setItems(this.fillQuanZiVo(records));
return pageResult;
}
/**
* 动态点赞
* @param publishId
* @return
*/
public Long likeComment(String publishId) {
User user = UserThreadLocal.get();
Boolean result = this.quanZiApi.likeComment(user.getId(), publishId);
if (result) {
//发消息
this.quanziMQService.likePublishMsg(publishId);
//查询点赞数
return this.quanZiApi.queryLikeCount(publishId);
}
return null;
}
/**
* 动态的取消点赞
*
* @param publishId
* @return
*/
public Long disLikeComment(String publishId) {
User user = UserThreadLocal.get();
Boolean result = this.quanZiApi.disLikeComment(user.getId(), publishId);
if (result) {
//发消息
this.quanziMQService.disLikePublishMsg(publishId);
//查询点赞数
return this.quanZiApi.queryLikeCount(publishId);
}
return null;
}
public Long loveComment(String publishId) {
User user = UserThreadLocal.get();
//喜欢
Boolean result = this.quanZiApi.loveComment(user.getId(), publishId);
if(result){
//发消息
this.quanziMQService.lovePublishMsg(publishId);
//查询喜欢数
return this.quanZiApi.queryLoveCount(publishId);
}
return null;
}
public Long disLoveComment(String publishId) {
User user = UserThreadLocal.get();
//取消喜欢
Boolean result = this.quanZiApi.disLoveComment(user.getId(), publishId);
if(result){
//发消息
this.quanziMQService.disLovePublishMsg(publishId);
//查询喜欢数
return this.quanZiApi.queryLoveCount(publishId);
}
return null;
}
public QuanZiVo queryById(String publishId) {
Publish publish = this.quanZiApi.queryPublishById(publishId);
if (publish == null) {
return null;
}
//发消息
this.quanziMQService.queryPublishMsg(publishId);
return this.fillQuanZiVo(Arrays.asList(publish)).get(0);
}
/**
* 查询评论列表
*
* @param publishId
* @param page
* @param pageSize
* @return
*/
public PageResult queryCommentList(String publishId, Integer page, Integer pageSize) {
PageResult pageResult = new PageResult();
pageResult.setPage(page);
pageResult.setPagesize(pageSize);
User user = UserThreadLocal.get();
//查询评论列表数据
PageInfo<Comment> pageInfo = this.quanZiApi.queryCommentList(publishId, page, pageSize);
List<Comment> records = pageInfo.getRecords();
if(CollUtil.isEmpty(records)){
return pageResult;
}
//查询用户信息
List<Object> userIdList = CollUtil.getFieldValues(records, "userId");
List<UserInfo> userInfoList = this.userInfoService.queryUserInfoByUserIdList(userIdList);
List<CommentVo> result = new ArrayList<>();
for (Comment record : records) {
CommentVo commentVo = new CommentVo();
commentVo.setContent(record.getContent());
commentVo.setId(record.getId().toHexString());
commentVo.setCreateDate(DateUtil.format(new Date(record.getCreated()), "HH:mm"));
//是否点赞
commentVo.setHasLiked(this.quanZiApi.queryUserIsLike(user.getId(), commentVo.getId()) ? 1 : 0);
//点赞数
commentVo.setLikeCount(Convert.toInt(this.quanZiApi.queryLikeCount(commentVo.getId())));
for (UserInfo userInfo : userInfoList) {
if(ObjectUtil.equals(record.getUserId(), userInfo.getUserId())){
commentVo.setAvatar(userInfo.getLogo());
commentVo.setNickname(userInfo.getNickName());
break;
}
}
result.add(commentVo);
}
pageResult.setItems(result);
return pageResult;
}
/**
* 发表评论
* @param publishId
* @param content
* @return
*/
public Boolean saveComments(String publishId, String content) {
User user = UserThreadLocal.get();
Boolean result = this.quanZiApi.saveComment(user.getId(), publishId, content);
if(result){
//发消息
this.quanziMQService.commentPublishMsg(publishId);
}
return result;
}
public PageResult queryAlbumList(Long userId, Integer page, Integer pageSize) {
PageResult pageResult = new PageResult();
pageResult.setPage(page);
pageResult.setPagesize(pageSize);
//查询数据
PageInfo<Publish> pageInfo = this.quanZiApi.queryAlbumList(userId, page, pageSize);
if(CollUtil.isEmpty(pageInfo.getRecords())){
return pageResult;
}
//填充数据
pageResult.setItems(this.fillQuanZiVo(pageInfo.getRecords()));
return pageResult;
}
public List<VisitorsVo> queryVisitorsList() {
User user = UserThreadLocal.get();
List<Visitors> visitorsList = this.visitorsApi.queryMyVisitor(user.getId());
if (CollUtil.isEmpty(visitorsList)) {
return Collections.emptyList();
}
List<Object> userIds = CollUtil.getFieldValues(visitorsList, "visitorUserId");
List<UserInfo> userInfoList = this.userInfoService.queryUserInfoByUserIdList(userIds);
List<VisitorsVo> visitorsVoList = new ArrayList<>();
for (Visitors visitor : visitorsList) {
for (UserInfo userInfo : userInfoList) {
if (ObjectUtil.equals(visitor.getVisitorUserId(), userInfo.getUserId())) {
VisitorsVo visitorsVo = new VisitorsVo();
visitorsVo.setAge(userInfo.getAge());
visitorsVo.setAvatar(userInfo.getLogo());
visitorsVo.setGender(userInfo.getSex().name().toLowerCase());
visitorsVo.setId(userInfo.getUserId());
visitorsVo.setNickname(userInfo.getNickName());
visitorsVo.setTags(StringUtils.split(userInfo.getTags(), ','));
visitorsVo.setFateValue(visitor.getScore().intValue());
visitorsVoList.add(visitorsVo);
break;
}
}
}
return visitorsVoList;
}
}
接收消息的工作需要新创建my-tanhua-recommend工程,在此工程中完成相关的操作。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>my-tanhuaartifactId>
<groupId>cn.itcast.tanhuagroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>my-tanhua-recommendartifactId>
<dependencies>
<dependency>
<groupId>cn.itcast.tanhuagroupId>
<artifactId>my-tanhua-dubbo-interfaceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
dependency>
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-clientartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>joda-timegroupId>
<artifactId>joda-timeartifactId>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
dependency>
dependencies>
project>
application.properties
spring.application.name = itcast-rocketmq
server.port = 18082
# RocketMQ相关配置
rocketmq.name-server=192.168.31.81:9876
rocketmq.producer.group=tanhua
# mongodb相关配置
spring.data.mongodb.username=tanhua
spring.data.mongodb.password=l3SCjl0HvmSkTtiSbN0Swv40spYnHhDV
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=tanhua
spring.data.mongodb.port=27017
spring.data.mongodb.host=192.168.31.81
package com.tanhua.recommend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RecommendApplication {
public static void main(String[] args) {
SpringApplication.run(RecommendApplication.class, args);
}
}
存储到MongoDB的中的实体结构。
package com.tanhua.recommend.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "recommend_quanzi")
public class RecommendQuanZi {
private ObjectId id;
private Long userId;// 用户id
private Long publishId; //动态id,需要转化为Long类型
private Double score; //得分
private Long date; //时间戳
}
package com.tanhua.recommend.msg;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.recommend.pojo.RecommendQuanZi;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
@Component
@RocketMQMessageListener(topic = "tanhua-quanzi",
consumerGroup = "tanhua-quanzi-consumer")
@Slf4j
public class QuanZiMsgConsumer implements RocketMQListener<String> {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public void onMessage(String msg) {
try {
JSONObject jsonObject = JSONUtil.parseObj(msg);
Long userId = jsonObject.getLong("userId");
Long date = jsonObject.getLong("date");
String publishId = jsonObject.getStr("publishId");
Long pid = jsonObject.getLong("pid");
Integer type = jsonObject.getInt("type");
RecommendQuanZi recommendQuanZi = new RecommendQuanZi();
recommendQuanZi.setUserId(userId);
recommendQuanZi.setId(ObjectId.get());
recommendQuanZi.setDate(date);
recommendQuanZi.setPublishId(pid);
//1-发动态,2-浏览动态, 3-点赞, 4-喜欢, 5-评论,6-取消点赞,7-取消喜欢
switch (type) {
case 1: {
Publish publish = this.mongoTemplate.findById(new ObjectId(publishId), Publish.class);
if (ObjectUtil.isNotEmpty(publish)) {
double score = 0d;
//获取图片数
score += CollUtil.size(publish.getMedias());
//获取文本的长度
//文字长度:50以内1分,50~100之间2分,100以上3分
int length = StrUtil.length(publish.getText());
if (length >= 0 && length < 50) {
score += 1;
} else if (length < 100) {
score += 2;
} else {
score += 3;
}
recommendQuanZi.setScore(score);
}
break;
}
case 2: {
recommendQuanZi.setScore(1d);
break;
}
case 3: {
recommendQuanZi.setScore(5d);
break;
}
case 4: {
recommendQuanZi.setScore(8d);
break;
}
case 5: {
recommendQuanZi.setScore(10d);
break;
}
case 6: {
recommendQuanZi.setScore(-5d);
break;
}
case 7: {
recommendQuanZi.setScore(-8d);
break;
}
default: {
recommendQuanZi.setScore(0d);
break;
}
}
//数据保存到MongoDB中
this.mongoTemplate.save(recommendQuanZi);
} catch (Exception e) {
log.error("处理消息出错!msg = " + msg, e);
}
}
}
测试方法:使用APP进行操作,可以看到在MongoDB中已经有数据写入。
在推荐系统中,我们将基于前面写入到推荐表中的数据通过Spark进行计算,在Spark计算完成后将结果写入到Redis中,以供在业务系统中进行查询。
推荐服务我们将基于docker的形式进行部署:
#拉取镜像
docker pull registry.cn-hangzhou.aliyuncs.com/itcast/tanhua-spark-quanzi:1.0
#创建容器
docker create --name tanhua-spark-quanzi \
--env MONGODB_HOST=192.168.31.81 \
--env MONGODB_PORT=27017 \
--env MONGODB_USERNAME=tanhua \
--env MONGODB_PASSWORD=l3SCjl0HvmSkTtiSbN0Swv40spYnHhDV \
--env MONGODB_DATABASE=tanhua \
--env MONGODB_COLLECTION=recommend_quanzi \
--env SCHEDULE_PERIOD=10 \
--env REDIS_NODES="192.168.31.81:6379,192.168.31.81:6380,192.168.31.81:6381" \
registry.cn-hangzhou.aliyuncs.com/itcast/tanhua-spark-quanzi:1.0
#参数说明
#MONGODB_HOST mongodb服务的地址
#MONGODB_PORT mongodb服务的端口
#MONGODB_USERNAME mongodb服务的认证用户名
#MONGODB_PASSWORD mongodb服务的认证密码
#MONGODB_DATABASE mongodb连接的数据库
#MONGODB_COLLECTION 操作表
#SCHEDULE_PERIOD 下次执行时间间隔,但是为分,默认为10分钟
#REDIS_NODES redis集群地址,也可以使用单节点
#mongodb开启认证服务
#docker create --name mongodb --restart=always -p 27017:27017 -v mongodb:/data/db mongo:4.0.3 --auth
#启动服务,启动之后就会进行执行,在SCHEDULE_PERIOD时间后再次执行
docker start tanhua-spark-quanzi
#查看日志
docker logs -f tanhua-spark-quanzi
#执行完成后会将数据写入到redis中
小视频的推荐和动态推荐的实现逻辑非常的类似。
package com.tanhua.server.service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.User;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.VideoApi;
import com.tanhua.dubbo.server.pojo.Video;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class VideoMQService {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Reference(version = "1.0.0")
private VideoApi videoApi;
/**
* 发布小视频消息
*
* @return
*/
public Boolean videoMsg(String videoId) {
return this.sendMsg(videoId, 1);
}
/**
* 点赞小视频
*
* @return
*/
public Boolean likeVideoMsg(String videoId) {
return this.sendMsg(videoId, 2);
}
/**
* 取消点赞小视频
*
* @return
*/
public Boolean disLikeVideoMsg(String videoId) {
return this.sendMsg(videoId, 3);
}
/**
* 评论小视频
*
* @return
*/
public Boolean commentVideoMsg(String videoId) {
return this.sendMsg(videoId, 4);
}
/**
* 发送小视频操作相关的消息
*
* @param videoId
* @param type 1-发动态,2-点赞, 3-取消点赞,4-评论
* @return
*/
private Boolean sendMsg(String videoId, Integer type) {
try {
User user = UserThreadLocal.get();
Video video = this.videoApi.queryVideoById(videoId);
//构建消息
Map<String, Object> msg = new HashMap<>();
msg.put("userId", user.getId());
msg.put("date", System.currentTimeMillis());
msg.put("videoId", videoId);
msg.put("vid", video.getVid());
msg.put("type", type);
this.rocketMQTemplate.convertAndSend("tanhua-video", msg);
} catch (Exception e) {
log.error("发送消息失败! videoId = " + videoId + ", type = " + type, e);
return false;
}
return true;
}
}
package com.tanhua.server.service;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.service.PicUploadService;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.PicUploadResult;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.api.VideoApi;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.VideoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class VideoService {
@Reference(version = "1.0.0")
private VideoApi videoApi;
@Autowired
private PicUploadService picUploadService;
@Autowired
protected FastFileStorageClient storageClient;
@Autowired
private FdfsWebServer fdfsWebServer;
@Autowired
private UserInfoService userInfoService;
@Reference(version = "1.0.0")
private QuanZiApi quanZiApi;
@Autowired
private QuanZiService quanZiService;
@Autowired
private VideoMQService videoMQService;
/**
* 小视频上传
*
* @param picFile 封面图片
* @param videoFile 视频文件
* @return
*/
public Boolean saveVideo(MultipartFile picFile, MultipartFile videoFile) {
User user = UserThreadLocal.get();
Video video = new Video();
video.setUserId(user.getId());
video.setSeeType(1);
try {
//上传图片到阿里云oss
PicUploadResult uploadResult = this.picUploadService.upload(picFile);
video.setPicUrl(uploadResult.getName());
//上传视频到FastDFS中
StorePath storePath = this.storageClient.uploadFile(videoFile.getInputStream(),
videoFile.getSize(),
StrUtil.subAfter(videoFile.getOriginalFilename(), '.', true),
null);
video.setVideoUrl(fdfsWebServer.getWebServerUrl() + storePath.getFullPath());
String videoId = this.videoApi.saveVideo(video);
if(StrUtil.isNotEmpty(videoId)){
//发送消息
this.videoMQService.videoMsg(videoId);
}
return StrUtil.isNotEmpty(videoId);
} catch (IOException e) {
log.error("上传小视频出错~ userId = " + user.getId() + ", file = " + videoFile.getOriginalFilename(), e);
}
return null;
}
public PageResult queryVideoList(Integer page, Integer pageSize) {
User user = UserThreadLocal.get();
PageResult pageResult = new PageResult();
pageResult.setPage(page);
pageResult.setPagesize(pageSize);
PageInfo<Video> pageInfo = this.videoApi.queryVideoList(user.getId(), page, pageSize);
List<Video> records = pageInfo.getRecords();
if (CollUtil.isEmpty(records)) {
return pageResult;
}
//查询用户信息
List<Object> userIds = CollUtil.getFieldValues(records, "userId");
List<UserInfo> userInfoList = this.userInfoService.queryUserInfoByUserIdList(userIds);
List<VideoVo> videoVoList = new ArrayList<>();
for (Video record : records) {
VideoVo videoVo = new VideoVo();
videoVo.setUserId(record.getUserId());
videoVo.setCover(record.getPicUrl());
videoVo.setVideoUrl(record.getVideoUrl());
videoVo.setId(record.getId().toHexString());
videoVo.setSignature("我就是我~"); //TODO 签名
videoVo.setCommentCount(Convert.toInt(this.quanZiApi.queryCommentCount(videoVo.getId()))); //评论数
videoVo.setHasFocus(this.videoApi.isFollowUser(user.getId(), videoVo.getUserId()) ? 1 : 0); //是否关注
videoVo.setHasLiked(this.quanZiApi.queryUserIsLike(user.getId(), videoVo.getId()) ? 1 : 0); //是否点赞(1是,0否)
videoVo.setLikeCount(Convert.toInt(this.quanZiApi.queryLikeCount(videoVo.getId())));//点赞数
//填充用户信息
for (UserInfo userInfo : userInfoList) {
if (ObjectUtil.equals(videoVo.getUserId(), userInfo.getUserId())) {
videoVo.setNickname(userInfo.getNickName());
videoVo.setAvatar(userInfo.getLogo());
break;
}
}
videoVoList.add(videoVo);
}
pageResult.setItems(videoVoList);
return pageResult;
}
/**
* 点赞
*
* @param videoId
* @return
*/
public Long likeComment(String videoId) {
User user = UserThreadLocal.get();
Boolean result = this.quanZiApi.likeComment(user.getId(), videoId);
if (result) {
//发送消息
this.videoMQService.likeVideoMsg(videoId);
return this.quanZiApi.queryLikeCount(videoId);
}
return null;
}
/**
* 取消点赞
*
* @param videoId
* @return
*/
public Long disLikeComment(String videoId) {
User user = UserThreadLocal.get();
Boolean result = this.quanZiApi.disLikeComment(user.getId(), videoId);
if (result) {
//发送消息
this.videoMQService.disLikeVideoMsg(videoId);
return this.quanZiApi.queryLikeCount(videoId);
}
return null;
}
public Boolean saveComment(String videoId, String content) {
Boolean result = this.quanZiService.saveComments(videoId, content);
if(result){
//发送消息
this.videoMQService.commentVideoMsg(videoId);
}
return result;
}
public PageResult queryCommentList(String videoId, Integer page, Integer pageSize) {
return this.quanZiService.queryCommentList(videoId, page, pageSize);
}
/**
* 关注用户
*
* @param userId
* @return
*/
public Boolean followUser(Long userId) {
User user = UserThreadLocal.get();
return this.videoApi.followUser(user.getId(), userId);
}
/**
* 取消关注
*
* @param userId
* @return
*/
public Boolean disFollowUser(Long userId) {
User user = UserThreadLocal.get();
return this.videoApi.disFollowUser(user.getId(), userId);
}
}
package com.tanhua.recommend.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "recommend_video")
public class RecommendVideo {
private ObjectId id;
private Long userId;// 用户id
private Long videoId; //视频id,需要转化为Long类型
private Double score; //得分
private Long date; //时间戳
}
package com.tanhua.recommend.msg;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.tanhua.recommend.pojo.RecommendVideo;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
@Component
@RocketMQMessageListener(topic = "tanhua-video",
consumerGroup = "tanhua-video-consumer")
@Slf4j
public class VideoMsgConsumer implements RocketMQListener<String> {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public void onMessage(String msg) {
try {
JSONObject jsonObject = JSONUtil.parseObj(msg);
Long userId = jsonObject.getLong("userId");
Long vid = jsonObject.getLong("vid");
Integer type = jsonObject.getInt("type");
//1-发动态,2-点赞, 3-取消点赞,4-评论
RecommendVideo recommendVideo = new RecommendVideo();
recommendVideo.setUserId(userId);
recommendVideo.setId(ObjectId.get());
recommendVideo.setDate(System.currentTimeMillis());
recommendVideo.setVideoId(vid);
switch (type) {
case 1: {
recommendVideo.setScore(2d);
break;
}
case 2: {
recommendVideo.setScore(5d);
break;
}
case 3: {
recommendVideo.setScore(-5d);
break;
}
case 4: {
recommendVideo.setScore(10d);
break;
}
default: {
recommendVideo.setScore(0d);
break;
}
}
this.mongoTemplate.save(recommendVideo);
} catch (Exception e) {
log.error("处理小视频消息失败~" + msg, e);
}
}
}
可以看到,用户1对于视频有点赞、取消点赞、评论等操作。
#拉取镜像
docker pull registry.cn-hangzhou.aliyuncs.com/itcast/tanhua-spark-video:1.0
#创建容器
docker create --name tanhua-spark-video \
--env MONGODB_HOST=192.168.31.81 \
--env MONGODB_PORT=27017 \
--env MONGODB_USERNAME=tanhua \
--env MONGODB_PASSWORD=l3SCjl0HvmSkTtiSbN0Swv40spYnHhDV \
--env MONGODB_DATABASE=tanhua \
--env MONGODB_COLLECTION=recommend_video \
--env SCHEDULE_PERIOD=10 \
--env REDIS_NODES="192.168.31.81:6379,192.168.31.81:6380,192.168.31.81:6381" \
registry.cn-hangzhou.aliyuncs.com/itcast/tanhua-spark-video:1.0
#启动服务
docker start tanhua-spark-video
#查看日志
docker logs -f tanhua-spark-video
测试:
break;
}
case 2: {
recommendVideo.setScore(5d);
break;
}
case 3: {
recommendVideo.setScore(-5d);
break;
}
case 4: {
recommendVideo.setScore(10d);
break;
}
default: {
recommendVideo.setScore(0d);
break;
}
}
this.mongoTemplate.save(recommendVideo);
} catch (Exception e) {
log.error("处理小视频消息失败~" + msg, e);
}
}
}
#### 5.3.3、测试
[外链图片转存中...(img-aAeNU8FJ-1706663657395)]
可以看到,用户1对于视频有点赞、取消点赞、评论等操作。
### 5.4、部署推荐服务
~~~shell
#拉取镜像
docker pull registry.cn-hangzhou.aliyuncs.com/itcast/tanhua-spark-video:1.0
#创建容器
docker create --name tanhua-spark-video \
--env MONGODB_HOST=192.168.31.81 \
--env MONGODB_PORT=27017 \
--env MONGODB_USERNAME=tanhua \
--env MONGODB_PASSWORD=l3SCjl0HvmSkTtiSbN0Swv40spYnHhDV \
--env MONGODB_DATABASE=tanhua \
--env MONGODB_COLLECTION=recommend_video \
--env SCHEDULE_PERIOD=10 \
--env REDIS_NODES="192.168.31.81:6379,192.168.31.81:6380,192.168.31.81:6381" \
registry.cn-hangzhou.aliyuncs.com/itcast/tanhua-spark-video:1.0
#启动服务
docker start tanhua-spark-video
#查看日志
docker logs -f tanhua-spark-video
测试:
[外链图片转存中…(img-lqEfj8d6-1706663657395)]