【甘道夫】使用HIVE SQL实现推荐系统数据补全
2016-02-03 11:58
302 查看
需求
在推荐系统场景中,假设基础行为数据太少,或者过于稀疏,通过推荐算法计算得出的推荐结果非常可能达不到要求的数量。
比方,希望针对每一个item或user推荐20个item,可是通过计算仅仅得到8个。剩下的12个就须要补全。
欢迎转载,请注明出处:
http://blog.csdn.net/u010967382/article/details/39674047
策略
数据补全的详细策略是:
补全时机:在挖掘计算结束后,挖掘结果导入HBase(终于web系统从HBase取数据)前。进行数据补全,补全后的数据再导入HBase。(还有另外一个可选时机,在接到请求后再在程序中实现补全,但这种效率肯定没有直接从HBase中读数的高,所以空间换时间是更为合理的策略);
实现技术:补全过程基于HIVE实现;
补全数据:測试过程使用当前浏览item同分类下近一段时间的浏览量TopN;
測试场景:本文仅针对“看了又看”进行数据补全实验。其他推荐需求类似。
实验过程
1.首先在Oracle下调试SQL
调试过程涉及两张表:
(1)TEST_TOPN:
该表中每行代表了一个item在某一天的訪问量。
(2)TEST_X_AND_X:
该表中每行代表了针对每个item的看了又看的item及其訪问量。
我们的目的。就是将该表补全,针对每一个current_item都要有5个看了又看的item。
比方,针对10001号item,须要从it分类下取得top2填补到该表中来。
Oracle中通过下面SQL成功实现该目的:
select * from
(select row_number() over(partition by current_item_category,current_item_id order by source,view_count desc) no,
current_item_id, current_item_category, andx_item_id, source, view_count from
(select current_item_id, current_item_category, andx_item_id, 1 source, view_count
from test_x_and_x
union
select a.current_item_id,a.current_item_category,b.item_id,2,b.view_count
from
(select current_item_id,current_item_category from test_x_and_x
group by current_item_id,current_item_category) a, test_topn b
where a.current_item_category = b.item_category
)) where no<=5
注意:当中的source列用于标识数据来自原始表还是TOPN,全部TOPN的表数据都排在原始表数据之后。
2. 将Oracle中的SQL语句移植到HIVE中
成功移植的HIVE SQL:
select * from
(select rank() over(partition by c.current_item_category,c.current_item_id order by c.source,c.view_count desc) no,
c.current_item_id, c.current_item_category, c.andx_item_id, c.source, c.view_count
from
(select current_item_id,current_item_category,andx_item_id,1 source,view_count
from test_x_and_x
union all
select a.current_item_id current_item_id,a.current_item_category current_item_category,b.item_id andx_item_id,2 source,b.view_count view_count
from
(select current_item_id,current_item_category from test_x_and_x
group by current_item_id,current_item_category) a, test_topn b
where a.current_item_category = b.item_category) c
) d where d.no <= 5;
运行结果和Oracle中全然一致:
移植过程中遇到一些坑。特此记录:
HIVE仅仅支持union all,不支持union。
union all的两张表。不仅要相应字段数据类型同样,字段名(可使用列别名)也必须全然同样;
每个嵌套子查询的结果集都必须使用表别名!
在推荐系统场景中,假设基础行为数据太少,或者过于稀疏,通过推荐算法计算得出的推荐结果非常可能达不到要求的数量。
比方,希望针对每一个item或user推荐20个item,可是通过计算仅仅得到8个。剩下的12个就须要补全。
欢迎转载,请注明出处:
http://blog.csdn.net/u010967382/article/details/39674047
策略
数据补全的详细策略是:
补全时机:在挖掘计算结束后,挖掘结果导入HBase(终于web系统从HBase取数据)前。进行数据补全,补全后的数据再导入HBase。(还有另外一个可选时机,在接到请求后再在程序中实现补全,但这种效率肯定没有直接从HBase中读数的高,所以空间换时间是更为合理的策略);
实现技术:补全过程基于HIVE实现;
补全数据:測试过程使用当前浏览item同分类下近一段时间的浏览量TopN;
測试场景:本文仅针对“看了又看”进行数据补全实验。其他推荐需求类似。
实验过程
1.首先在Oracle下调试SQL
调试过程涉及两张表:
(1)TEST_TOPN:
该表中每行代表了一个item在某一天的訪问量。
(2)TEST_X_AND_X:
该表中每行代表了针对每个item的看了又看的item及其訪问量。
我们的目的。就是将该表补全,针对每一个current_item都要有5个看了又看的item。
比方,针对10001号item,须要从it分类下取得top2填补到该表中来。
Oracle中通过下面SQL成功实现该目的:
select * from
(select row_number() over(partition by current_item_category,current_item_id order by source,view_count desc) no,
current_item_id, current_item_category, andx_item_id, source, view_count from
(select current_item_id, current_item_category, andx_item_id, 1 source, view_count
from test_x_and_x
union
select a.current_item_id,a.current_item_category,b.item_id,2,b.view_count
from
(select current_item_id,current_item_category from test_x_and_x
group by current_item_id,current_item_category) a, test_topn b
where a.current_item_category = b.item_category
)) where no<=5
注意:当中的source列用于标识数据来自原始表还是TOPN,全部TOPN的表数据都排在原始表数据之后。
2. 将Oracle中的SQL语句移植到HIVE中
成功移植的HIVE SQL:
select * from
(select rank() over(partition by c.current_item_category,c.current_item_id order by c.source,c.view_count desc) no,
c.current_item_id, c.current_item_category, c.andx_item_id, c.source, c.view_count
from
(select current_item_id,current_item_category,andx_item_id,1 source,view_count
from test_x_and_x
union all
select a.current_item_id current_item_id,a.current_item_category current_item_category,b.item_id andx_item_id,2 source,b.view_count view_count
from
(select current_item_id,current_item_category from test_x_and_x
group by current_item_id,current_item_category) a, test_topn b
where a.current_item_category = b.item_category) c
) d where d.no <= 5;
运行结果和Oracle中全然一致:
移植过程中遇到一些坑。特此记录:
HIVE仅仅支持union all,不支持union。
union all的两张表。不仅要相应字段数据类型同样,字段名(可使用列别名)也必须全然同样;
每个嵌套子查询的结果集都必须使用表别名!
相关文章推荐
- MYSQL 区分大小写及乱码解决
- [MySQL]快速解决"is marked as crashed and should be repaired"故障
- MySQL中死锁
- redis详解
- Redis3.0集群完全版(数据迁移问题)
- mysql 触发器
- 【MySQL】MySQL无基础学习和入门之一:数据库基础概述和实验环境搭建
- linux系统重启oracle
- MySql主从复制的简单实现
- 如何配置log4j2日志记录至数据库
- java数据库编程——Insert and Retrieve Images from MySql Table Using Java
- 通过yum安装mysql
- MySQL分片水很深
- postgresql 时区与时间函数
- mysql 时区与时间函数
- 高性能Mysql主从架构的复制原理及配置详解
- C# memcache
- Linux下Redis的安装和部署
- CentOs6.5 通过vncserver安装oracle
- Redis 实例: 集群