Apache-pig-running-scripts

提供:Dev Guides
移動先:案内検索

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スクリプトを実行します。 以下に示すように、シェル(Linux)からPigスクリプトを実行できます。

Local mode MapReduce mode
$ pig -x local *Sample_script.pig * $ pig -x mapreduce* Sample_script.pig*

以下に示すように、execコマンドを使用してGruntシェルからも実行できます。

grunt> exec/sample_script.pig

HDFSからPigスクリプトを実行する

HDFSにあるPigスクリプトを実行することもできます。 /pig_data/ という名前のHDFSディレクトリに Sample_script.pig という名前のPigスクリプトがあるとします。 以下に示すように実行できます。

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

次の内容のファイル student_details.txt がHDFSにあると仮定します。

*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://localhost: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 という名前の関係としてロードします。
  • スクリプトの2番目のステートメントは、関係のタプルを年齢に基づいて降順に並べ、 student_order として保存します。
  • スクリプトの3番目のステートメントは、 student_order の最初の4つのタプルを student_limit として保存します。
  • 最後に、4番目のステートメントはリレーション student_limit の内容をダンプします。

次に示すように、 sample_script.pig を実行します。

$./pig -x mapreduce hdfs://localhost: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)