hivepython 同时读入python 且python脚本中处理外部文件txt

 
找出表test_gid2中每个gid的安装列表中含有文件pkgs中的pkg名字的数据行。
pkgs文件要与python脚本放在一个路径下。
用 transform 的传入数据的时候,不管原文件分隔符是什么样的,在python里面处理都是用“ ”来处理。
test_gid2表包含的字段为:gid,phone_model,usertags,installed_applist
        
tes4.py脚本内容:
 
import codecs
import os
import re
import sys
 
lt1=[ ]
f1=codecs.open('pkgs.txt','r','utf-8')
for i in f1.readlines():
    line=i.strip()
    lt1.append(line)
f1.close()
 
for lines in sys.stdin:
    arr=lines.strip().split(' ')
    pkgs=re.split(";|,",arr[-1])
    for j in lt1:
         if j in pkgs:
             print ' '.join(arr)
             break    #避免重复读入数据,一旦有了匹配上的pkg就退出这次循环,去匹配下一行数据
 
注意:add 两个文件到hive中,一个是 py脚本,test4.py  一个是文本文件 pkgs.txt
sql脚本:
 
select TRANSFORM(gid,phone_model,usertags,installed_applist)
USING 'python test4.py'
as (gid,phone_model,usertags,installed_applist)
from test_gid2 ;
 
 
=================
pkgs.txt  文件内容格式,一行一个pkgname:

com.tencent.mm

com.tencent.mobileqq

cn.testin.allintest 

原文地址:https://www.cnblogs.com/zhangbojiangfeng/p/6135704.html