Apache Pig 運行腳本

2018-01-04 16:12 更新

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

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)


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號