Windows系エンジニアのためのElastic Map Reduce 5

Amazon EMRでHiveを使う
EMRに接続して操作するためにSSHに対応したtelnetクライアントが必要です。私自身長年使っていることもあって、TeraTerm(http://ttssh2.sourceforge.jp/)をお勧めします。
Cluster Listから作成したEMRインスタンスの詳細を表示すると、Master public DNS nameにドメイン名が表示されています。このドメインにSSHで接続すると以下のように表示されます。SSHでログインするときのユーザーIDはhadoopです。

Linux (none) 3.2.30-49.59.amzn1.x86_64 #1 SMP Wed Oct 3 19:54:33 UTC 2012 x86_64
——————————————————————————–

Welcome to Amazon Elastic MapReduce running Hadoop and Debian/Squeeze.

Hadoop is installed in /home/hadoop. Log files are in /mnt/var/log/hadoop. Check
/mnt/var/log/hadoop/steps for diagnosing step failures.

The Hadoop UI can be accessed via the following commands:

JobTracker lynx http://localhost:9100/
NameNode lynx http://localhost:9101/

——————————————————————————–
hadoop@ip-10-121-9-185:~$
hadoop@ip-10-121-9-185:~$ hive

プロンプトが表示されたらhiveと入力して、hiveのインスタンスを起動します。
先ほどS3にアップロードしたファイルをテーブルとしてマウントします。

> Create external table reviewsNew
> (MemberID String, ProductID String, ReviewDate TimeStamp,
> HelpFeeedbackNum Int, FeedbackNum Int, Rating Int,
> Title String, Body String )
> row format delimited fields terminated by ‘t’ lines terminated by ‘n’
> stored as textfile location ‘s3://emr-sample/reviewsNew’

これで集計をおこなう準備が完了しました。

> select size(split(Body, ‘ ‘)) as wordscnt, count(*)
> from reviewsNew
> group by size(split(Body, ‘ ‘))
> sort by wordscnt;

上記のようなselect文を実行して集計してみます。bodyに含まれる単語数をカウントして、各単語数のレビューが何件あるかを集計します。この手のインデックスが全く機能しないようなSQLをRDBMSで実行した場合、現実的な時間内には終了しません。hiveなら5ノード程度の小さなクラスタ構成でも7分ほどで集計が終わります。
実際に使用する場合には以下のように結果保存用のテーブルを作成しておきます。

> create external table wordscnt
> (wordcnt Int, recordCnt Int)
> row format delimited fields terminated by ‘t’ lines terminated by ‘n’
> stored as textfile location ‘s3://emr-sample/wordscnt’

そして以下のように保存先テーブルを指定し、結果保存用のテーブルに格納します。その後、S3から作成されたデータファイルをダウンロードします。

> insert into table wordscnt
> select size(split(Body, ‘ ‘)) as wordscnt, count(*)
> from reviewsNew
> group by size(split(Body, ‘ ‘))
> sort by wordscnt;

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です