案例1:
==效果等同于一行转多行
数据表名称:zhangb.gid_tags
数据格式,每行是2个字段,(gid,tags) ,可能有脏数据,分隔符为“\t”,
ANDROID-9de77225cadb4319adfc1b1fe51c54f0 h65010000,014200,018100,011300,0200
ANDROID-9de77dfdbbab42679eed11f4e48c0ffc 022000,026400,022400,016400,018100
要统计tags中每个tag出现的次数:使用hive python方法:
第一步:写python处理脚本:脚本名字:hivepython.py
import sys
for line in sys.stdin:
appArray=line.strip( ).split("\t")
if len(appArray)==2:
gid=appArray[0]
tags=appArray[1].split(",")
for tag in tags:
print tag
脚本输出是1个字段 tag
第二步,将python脚本加进hive 中:
add file /home/zhangb/hivepython.py
第三步:运行hive语句:
select b.* from (
select a.counter_attr as tag,count(1) as tag_count from (
select TRANSFORM(gid,usertags) USING ‘python hivepython.py‘ as counter_attr from zhangb.gid_tags) a
group by a.counter_attr) b
sort by b.tag;
案例2:
案例1是python处理后输出了1个字段,下面是输出多个字段的形式:
【参考:http://www.cnblogs.com/Joe-T/p/4643452.html】
Hive中有一表,列分隔符为冒号(:),有一列utime是Timestamp格式,需要转成Weekday存到新表。
利用Python写一个Pipeline的Transform,weekday.py的代码也很简单:
import sys
import datetime
for line in sys.stdin:
line=line.strip()
uid,mid,rating,utime=line.split(‘:‘)
weekday=datetime.datetime.fromtimestamp(float(utime)).isoweekday()
print ‘\t‘.join([uid,mid,rating,str(weekday)])
HQL的查询也很简单:
select
transform(uid,mid,rating,utime)
using ‘python weekday.py‘ as (uid,mid,rating,weekday)
from rating