【Hadoop/Hive/mapreduce】系列之使用union all 命令之后如何对hive表格使用python进行去重...

业务场景大概是这样的,这里由两个hive表格,tableA 和 tableB, 格式内容都是这样的:
uid cate1 cate2

在hive QL中,我们知道union有着自动去重的功能,但是那是真对几行内容完全一致的情况下才可以。现在我们要进行去重的情况是根据uid进行去重。
也就是说可能存在这种情况:
1234 老师 唱歌
1234 老师 跳舞
对于hive表格中的这两行数据我们只想要保留其中的一行。

针对这种情况,我们做的大致思路就是,取两个表格数据的时候同时人为加上一个flag,然后使用python代码根据flag进行区分保留。
为了进行去重,我们写了两个代码,一个是取得hive数据的shell脚本,一个是处理hive数据的python脚本

vim get_data.sh
function merge(){
cat <<EOF
add file ./process.py;
    select transform(a.*) using 'python tt.py' as uid,cate1,cate2 from

    (select * from
    (select uid,cate1,cate2,"0" as flag from tableA where dt='sth1'
    union all
    select uid,cate1,cate2,"1" as flag from tableB where dt='sth2'
    )ts
    distribute by uid sort by uid,flag asc
    )a
EOF
}

对于上面这个代码,我觉得有一点需要特别注意,就是

distribute by uid sort by uid,flag asc

为了了解这行代码,我特意去看了看这里的解释参考
简单来说就是说,distribute by uid代表的就是所有uid相同的数据会被送到同一个reducer中去处理。

vim process.py

#!/bin/env python
#-*- encoding:utf-8 -*-
import os
import sys

def set_values(value):
        if value.isdigit():
                return int(value)
        else :
                return 0

lastuid=""
cate1=""
cate2=""
flag=""

for line in sys.stdin :
        line=line.replace("\n","").replace(" ","")
        v=line.split("\t")
        try :
                uid=v[0]
                if not uid.isdigit() or len(v) != 4:
                        pass
                if lastuid!="" and lastuid!=uid:
                        print (lastuid+"\t"+str(cate1)+"\t"+str(cate2))
                        lastuid=""
                        cate1=""
                        cate2=""
                        flag=""
                cate1=v[1]
                cate2=v[2]
                flag=v[3]
                lastuid=uid
        except :
                pass

print (lastuid+"\t"+str(cate1)+"\t"+str(cate2)) #这行代码是为了输出最后一行,这行代码很类似于python word count中的示例代码

转载于:https://www.cnblogs.com/lzida9223/p/10511663.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值