新聞中心
在本章中,我們將了解如何以批處理模式運行Apache Pig腳本。

創(chuàng)新互聯(lián)長期為近1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為自流井企業(yè)提供專業(yè)的網(wǎng)站設(shè)計制作、網(wǎng)站建設(shè),自流井網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
Pig腳本中的注釋
在將腳本寫入文件時,我們可以在其中包含注釋,如下所示。
多行注釋
我們將用'/*'開始多行注釋,以'*/'結(jié)束。
/* These are the multi-line comments In the pig script */
單行注釋
我們將用“--"開始單行注釋。
--we can write single line comments like this.
在批處理模式下執(zhí)行Pig腳本
在以批處理方式執(zhí)行Apache Pig語句時,請按照以下步驟操作。
步驟1
將所有需要的Pig Latin語句寫在單個文件中。我們可以將所有Pig Latin語句和命令寫入單個文件,并將其另存為 .pig 文件。
步驟2
執(zhí)行Apache Pig腳本。你可以從shell(Linux)執(zhí)行Pig腳本,如下所示。
| Local模式 | MapReduce模式 |
|---|---|
|
$ pig -x local Sample_script.pig |
$ pig -x mapreduce Sample_script.pig |
你可以使用exec命令從Grunt shell執(zhí)行它,如下所示。
grunt> exec /sample_script.pig
從HDFS執(zhí)行Pig腳本
我們還可以執(zhí)行駐留在HDFS中的Pig腳本。假設(shè)在名為 /pig_data/ 的HDFS目錄中有名為 Sample_script.pig 的Pig腳本。我們可以執(zhí)行它如下所示。
$ pig -x mapreduce hdfs://localhost:9000/pig_data/Sample_script.pig
例
假設(shè)在HDFS中有一個具有以下內(nèi)容的文件 student_details.txt 。
student_details.txt
001,Rajiv,Reddy,21,9848022337,Hyderabad 002,siddarth,Battacharya,22,9848022338,Kolkata 003,Rajesh,Khanna,22,9848022339,Delhi 004,Preethi,Agarwal,21,9848022330,Pune 005,Trupthi,Mohanthy,23,9848022336,Bhuwaneshwar 006,Archana,Mishra,23,9848022335,Chennai 007,Komal,Nayak,24,9848022334,trivendram 008,Bharathi,Nambiayar,24,9848022333,Chennai
我們還在同一個HDFS目錄中有一個名為 sample_script.pig 的示例腳本。此文件包含對student關(guān)系執(zhí)行操作和轉(zhuǎn)換的語句,如下所示。
student = LOAD 'hdfs://localhost:9000/pig_data/student_details.txt' USING PigStorage(',')
as (id:int, firstname:chararray, lastname:chararray, phone:chararray, city:chararray);
student_order = ORDER student BY age DESC;
student_limit = LIMIT student_order 4;
Dump student_limit;
-
腳本的第一個語句會將名為 student_details.txt 的文件中的數(shù)據(jù)加載為名為student的關(guān)系。
-
腳本的第二個語句將根據(jù)年齡以降序排列關(guān)系的元組,并將其存儲為 student_order 。
-
腳本的第三個語句會將 student_order 的前4個元組存儲為 student_limit 。
-
最后,第四個語句將轉(zhuǎn)儲關(guān)系 student_limit 的內(nèi)容。
現(xiàn)在,執(zhí)行 sample_script.pig ,如下所示。
$./pig -x mapreduce hdfs://localhost:9000/pig_data/sample_script.pig
Apache Pig被執(zhí)行,并提供具有以下內(nèi)容的輸出。
(7,Komal,Nayak,24,9848022334,trivendram) (8,Bharathi,Nambiayar,24,9848022333,Chennai) (5,Trupthi,Mohanthy,23,9848022336,Bhuwaneshwar) (6,Archana,Mishra,23,9848022335,Chennai) 2015-10-19 10:31:27,446 [main] INFO org.apache.pig.Main - Pig script completed in 12 minutes, 32 seconds and 751 milliseconds (752751 ms)
本文名稱:創(chuàng)新互聯(lián)ApachePig教程:ApachePig運行腳本
分享地址:http://fisionsoft.com.cn/article/cdcsdci.html


咨詢
建站咨詢
