python hive udf开发

编写python的代码

python文件名:getOperators.py,代码如下:


#!/usr/bin/env python
# -*- coding:UTF-8 -*-
import sys
import re
'''
python 获取手机运营商
'''

if __name__ == '__main__':

    isChinaMobile = "^134[0-8]\\d{7}$|^(?:13[5-9]|147|15[0-27-9]|178|1703|1705|1706|18[2-478])\\d{7,8}$"; # 移动
    isChinaUniom = "^(?:13[0-2]|145|15[56]|176|1704|1707|1708|1709|171|18[56])\\d{7,8}|$"; # 联通
    isChinaTelcom = "^(?:133|153|1700|1701|1702|177|173|18[019])\\d{7,8}$"; # 电信


    regexMobile = re.compile(isChinaMobile, flags=0)
    regexUniom = re.compile(isChinaUniom, flags=0)
    regexTelcom = re.compile(isChinaTelcom, flags=0)

    for line in sys.stdin:
        tel = line.strip()
        if regexMobile.match(tel):
            print "中国移动"
        elif regexUniom.match(tel):
            print "中国联通"
        elif regexTelcom.match(tel):
            print "中国电信"
        else:
            print "未知"

把python文件上传到hdfs中

格式 hdfs dfs -put 本地路径 hdfs路径

hdfs dfs -put /data/scripts/python_udf/getOperators.py /user/webuser/hive_udf/python

把python文件添加到hive系统中

在hive命令行运行下面命令。 注意:m102 是namenode的主机名,这儿需改成你自己的。

add file hdfs://m102/user/webuser/hive_udf/python/getOperators.py;

HQL语句中查询使用

使用格式: transform(字段名) using "python文件名"

select  transform(telephone)  using  "getOperators.py"  as  (yys)
from  m_uc_account

注意

一个session断开后,下一次新的session需要重新把python文件添加hive系统中。

猜你喜欢

转载自my.oschina.net/u/1396185/blog/1613310