【问题标题】:How to implement percentile in hive如何在蜂巢中实现百分位数
【发布时间】:2019-07-07 08:09:07
【问题描述】:

我在 hive 中有这样一张桌子

user_id     no.of game_plays
u1           52
u2           190
u10          166
u9           100
u3           90
u4           44
u5           21
u7           10
u8           5

以上只是一小部分数据。

因此,总游戏次数为 678

我想计算每个组中的用户,如下所示

who contribute to top 33.3% of total game_plays and 
who contribute to between 33.3% and 66.6% of total game_plays 
who contribute to bottom 33.3% of total game_plays

基本上,像上面一样将数据分成 3 个组,并从每个组中获取前 20 名用户。

我知道如何在 BigQuery 中实现的逻辑,例如....获取按 game_plays 排序的百分位值,然后在上面的查询中放置一个 case 语句,并在每个组中使用 game_plays 进行排名并选择排名

它给出了我想要的结果。

我不知道如何在 hive 中实现这种东西。

我已经浏览了以下页面,但没有得到任何想法

How to implement percentile in Hive?

How to calculate median in Hive

并且已经通过下面的功能链接,

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types

我知道我必须对函数进行百分位运算……但要确定我是如何实现的。

下面是我试过的代码,

select a.user_id,a.game_plays, percentile(a.game_plays,0.66) as percentile
from (
select user_id, sum(game_plays) as game_plays
from game_play_table
where data_date = '2019-06-01' 
group by user_id) a

我知道上面的代码并没有给出确切的输出,但是在上面写了一个外部查询之后......我可以得到我想要的输出......但是上面的查询输出本身就非常不同。

谁能帮忙???

【问题讨论】:

    标签: hadoop hive percentile


    【解决方案1】:

    您可以使用 "case" 来计算百分位数

    select user_id,game_plays ,
    case when (game_plays * (100 /678)) > 33.3 then 'top 33.3%'
    when (game_plays * (100 /678)) > 33.3) and (game_plays * (100 /678)) < 66.6) then 'between 33.3% and 66.6%'
    when (game_plays * (100 /678)) < 33.3) then 'less then 33.3%'
    end as percentile 
    from game_play_table
    where data_date = '2019-06-01' 
    group by user_id
    

    【讨论】:

    • 嗨,M.achaibou,我认为以上所有内容都“小于 33.3%”,除非任何单个记录的贡献超过 33.3%,然后它会改变存储桶...... .只是为了更清楚....我想要累积前 33.3% 的游戏播放量的用户....您了解其中的区别吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 2015-06-17
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多