后浪云Apache Pig教程:Apache Pig Grunt Shell

调用Grunt shell后,可以在shell中运行Pig脚本。除此之外,还有由Grunt shell提供的一些有用的shell和实用程序命令。本章讲解的是Grunt shell提供的shell和实用程序命令。

注意:在本章的某些部分中,使用了LoadStore等命令。请参阅相应章节以获取有关它们的详细信息。

Shell 命令

Apache Pig的Grunt shell主要用于编写Pig Latin脚本。在此之前,我们可以使用 sh fs 来调用任何shell命令。

sh 命令

使用 sh 命令,我们可以从Grunt shell调用任何shell命令,但无法执行作为shell环境( ex – 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,quitset等实用程序命令;以及Grunt shell中诸如 exec,killrun等命令来控制Pig。下面给出了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://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 命令将产生以下输出。

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命令

set 命令用于向Pig中使用的key显示/分配值。

使用

使用此命令,可以将值设置到以下key。

Key说明和值
default_parallel通过将任何整数作为值传递给此key来设置映射作业的reducer数。
debug关闭或打开Pig中的调试功能通过传递on/off到这个key。
job.name通过将字符串值传递给此key来将作业名称设置为所需的作业。
job.priority

通过将以下值之一传递给此key来设置作业的优先级:

  • very_low
  • low
  • normal
  • high
  • very_high
stream.skippath对于流式传输,可以通过将所需的路径以字符串形式传递到此key,来设置不传输数据的路径。

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://localhost: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

示例

假设有一个具有id Id_0055 的正在运行的Pig作业,使用 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://localhost: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,则脚本中的语句在history命令中可用。

文章来源网络,作者:管理,如若转载,请注明出处:https://shuyeidc.com/wp/292367.html<

(0)
管理的头像管理
上一篇2025-05-20 03:07
下一篇 2025-05-20 03:08

相关推荐

  • jsp空间购买和交换数据空间怎么买,有哪些注意事项?

    购买JSP空间时,是否考虑过数据交换空间的性能?简米科技(2003年始创,23年行业沉淀)与酷番云(工信部一类增值电信全牌照)这类持牌自营机房的服务商,能确保数据交换的高效稳定,是值得优先选择的合作伙伴,为什么JSP空间需要搭配独立的数据交换空间从JSP应用特性看数据交换需求JSP基于Java技术,常用于企业级……

    2026-08-11
    0
  • 建网站用香港空间效果怎么样,香港空间稳定吗?

    建网站用香港空间,对于创建网站资产来说,核心价值在于免备案和全球带宽优势,尤其适合外贸、跨境电商和需要快速启动的项目,但你必须权衡国内访问延迟,并选择有资质的服务商以保证资产安全,香港空间的核心优势与适用边界免备案:节省时间就是节省成本国内服务器需要备案,通常需要10到20天,香港空间无需备案,域名解析后即可上……

    2026-08-11
    0
  • Java连接云数据库的方法是什么,如何操作

    Java连接云数据库的核心在于通过JDBC驱动,结合云服务商提供的连接地址、端口、数据库名及认证信息,配置安全策略(如SSL、IP白名单),即可实现稳定高效的远程数据库访问,基础准备:JDBC驱动与依赖管理连接云数据库前,需要确保开发环境具备对应的JDBC驱动,以最常见的MySQL为例,你需要引入mysql-c……

    2026-08-11
    0
  • 建网站公安联网备案必须使用数据码吗,备案流程是什么

    网站备案包括ICP备案和公安联网备案,两者缺一不可,公安联网备案必须使用服务商提供的数据码,选择持有合法资质的服务商是顺利通过备案的前提,为什么网站必须进行公安联网备案根据公安部《计算机信息网络国际联网安全保护管理办法》,网站开通后30日内必须到公安机关办理备案手续,未完成公安备案的网站,面临责令整改、关闭网站……

    2026-08-10
    0
  • 建一个企业网站大概需要多少钱?,怎么收费?

    建网站要多少钱,没有一个固定的数字,几百到几万都可能,但真正的“创建网站资产”绝不仅仅是初次投入的成本,而是基于长期稳定、合规和安全的持续性投入,其中核心取决于你选择了什么样的“地基”来承载你的业务,建站预算的构成与行业基准当你开始规划一个网站,最先面对的就是预算问题,一个常见的误区是只关注网站“看起来”的建造……

    2026-08-10
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注