Python Pig UDF函數(shù)

更多信息https://blue-shadow.top/
附書代碼Github工程:https://github.com/Shadow-Hunter-X

Pig和Hadoop都是Java編寫的。Java也是編寫UDF的原生語言,但通過Java進(jìn)行編寫需要通過編譯打包成Jar包后進(jìn)行部署,過程是很繁瑣的。但通過Python編寫UDF是很方便快捷的,至少在功能測試階段是這樣很方便

pig_util模塊說明

對于pig_util模塊可以去安裝Pig的目錄下獲取。Hello World例子,大家應(yīng)該都會(huì)感覺很親切,所以先以Hello World程序開始說明。在這個(gè)Hello World例子中只是單純的輸出數(shù)據(jù),并沒有使用Pig傳遞的數(shù)據(jù)。

  • 編寫python腳本,腳本名為hello_world.py。
from pig_util import outputSchema    
@outputSchema('word:chararray')     # 使用outputSchema裝飾器
def hello_world():
return "hello world"
(2)進(jìn)入Grunt中調(diào)用Hello World UDF。
(base) root@test_data# pig -x local
-- 1注冊Python腳本,通過輸出的的信息,判斷成功的注冊Python腳本
grunt> REGISTER /home/hadoop/test_data/hello_world.py using streaming_python as hello_udf ;  
-- 2加載數(shù)據(jù)
grunt> movies_data = load '/home/hadoop/test_data/movies.csv' using PigStorage(',') as (movieId:chararray, title:chararray, genres:chararray) ;
grunt> movies_10 = LIMIT movies_data 10 ;
-- 3調(diào)用Python的udf
grunt> movies_10_hello = FOREACH movies_10 GENERATE hello_udf.hello_world() ;
grunt> dump movies_10_hello ;     -- 輸出10個(gè)hello world,如下所示
2019-09-26 20:10:45,505 [MainThread] INFO  org.apache.pig.backend.hadoop.executionengine.util.MapRedUtil - Total input paths to process : 1
(hello world)
(hello world)
(hello world)

Pig調(diào)用Python UDF

在通過前一小節(jié)對pig_util.py使用實(shí)際并沒有數(shù)據(jù)操作,只是演示了一個(gè)處理流程。這一節(jié)將進(jìn)行演示,使用Python UDF操作Pig數(shù)據(jù)。在使用Python腳本時(shí)需要指定解釋器,支持兩種Jpython和C Ptyhon。

使用Jython:register '/path/to/pigudf.py' using jython as myfuncs;
使用C Python:register '/path/to/pigudf.py' using streaming_python as myfuncs;

對Movies數(shù)據(jù)進(jìn)行操作。由于電影名中包含出品年份,像這樣Toy Story (1995)。現(xiàn)在使用Python將影名和年份拆開,后計(jì)算至今出版了多少年。

from pig_util import outputSchema
from datetime import datetime
import re
@outputSchema('title:chararray')
def parse_title(title):
    ............
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容