• 【甘道夫】使用HIVE SQL实现推荐系统数据补全


    需求
    在推荐系统场景中,假设基础行为数据太少,或者过于稀疏,通过推荐算法计算得出的推荐结果非常可能达不到要求的数量。
    比方,希望针对每一个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中全然一致:


    移植过程中遇到一些坑。特此记录:
    1. HIVE仅仅支持union all,不支持union。
    2. union all的两张表。不仅要相应字段数据类型同样,字段名(可使用列别名)也必须全然同样;
    3. 每个嵌套子查询的结果集都必须使用表别名!
  • 相关阅读:
    Linux下使用mtr做路由图进行网络分析
    PHP使用AJAX返回登录成功信息完整参考代码
    ajax提交表单数据不跳转
    帝国cms常用变量总结
    2020软件工程作业04
    2020软件工程作业03
    疑问
    2020软件工程02
    疑问
    2020软件工程作业01
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5179383.html
Copyright © 2020-2023  润新知