hive python udf

udfdata.txt:
aa pingpong,swim,running

create table udfdata(name String,hobby String) 
row format delimited fields terminated by '\t'   ;

LOAD DATA LOCAL INPATH '/Users/lifei/hivedata/udfdata.txt' 
OVERWRITE INTO TABLE udfdata;

add file /Users/lifei/hivedata/parsehobby.py;
select transform(name,hobby) using 'python parsehobby.py' as (name,hobby) from udfdata

aa	pingpong
aa	swim
aa	running


add file /Users/lifei/hivedata/splithobby.py;
select transform(hobby) using 'python splithobby.py' as (name,hobby,test) from udfdata;

pingpong	swim	running
#parsehobby.py
import sys
for line in sys.stdin:
    detail = line.strip().split('\t')
    if (len(detail) < 2):
        continue
    name = detail[0]
    hobbis = detail[1]
    hobbyArray = hobbis.split(',')
    for hobby in hobbyArray:
        print "%s\t%s" %(name,hobby)

#splithobby.py  
import sys
for line in sys.stdin:
    detail = line.strip().split(',')
    print "%s\t%s\t%s" %(detail[0],detail[1],detail[2])

ps: explode和collect_set

中文或者str时报错:'ascii' codec can't encode characters in position 61-62
reload(sys)
sys.setdefaultencoding('utf8')

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值