- 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 運算子
- 分組 & 連線
- Apache Pig - Group 運算子
- Apache Pig - Cogroup 運算子
- Apache Pig - Join 運算子
- Apache Pig - Cross 運算子
- 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 - Grunt Shell
呼叫 Grunt shell 後,您可以在 shell 中執行 Pig 指令碼。除此之外,Grunt shell 還提供了一些有用的 shell 和實用程式命令。本章解釋了 Grunt shell 提供的 shell 和實用程式命令。
注意 − 本章某些部分使用了Load和Store之類的命令。請參閱各個章節以獲取有關它們的詳細資訊。
Shell 命令
Apache Pig 的 Grunt shell 主要用於編寫 Pig Latin 指令碼。在此之前,我們可以使用sh和fs呼叫任何 shell 命令。
sh 命令
使用sh命令,我們可以從 Grunt shell 呼叫任何 shell 命令。從 Grunt shell 使用sh命令,我們無法執行屬於 shell 環境一部分的命令(例如 - cd)。
語法
以下是sh命令的語法。
grunt> sh shell command parameters
示例
我們可以使用如下所示的sh選項,從 Grunt shell 呼叫 Linux shell 的ls命令。在此示例中,它列出了/pig/bin/目錄中的檔案。
grunt> sh ls pig pig_1444799121955.log pig.cmd pig.py
fs 命令
使用fs命令,我們可以從 Grunt shell 呼叫任何 FsShell 命令。
語法
以下是fs命令的語法。
grunt> sh File System command parameters
示例
我們可以使用 fs 命令從 Grunt shell 呼叫 HDFS 的 ls 命令。在下面的示例中,它列出了 HDFS 根目錄中的檔案。
grunt> fs –ls Found 3 items drwxrwxrwx - Hadoop supergroup 0 2015-09-08 14:13 Hbase drwxr-xr-x - Hadoop supergroup 0 2015-09-09 14:52 seqgen_data drwxr-xr-x - Hadoop supergroup 0 2015-09-08 11:30 twitter_data
同樣,我們可以使用fs命令從 Grunt shell 呼叫所有其他檔案系統 shell 命令。
實用程式命令
Grunt shell 提供了一組實用程式命令。這些包括實用程式命令,例如clear、help、history、quit和set;以及用於從 Grunt shell 控制 Pig 的命令,例如exec、kill和run。以下是 Grunt shell 提供的實用程式命令的描述。
clear 命令
clear命令用於清除 Grunt shell 的螢幕。
語法
您可以使用如下所示的clear命令清除 Grunt shell 的螢幕。
grunt> clear
help 命令
help命令將為您提供 Pig 命令或 Pig 屬性的列表。
用法
您可以使用如下所示的help命令獲取 Pig 命令列表。
grunt> help
Commands: <pig latin statement>; - See the PigLatin manual for details:
http://hadoop.apache.org/pig
File system commands:fs <fs arguments> - Equivalent to Hadoop dfs command:
http://hadoop.apache.org/common/docs/current/hdfs_shell.html
Diagnostic Commands:describe <alias>[::<alias] - Show the schema for the alias.
Inner aliases can be described as A::B.
explain [-script <pigscript>] [-out <path>] [-brief] [-dot|-xml]
[-param <param_name>=<pCram_value>]
[-param_file <file_name>] [<alias>] -
Show the execution plan to compute the alias or for entire script.
-script - Explain the entire script.
-out - Store the output into directory rather than print to stdout.
-brief - Don't expand nested plans (presenting a smaller graph for overview).
-dot - Generate the output in .dot format. Default is text format.
-xml - Generate the output in .xml format. Default is text format.
-param <param_name - See parameter substitution for details.
-param_file <file_name> - See parameter substitution for details.
alias - Alias to explain.
dump <alias> - Compute the alias and writes the results to stdout.
Utility Commands: exec [-param <param_name>=param_value] [-param_file <file_name>] <script> -
Execute the script with access to grunt environment including aliases.
-param <param_name - See parameter substitution for details.
-param_file <file_name> - See parameter substitution for details.
script - Script to be executed.
run [-param <param_name>=param_value] [-param_file <file_name>] <script> -
Execute the script with access to grunt environment.
-param <param_name - See parameter substitution for details.
-param_file <file_name> - See parameter substitution for details.
script - Script to be executed.
sh <shell command> - Invoke a shell command.
kill <job_id> - Kill the hadoop job specified by the hadoop job id.
set <key> <value> - Provide execution parameters to Pig. Keys and values are case sensitive.
The following keys are supported:
default_parallel - Script-level reduce parallelism. Basic input size heuristics used
by default.
debug - Set debug on or off. Default is off.
job.name - Single-quoted name for jobs. Default is PigLatin:<script name>
job.priority - Priority for jobs. Values: very_low, low, normal, high, very_high.
Default is normal stream.skippath - String that contains the path.
This is used by streaming any hadoop property.
help - Display this message.
history [-n] - Display the list statements in cache.
-n Hide line numbers.
quit - Quit the grunt shell.
history 命令
此命令顯示自呼叫 Grunt shell 以來已執行/使用的語句列表。
用法
假設自開啟 Grunt shell 以來,我們已執行了三個語句。
grunt> customers = LOAD 'hdfs://:9000/pig_data/customers.txt' USING PigStorage(',');
grunt> orders = LOAD 'hdfs://:9000/pig_data/orders.txt' USING PigStorage(',');
grunt> student = LOAD 'hdfs://:9000/pig_data/student.txt' USING PigStorage(',');
然後,使用history命令將產生以下輸出。
grunt> history
customers = LOAD 'hdfs://:9000/pig_data/customers.txt' USING PigStorage(',');
orders = LOAD 'hdfs://:9000/pig_data/orders.txt' USING PigStorage(',');
student = LOAD 'hdfs://:9000/pig_data/student.txt' USING PigStorage(',');
set 命令
set命令用於顯示/為 Pig 中使用的鍵分配值。
用法
使用此命令,您可以為以下鍵設定值。
| 鍵 | 描述和值 |
|---|---|
| default_parallel | 您可以透過將任何整數作為值傳遞給此鍵來設定 map 作業的 reducer 數量。 |
| debug | 您可以透過將 on/off 傳遞給此鍵來關閉或開啟 Pig 中的除錯功能。 |
| job.name | 您可以透過將字串值傳遞給此鍵來將作業名稱設定為所需的作業。 |
| job.priority | 您可以透過將以下值之一傳遞給此鍵來為作業設定作業優先順序:
|
| stream.skippath | 對於流式處理,您可以設定不應傳輸資料的路徑,方法是將所需路徑以字串的形式傳遞給此鍵。 |
quit 命令
您可以使用此命令退出 Grunt shell。
用法
如下所示退出 Grunt shell。
grunt> quit
現在讓我們看一下可以使用哪些命令從 Grunt shell 控制 Apache Pig。
exec 命令
使用exec命令,我們可以從 Grunt shell 執行 Pig 指令碼。
語法
以下是實用程式命令exec的語法。
grunt> exec [–param param_name = param_value] [–param_file file_name] [script]
示例
讓我們假設在 HDFS 的/pig_data/目錄中有一個名為student.txt的檔案,其內容如下所示。
Student.txt
001,Rajiv,Hyderabad 002,siddarth,Kolkata 003,Rajesh,Delhi
並且,假設我們在 HDFS 的/pig_data/目錄中有一個名為sample_script.pig的指令碼檔案,其內容如下所示。
Sample_script.pig
student = LOAD 'hdfs://:9000/pig_data/student.txt' USING PigStorage(',')
as (id:int,name:chararray,city:chararray);
Dump student;
現在,讓我們使用如下所示的exec命令從 Grunt shell 執行上述指令碼。
grunt> exec /sample_script.pig
輸出
exec命令執行sample_script.pig中的指令碼。按照指令碼中的指示,它將student.txt檔案載入到 Pig 中,並給出 Dump 運算子的結果,顯示以下內容。
(1,Rajiv,Hyderabad) (2,siddarth,Kolkata) (3,Rajesh,Delhi)
kill 命令
您可以使用此命令從 Grunt shell 終止作業。
語法
以下是kill命令的語法。
grunt> kill JobId
示例
假設有一個正在執行的 Pig 作業,其 ID 為Id_0055,您可以使用如下所示的kill命令從 Grunt shell 終止它。
grunt> kill Id_0055
run 命令
您可以使用run命令從 Grunt shell 執行 Pig 指令碼。
語法
以下是run命令的語法。
grunt> run [–param param_name = param_value] [–param_file file_name] script
示例
讓我們假設在 HDFS 的/pig_data/目錄中有一個名為student.txt的檔案,其內容如下所示。
Student.txt
001,Rajiv,Hyderabad 002,siddarth,Kolkata 003,Rajesh,Delhi
並且,假設我們在本地檔案系統中有一個名為sample_script.pig的指令碼檔案,其內容如下所示。
Sample_script.pig
student = LOAD 'hdfs://:9000/pig_data/student.txt' USING
PigStorage(',') as (id:int,name:chararray,city:chararray);
現在,讓我們使用如下所示的 run 命令從 Grunt shell 執行上述指令碼。
grunt> run /sample_script.pig
您可以使用如下所示的Dump 運算子檢視指令碼的輸出。
grunt> Dump; (1,Rajiv,Hyderabad) (2,siddarth,Kolkata) (3,Rajesh,Delhi)
注意 − exec和run命令之間的區別在於,如果我們使用run,則指令碼中的語句將出現在命令歷史記錄中。