【问题标题】:sub query in select clause with hive带有 hive 的 select 子句中的子查询
【发布时间】:2015-02-02 15:23:03
【问题描述】:

我无法通过 Hive 中的有效查询找到实现以下功能的方法。意图是根据加权平均获得一年内上映的评分最高的电影。

更清楚地说,这是我应该能够在单个查询中在 hive 中执行的操作。

var allMoviesRated = select count(movieid) where year(from_unixtime(unixtime)) = 1997;

选择movieid、avg(rating)、count(movieid)、avg(rating)/allMoviesRated 作为加权 (select movieid, rating, year(from_unixtime(unixtime)) as year from u_data where u_data_new.year = 1997) u_data_new group by movieid order by weighted desc limit 10;

【问题讨论】:

  • 我已经使用 join 完成了。可能是一项昂贵的操作,但它确实有效。

标签: hiveql


【解决方案1】:

很遗憾.. 我不认为有一种方法可以在单个查询中使用子查询来计算所有评分的电影。

您可以编写一个执行 2 个查询的脚本 首先查询一个获取 allMoviesRated 并将其存储在脚本变量中。 第二个查询是使用 hiveconf 传递此值的排名查询

因此您的脚本可能看起来像

your script.bash or python------------start--------
var allMoviesRated  = os.cmd (hive -S "use db; select count(distinct movieid);")
ranking = os.cmd ( hive -S -hiveconf NUM_MOVIES = allMoviesRated -f ranking_query.hql)  
your script.bash or python------------end--------

ranking_query.hql:

select movieid, avg(rating), count(movieid), avg(rating)/${hiveconf:NUM_MOVIES }as weighted 
from (
    select movieid, rating, year(from_unixtime(unixtime)) as year 
    from u_data where u_data_new.year = 1997) u_data_new 
group by movieid order by weighted desc limit 10;

【讨论】:

  • 感谢 urvish 的努力。
猜你喜欢
  • 1970-01-01
  • 2020-12-27
  • 2017-07-11
  • 2014-02-23
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
  • 2020-06-19
  • 1970-01-01
相关资源
最近更新 更多