標籤:bsp span 欄位 tin close read div str split
找出表test_gid2中每個gid的安裝列表中含有檔案pkgs中的pkg名字的資料行。pkgs檔案要與python指令碼放在一個路徑下。用 transform 的傳入資料的時候,不管原檔案分隔字元是什麼樣的,在python裡面處理都是用“\t”來處理。test_gid2表包含的欄位為:gid,phone_model,usertags,installed_applist tes4.py指令碼內容: import codecsimport osimport reimport 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(‘\t‘) pkgs=re.split(";|,",arr[-1]) for j in lt1: if j in pkgs: print ‘\t‘.join(arr) break #避免重複讀入資料,一旦有了匹配上的pkg就退出這次迴圈,去匹配下一行資料 注意:add 兩個檔案到hive中,一個是 py指令碼,test4.py 一個是文字檔 pkgs.txtsql指令碼: 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
hivepython 同時讀入python 且python指令碼中處理外部檔案txt