調(diào)用Grunt shell后,可以在shell中運行Pig腳本。除此之外,還有由Grunt shell提供的一些有用的shell和實用程序命令。本章講解的是Grunt shell提供的shell和實用程序命令。
注意:在本章的某些部分中,使用了Load和Store等命令。請參閱相應章節(jié)以獲取有關它們的詳細信息。
Apache Pig的Grunt shell主要用于編寫Pig Latin腳本。在此之前,我們可以使用 sh 和 fs 來調(diào)用任何shell命令。
使用 sh 命令,我們可以從Grunt shell調(diào)用任何shell命令,但無法執(zhí)行作為shell環(huán)境( ex - cd)一部分的命令。
語法
下面給出了 sh 命令的語法。
grunt> sh shell command parameters
示例
我們可以使用 sh 選項從Grunt shell中調(diào)用Linux shell的 ls 命令,如下所示。在此示例中,它列出了 /pig/bin/ 目錄中的文件。
grunt> sh ls pig pig_1444799121955.log pig.cmd pig.py
使用 fs 命令,我們可以從Grunt shell調(diào)用任何FsShell命令。
語法
下面給出了 fs 命令的語法。
grunt> sh File System command parameters
示例
我們可以使用fs命令從Grunt shell調(diào)用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中調(diào)用所有其他文件系統(tǒng)的shell命令。
Grunt shell提供了一組實用程序命令。這些包括諸如clear,help,history,quit和set等實用程序命令;以及Grunt shell中諸如 exec,kill和run等命令來控制Pig。下面給出了Grunt shell提供的實用命令的描述。
clear 命令用于清除Grunt shell的屏幕。
語法
你可以使用 clear 命令清除grunt shell的屏幕,如下所示。
grunt> clear
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.
此命令顯示自Grunt shell被調(diào)用以來執(zhí)行/使用的語句的列表。
使用
假設我們自打開Grunt shell之后執(zhí)行了三個語句。
grunt> customers = LOAD 'hdfs://localhost:9000/pig_data/customers.txt' USING PigStorage(','); grunt> orders = LOAD 'hdfs://localhost:9000/pig_data/orders.txt' USING PigStorage(','); grunt> student = LOAD 'hdfs://localhost:9000/pig_data/student.txt' USING PigStorage(',');
然后,使用 history 命令將產(chǎn)生以下輸出。
grunt> history customers = LOAD 'hdfs://localhost:9000/pig_data/customers.txt' USING PigStorage(','); orders = LOAD 'hdfs://localhost:9000/pig_data/orders.txt' USING PigStorage(','); student = LOAD 'hdfs://localhost:9000/pig_data/student.txt' USING PigStorage(',');
set 命令用于向Pig中使用的key顯示/分配值。
使用
使用此命令,可以將值設置到以下key。
Key | 說明和值 |
---|---|
default_parallel | 通過將任何整數(shù)作為值傳遞給此key來設置映射作業(yè)的reducer數(shù)。 |
debug | 關閉或打開Pig中的調(diào)試功能通過傳遞on/off到這個key。 |
job.name | 通過將字符串值傳遞給此key來將作業(yè)名稱設置為所需的作業(yè)。 |
job.priority | 通過將以下值之一傳遞給此key來設置作業(yè)的優(yōu)先級:
|
stream.skippath | 對于流式傳輸,可以通過將所需的路徑以字符串形式傳遞到此key,來設置不傳輸數(shù)據(jù)的路徑。 |
你可以使用此命令從Grunt shell退出。
使用
從Grunt shell中退出,如下所示。
grunt> quit
現(xiàn)在讓我們看看從Grunt shell控制Apache Pig的命令。
使用 exec 命令,我們可以從Grunt shell執(zhí)行Pig腳本。
語法
下面給出了實用程序命令 exec 的語法。
grunt> exec [–param param_name = param_value] [–param_file file_name] [script]
示例
我們假設在HDFS的 /pig_data/ 目錄中有一個名為 student.txt 的文件,其中包含以下內(nèi)容。
Student.txt
001,Rajiv,Hyderabad 002,siddarth,Kolkata 003,Rajesh,Delhi
并且,假設我們在HDFS的 /pig_data/ 目錄中有一個名為 sample_script.pig 的腳本文件,并具有以下內(nèi)容。
Sample_script.pig
student = LOAD 'hdfs://localhost:9000/pig_data/student.txt' USING PigStorage(',') as (id:int,name:chararray,city:chararray); Dump student;
現(xiàn)在,讓我們使用 exec 命令從Grunt shell中執(zhí)行上面的腳本,如下所示。
grunt> exec /sample_script.pig
輸出
exec 命令執(zhí)行 sample_script.pig 中的腳本。按照腳本中的指示,它會將 student.txt 文件加載到Pig中,并顯示Dump操作符的結(jié)果,顯示以下內(nèi)容。
(1,Rajiv,Hyderabad) (2,siddarth,Kolkata) (3,Rajesh,Delhi)
你可以使用此命令從Grunt shell中終止一個作業(yè)。
語法
下面給出了 kill 命令的語法。
grunt> kill JobId
示例
假設有一個具有id Id_0055 的正在運行的Pig作業(yè),使用 kill 命令從Grunt shell中終止它,如下所示。
grunt> kill Id_0055
你可以使用run命令從Grunt shell運行Pig腳本
語法
下面給出了 run 命令的語法。
grunt> run [–param param_name = param_value] [–param_file file_name] script
示例
假設在HDFS的 /pig_data/ 目錄中有一個名為 student.txt 的文件,其中包含以下內(nèi)容。
Student.txt
001,Rajiv,Hyderabad 002,siddarth,Kolkata 003,Rajesh,Delhi
并且,假設我們在本地文件系統(tǒng)中有一個名為 sample_script.pig 的腳本文件,并具有以下內(nèi)容。
Sample_script.pig
student = LOAD 'hdfs://localhost:9000/pig_data/student.txt' USING PigStorage(',') as (id:int,name:chararray,city:chararray);
現(xiàn)在,讓我們使用run命令從Grunt shell運行上面的腳本,如下所示。
grunt> run /sample_script.pig
你可以使用Dump操作符查看腳本的輸出,如下所示。
grunt> Dump; (1,Rajiv,Hyderabad) (2,siddarth,Kolkata) (3,Rajesh,Delhi)
注意: exec 和 run 命令之間的區(qū)別是,如果使用run,則腳本中的語句在history命令中可用。
更多建議: