Hive表数据去重

一、数据全部重复

例如:

namescore
Computer1600
Phone12
Phone12

操作步骤:

1.复制表结构
CREATE TABLE <new_table> LIKE <old_table>;

2.插入去重后的数据
insert overwrite table
<new_table> select distinct * from <old_table> ;

ps:有时执行这个语句会报以下错误:
FAILED: SemanticException TOK_ALLCOLREF is not supported in current context

写上所有列名就好了:
insert overwrite table
<new_table> select distinct name, score from <old_table> ;

二、部分数据重复

例如:

namescoretype
Computer16002
Phone121
Phone151

操作步骤:

1.复制表结构
CREATE TABLE <new_table> LIKE <old_table>;

2.插入去重后的数据
insert overwrite table <new_table>(
select t.name, t.score, t.type
from (
select
name, score, ,type, row_number() over(distribute by name sort by score ) as rn
from <old_table>
) t where t.rn=1
);

3.总结一下就是:

insert overwrite table <new_table> (
select <字段>
from (
select <字段>, row_number() over(distribute by <有重复的字段> sort by <重复字段的排列根据字段>) as rn
from <old_table>
) t where t.rn=1
);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值