Hive学习小记-(15)transform函数

可以参考的一些帖子:

https://www.imooc.com/article/50825

https://blog.csdn.net/lidongmeng0213/article/details/110878902

https://www.cnblogs.com/songweideboke/p/9814604.html

https://www.cnblogs.com/silva/p/4498032.html

一、写了一个简单函数xftp传到/opt/module/hive/transform/test_transform.py

#!/bin/python
import sys
for line in sys.stdin:
    str=line.strip()+'tran'
    print(str)

二、使用hive中test_youhua.test_avg_medium_freq做样例数据

hive> select * from test_avg_medium_freq;
OK
桑普森    400000
迈克    30000
怀特    20000
阿诺德    20000
史密斯    20000
劳伦斯    15000
哈德逊    15000
肯特    10000
贝克    10000
斯科特    10000

三、add path 及select transform操作

写了select transform(name),transform只做了字符拼接,不知道第二列的NULL是哪来的

-- 这里要先add path,否则会报错FAILED: Execution Error, return code 20003 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask. An error occurred when trying to close the Operator running your custom script.
hive> add file /opt/module/hive/transform/test_transform.py;
Added resources: [/opt/module/hive/transform/test_transform.py]
hive> select transform(name) USING 'python test_transform.py' from test_avg_medium_freq;
Total MapReduce CPU Time Spent: 990 msec
OK
桑普森tran    NULL
迈克tran    NULL
怀特tran    NULL
阿诺德tran    NULL
史密斯tran    NULL
劳伦斯tran    NULL
哈德逊tran    NULL
肯特tran    NULL
贝克tran    NULL
斯科特tran    NULL
Time taken: 11.036 seconds, Fetched: 10 row(s)

 四、加AS指定列名

加了AS指定列名之后好了,NULL没有再出现

select transform(name) USING 'python test_transform.py' as trans_name from test_avg_medium_freq;

posted @ 2021-01-24 15:12  foolangirl  阅读(645)  评论(0编辑  收藏  举报