hive数据去重测试

需求对id进行去重

create table test100
(
    id    int,
    month    string,
    label      int
)
    row format delimited
        fields terminated by ','
    stored as textfile;

数据

133,201901,1
134,201812,1
133,201809,1
134,201803,1
133,201801,1
134,201801,1

加载数据

load data local inpath '/root/test100.ttx' into table test100;

distinct

distinct 只能对单个字段去重,完成不了这个需求

select distinct(id),month,label from test100;
133     201801  1
133     201809  1
133     201901  1
134     201801  1
134     201803  1
134     201812  1

group by

行不通

hive> SELECT id,month,label FROM test100 GROUP BY id;
FAILED: SemanticException [Error 10025]: Line 1:10 Expression not in GROUP BY key 'month'

row_number

with a as
(select id,month,label,row_number() over(distribute by id sort by month) `rn` from test100)
select id,month,label from a
where a.rn=1
;
133     201801  1
134     201801  1

总结

  • distinct去重和 group by去重有很多局限,优选row_number函数进行去重

参考

Hive去重最佳方法_SunnyRivers-CSDN博客_hive 去重

hive数据表去重方法_zcc_0015的专栏-CSDN博客_hive表数据去重

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值