hive python udf

udfdata.txt:
aa pingpong,swim,runningpython

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_setsql

中文或者str時報錯:'ascii' codec can't encode characters in position 61-62
reload(sys)
sys.setdefaultencoding('utf8')code

相關文章
相關標籤/搜索