- Apache Pig 教程
- Apache Pig - 首頁
- Apache Pig 簡介
- Apache Pig - 概述
- Apache Pig - 架構
- Apache Pig 環境
- Apache Pig - 安裝
- Apache Pig - 執行
- Apache Pig - Grunt Shell
- Pig Latin
- Pig Latin - 基礎
- 載入和儲存運算子
- Apache Pig - 讀取資料
- Apache Pig - 儲存資料
- 診斷運算子
- Apache Pig - 診斷運算子
- Apache Pig - Describe 運算子
- Apache Pig - Explain 運算子
- Apache Pig - Illustrate 運算子
- Pig Latin 內建函式
- Apache Pig - Eval 函式
- 載入和儲存函式
- Apache Pig - Bag 和 Tuple 函式
- Apache Pig - 字串函式
- Apache Pig - 日期時間函式
- Apache Pig - 數學函式
- Apache Pig 有用資源
- Apache Pig - 快速指南
- Apache Pig - 有用資源
- Apache Pig - 討論
Apache Pig - 執行指令碼
本章將介紹如何在批處理模式下執行 Apache Pig 指令碼。
Pig 指令碼中的註釋
在檔案中編寫指令碼時,可以包含如下所示的註釋。
多行註釋
我們將以'/*'開頭多行註釋,以'*/'結尾。
/* These are the multi-line comments In the pig script */
單行註釋
我們將以'--'開頭單行註釋。
--we can write single line comments like this.
以批處理模式執行 Pig 指令碼
在批處理模式下執行 Apache Pig 語句時,請按照以下步驟操作。
步驟 1
將所有必需的 Pig Latin 語句寫入單個檔案。我們可以將所有 Pig Latin 語句和命令寫入單個檔案並將其儲存為.pig檔案。
步驟 2
執行 Apache Pig 指令碼。您可以從 shell(Linux)執行 Pig 指令碼,如下所示。
| 本地模式 | MapReduce 模式 |
|---|---|
| $ pig -x local Sample_script.pig | $ pig -x mapreduce Sample_script.pig |
您也可以使用 exec 命令從 Grunt shell 執行它,如下所示。
grunt> exec /sample_script.pig
從 HDFS 執行 Pig 指令碼
我們還可以執行駐留在 HDFS 中的 Pig 指令碼。假設在名為/pig_data/的 HDFS 目錄中有一個名為Sample_script.pig的 Pig 指令碼。我們可以按如下方式執行它。
$ pig -x mapreduce hdfs://:9000/pig_data/Sample_script.pig
示例
假設我們在 HDFS 中有一個名為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關係執行操作和轉換的語句,如下所示。
student = LOAD 'hdfs://: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的檔案中的資料載入為名為student的關係。
指令碼的第二條語句將根據年齡以降序排列關係的元組,並將其儲存為student_order。
指令碼的第三條語句將student_order的前 4 個元組儲存為student_limit。
最後,第四條語句將轉儲關係student_limit的內容。
現在讓我們按如下所示執行sample_script.pig。
$./pig -x mapreduce hdfs://:9000/pig_data/sample_script.pig
Apache Pig 將被執行,並提供以下內容的輸出。
(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)