课程表

Apache Pig 基础

Apache Pig 诊断运算符

Apache Pig 分组和连接

Apache Pig 合并和拆分

Apache Pig 过滤

Apache Pig 排序

Pig Latin 内置函数

Apache Pig 其他执行模式

工具箱
速查手册

Apache Pig 运行脚本

当前位置:免费教程 » 大数据/云 » Apache Pig

在本章中,我们将了解如何以批处理模式运行Apache Pig脚本。

Pig脚本中的注释

在将脚本写入文件时,我们可以在其中包含注释,如下所示。

多行注释

我们将用'/*'开始多行注释,以'*/'结束。

  1. /* These are the multi-line comments
  2. In the pig script */

单行注释

我们将用“--"开始单行注释。

  1. --we can write single line comments like this.

在批处理模式下执行Pig脚本

在以批处理方式执行Apache Pig语句时,请按照以下步骤操作。

步骤1

将所有需要的Pig Latin语句写在单个文件中。我们可以将所有Pig Latin语句和命令写入单个文件,并将其另存为 .pig 文件。

步骤2

执行Apache Pig脚本。你可以从shell(Linux)执行Pig脚本,如下所示。

Local模式MapReduce模式

$ pig -x local Sample_script.pig

$ pig -x mapreduce Sample_script.pig

你可以使用exec命令从Grunt shell执行它,如下所示。

  1. grunt> exec /sample_script.pig

从HDFS执行Pig脚本

我们还可以执行驻留在HDFS中的Pig脚本。假设在名为 /pig_data/ 的HDFS目录中有名为 Sample_script.pig 的Pig脚本。我们可以执行它如下所示。

  1. $ pig -x mapreduce hdfs://localhost:9000/pig_data/Sample_script.pig

假设在HDFS中有一个具有以下内容的文件 student_details.txt

student_details.txt

  1. 001,Rajiv,Reddy,21,9848022337,Hyderabad
  2. 002,siddarth,Battacharya,22,9848022338,Kolkata
  3. 003,Rajesh,Khanna,22,9848022339,Delhi
  4. 004,Preethi,Agarwal,21,9848022330,Pune
  5. 005,Trupthi,Mohanthy,23,9848022336,Bhuwaneshwar
  6. 006,Archana,Mishra,23,9848022335,Chennai
  7. 007,Komal,Nayak,24,9848022334,trivendram
  8. 008,Bharathi,Nambiayar,24,9848022333,Chennai

我们还在同一个HDFS目录中有一个名为 sample_script.pig 的示例脚本。此文件包含对student关系执行操作和转换的语句,如下所示。

  1. student = LOAD 'hdfs://localhost:9000/pig_data/student_details.txt' USING PigStorage(',')
  2. as (id:int, firstname:chararray, lastname:chararray, phone:chararray, city:chararray);
  3. student_order = ORDER student BY age DESC;
  4. student_limit = LIMIT student_order 4;
  5. Dump student_limit;
  • 脚本的第一个语句会将名为 student_details.txt 的文件中的数据加载为名为student的关系。

  • 脚本的第二个语句将根据年龄以降序排列关系的元组,并将其存储为 student_order

  • 脚本的第三个语句会将 student_order 的前4个元组存储为 student_limit

  • 最后,第四个语句将转储关系 student_limit 的内容。

现在,执行 sample_script.pig ,如下所示。

  1. $./pig -x mapreduce hdfs://localhost:9000/pig_data/sample_script.pig

Apache Pig被执行,并提供具有以下内容的输出。

  1. (7,Komal,Nayak,24,9848022334,trivendram)
  2. (8,Bharathi,Nambiayar,24,9848022333,Chennai)
  3. (5,Trupthi,Mohanthy,23,9848022336,Bhuwaneshwar)
  4. (6,Archana,Mishra,23,9848022335,Chennai)
  5. 2015-10-19 10:31:27,446 [main] INFO org.apache.pig.Main - Pig script completed in 12
  6. minutes, 32 seconds and 751 milliseconds (752751 ms)
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号