Amazon Elastic MapReduceでPigを使う。(その2;Amazon EMRでサンプルプログラムを実行する)

先のログで作成したサンプルプログラムをAmazon Elastic MapReduce(EMR)で実行する。

Job Flowの作成

elastic mapreduce ruby コマンドラインツールから、Jobフローを作成する。この際、「pigを使うよ」と宣言する。
リージョンは、これまでのサンプルと同じ「US Standard(N.Virginia)」を選択した。

elastic-mapreduce --create --alive --name Spot-Cluster --instance-group master --instance-type m1.small --instance-count 1 --instance-group core --instance-type m1.small --instance-count 1 --instance-group task --instance-type m1.small --instance-count 1 --pig-interactive

マスタノードにログインしてサンプルを実行する

sshでマスターノードにログインする。

ssh -i ~/.ssh/[your keypair].pem hadoop@[your host name of ec2  instance working as master].amazonaws.com


pigと入力すると、Gruntが動くのが確認できる。quitで終了する。

hadoop@ip-10-116-119-123:~$ pig
2013-06-28 07:52:03,265 [main] INFO  org.apache.pig.Main - Logging error messages to: /home/hadoop/pig_1372405923260.log
2013-06-28 07:52:03,309 [main] INFO  org.apache.pig.impl.util.Utils - Default bootup file /home/hadoop/.pigbootup not found
2013-06-28 07:52:04,228 [main] INFO  org.apache.pig.backend.hadoop.executionengine.HExecutionEngine - Connecting to hadoop file system at: hdfs://10.116.119.123:9000
2013-06-28 07:52:07,612 [main] INFO  org.apache.pig.backend.hadoop.executionengine.HExecutionEngine - Connecting to map-reduce job tracker at: 10.116.119.123:9001
grunt> 

先のログで作成したサンプルデータ(text.txt)をmasterノードに送る。

sftp ~/.ssh/[your keypair].pem hadoop@[your host name of ec2  instance working as master].amazonaws.com
Connected to [your host name of ec2  instance working as master].amazonaws.com.
sftp> lcd Documents
sftp> put test.txt test.txt
Uploading test.txt to /home/hadoop/test.txt
test.txt                                      100%   22     0.0KB/s   00:00    

SSHでログインしているターミナルから、test.txtをHDFSにおく。

hadoop@ip-10-116-119-123:~$ hadoop fs -mkdir input         
hadoop@ip-10-116-119-123:~$ hadoop fs -ls .       
Found 1 items
drwxr-xr-x   - hadoop supergroup          0 2013-06-28 08:09 /user/hadoop/input
hadoop@ip-10-116-119-123:~$ hadoop fs -copyFromLocal test.txt ./input
hadoop@ip-10-116-119-123:~$ hadoop fs -ls ./input
Found 1 items
-rw-r--r--   1 hadoop supergroup         22 2013-06-28 08:12 /user/hadoop/input/test.txt

Pigと入力しGruntを起動し、入力ファイルがHDFSから読み込めるかチェックする。

grunt> sample_input = LOAD '/user/hadoop/input/test.txt' AS (no:int,name:chararray);
grunt> DUMP sample_input;

結果は以下となった。日本語が化ける。。。
MAP ONLYとあるように、読み込んでDUMPするだけの命令なら、reduceプログラムは作成されず、mapプログラムだけで完結してしまう。

...
...
Success!

Job Stats (time in seconds):
JobId	Maps	Reduces	MaxMapTime	MinMapTIme	AvgMapTime	MaxReduceTime	MinReduceTime	AvgReduceTime	Alias	Feature	Outputs
job_201306280742_0006	1	0	9	9	9	0	0	sample_input	MAP_ONLY	hdfs://10.116.119.123:9000/tmp/temp-1107192093/tmp-882673410,

Input(s):
Successfully read 2 records (35 bytes) from: "/user/hadoop/input/test.txt"

Output(s):
Successfully stored 2 records (35 bytes) in: "hdfs://10.116.119.123:9000/tmp/temp-1107192093/tmp-882673410"

Counters:
Total records written : 2
Total bytes written : 35
Spillable Memory Manager spill count : 0
Total bags proactively spilled: 0
Total records proactively spilled: 0

Job DAG:
job_201306280742_0006


2013-06-28 08:27:08,587 [main] INFO  org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MapReduceLauncher - Success!
2013-06-28 08:27:08,690 [main] INFO  org.apache.hadoop.mapreduce.lib.input.FileInputFormat - Total input paths to process : 1
2013-06-28 08:27:08,690 [main] INFO  org.apache.pig.backend.hadoop.executionengine.util.MapRedUtil - Total input paths to process : 1
(1,hoge)
(2,????)

正しいスキームで読み込めたか確認する。

grunt> DESCRIBE sample_input;
sample_input: {no: int,name: chararray}
sample_input = LOAD '/user/hadoop/input/test.txt' AS (no:int,name:chararray);
DUMP sample_input;

を~/test.pigとして保管する。

pig test.pig

とすると、Gruntで対話式に実行したのと同じ結果を(バッチ処理として)得ることができる。