Hive 常用案例分析

1. 数据分箱占比统计

select a.bin
,a.bin_cnt
,concat(round(cast(a.bin_cnt*1.0/b.total_num as decimal(19,4))*100,2),'%') as bin_rate
from (
    select case
        when a1.exchange_rate<0.2 then '[0.0-0.2)'
        when a1.exchange_rate<0.5 then '[0.2-0.5)'
        when a1.exchange_rate<0.8 then '[0.5-0.8)'
        else '[0.8-1]'
    end as bin            --换血率分箱
    ,count(1) as bin_cnt  --分箱数量
    ,'11' as type                      --用于匹配总数的一个临时标志
    from (
        select uid
        ,exchange_rate
        from XX  
    ) a1
    group by case
        when a1.exchange_rate<0.2 then '[0.0-0.2)'
        when a1.exchange_rate<0.5 then '[0.2-0.5)'
        when a1.exchange_rate<0.8 then '[0.5-0.8)'
        else '[0.8-1.0]'
    end
) a
left join (    --通过与原表left join 获得总样本数
    select count(1) as total_num
    ,'11' as type
    from XX
) b
on a.type=b.type
;

2. Hive实现样本随机采样

order by rand() 函数可以对样本进行随机排序,进而实现hive表数据随机取样。

2.1  rand()

产生一个介于0和1之间的随机数

select rand();
-- 0.5523740163966064

-- 指定随机种子,以后使用这个种子每次产生的随机数都是一样的。
select rand(1234);
-- 0.6465821602909256

2.2 order by rand()

对结果进行随机排序

-- 数据随机排序后取前100条
select uid from tablename order 
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值