hive python udf

原创
2016/07/21 13:22
阅读数 50

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 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部