Amazon Elastic Mapreduce + Apache Mahout レコメンデーション

Preview:

DESCRIPTION

社内エンジニア情報共有会で 使った資料です。

Citation preview

Amazon Elastic MapReduce +Apache Mahoutレコメンデーション!

アジェンダ

今回する話、しない話

する話• 使うモノの紹介• レコメンデーションをする流れ/手順

しない話• 機械学習の話• 類似指標の統計学な話•Hadoop の仕組み

難しいことは無しで!

レコメンデーション

レコメンデーション

使うモノ

使うモノ

Apache Mahout

Amazon Elastice MapReduce

Amazon S3

データ (csv ,tsv)

Mahout

Mahout

機械学習・データマイニング ライブラリ• レコメンデーションエンジン• クラスタリング • 分類

Mahout

Hadoop 上で動作する スケーラブル Java オープンソース

象 象使い

Mahout

選べる、類似指標• ピアソンの相関係数• ユークリッド距離による類似度• コサイン類似度• スピアマンの相関関係による相対順位•Tanimoto 係数• 対数尤度検定

Elastic MapReduce

Elastic MapReduce

Hadoop が! MapReduce が!簡単につかえる ブラウザからもコマンドラインからも操作可能 略称: EMR

S3

S3

静的ファイルがおけるストレージ 2014/2 に EBS と一緒に価格改定!

• … 落ち着いたら c3 インスタンスも価格改定していただけると…

全体図

全体図

全体図(今日はこっち)

実践!レコメンデーション!

準備

準備

IAM (アクセス権限)• サーバ→ S3 接続用•Mac→EMR 実行用

Server に s3cmd インストール•aws s3 コマンドでもいいけど今回は s3cmd

準備

Mac に Amazon Elastic MapReduce Ruby Client をインストール• http://aws.amazon.com/developertools/Elastic-

MapReduce/2264• http://docs.aws.amazon.com/

ElasticMapReduce/latest/DeveloperGuide/emr-cli-install.html• ruby : 1.8.7 が必要( Mac OS X mavericks の ruby

バージョンは 2.0 )

S3 に Mohout 配置 Server にテストデータを準備

IAM 設定

IAM 設定

割愛

s3cmd インストール

■ インストール # yum --enablerepo epel install s3cmd

■ 設定 # s3cmd --configure Enter new values or accept defaults in brackets with Enter. Refer to user manual for detailed description of all options.

Access key and Secret key are your identifiers for Amazon S3 Access Key: (ひみつ)★ Secret Key: (ひみつ)★

Encryption password is used to protect your files from reading by unauthorized persons while in transfer to S3 Encryption password: Path to GPG program [/usr/bin/gpg]:★

(続く)

26

s3cmd インストール

(続き) When using secure HTTPS protocol all communication with Amazon S3 servers is protected from 3rd party eavesdropping. This method is slower than plain HTTP and can't be used if you're behind a proxy Use HTTPS protocol [No]:y★

On some networks all internet access must go through a HTTP proxy. Try setting it here if you can't conect to S3 directly HTTP Proxy server name:

New settings: Access Key: (ひみつ)★ Secret Key: (ひみつ)★ Encryption password: Path to GPG program: /usr/bin/gpg Use HTTPS protocol: False HTTP Proxy server name: HTTP Proxy server port: 0

(続く)

27

(続き)

Test access with supplied credentials? [Y/n] y★ Please wait... Success. Your access key and secret key worked fine :-)

Now verifying that encryption works... Not configured. Never mind.

Save settings? [y/N] y★ Configuration saved to '/root/.s3cfg' #

28

Elastic MapReduce Ruby Client インストール

■ バージョン確認( 2.0 が入っている) $ ruby -v

■ インストール&設定 $ brew tap homebrew/dupes ; brew install apple-gcc42 $ CONFIGURE_OPTS="--without-tk" rbenv install 1.8.7-p374 →tk をのぞいてインストールしないと rbenv 実行時にエラーが出た $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile $ echo 'export PATH="$HOME/.rbenv/shims:$PATH"' >> ~/.bash_profile

■ 動作確認 $ rbenv versions → インストール済み ruby のバージョン確認

■ruby 1.8.7 インストール $ rbenv install -l → インストールできるバージョン確認 $ rbenv install 1.8.7-p374

■ バージョン切替(自分だけ使う場合) $ rbenv shell 1.8.7-p374

30

rbenv インストール& ruby1.8.7 切替

■ ディレクトリ作成 $ mkdir -p /usr/local/aws/elastic-mapreduce/

■ ダウンロード $ cd /usr/local/aws/elastic-mapreduce/ $ wget http://elasticmapreduce.s3.amazonaws.com/elastic-mapreduce-ruby.zip

■ 解凍 $ unzip elastic-mapreduce-ruby.zip

31

Elastic MapReduce Ruby Client インストール

■ テンプレコピー $ cp -p tests/credentials.json .

■ 編集 $ vi credentials.json

■ 中身 $ cat credentials.json { "access-id": " (ないしょ) ", "private-key": " (ないしょ) ", "key-pair": " ( key pair 名) ", "key-pair-file": "/Users/hoge/.ssh/(key pair 名 ).pem", "region": "ap-northeast-1" } $

■ 動作確認 $ ./elastic-mapreduce --version

32

Elastic MapReduce Ruby Client 設定

s3 に Mohout 配置

s3 に Mahout 配置

ダウンロード• mahout-core-0.8-job.jar

S3 Bucket 作成• 今回は  s3://emr-mahout002/

フォルダ作成、 mahout 配置• s3://emr-mahout002/program/mahout-core-0.8-

job.jar • s3://emr-mahout002/input/

s3 に Mahout 配置

lucene-4.3.0.tgz と bootstrap action 用のシェルを用意 EMR の /home/hadoop/lib にある lucene が古

いので置き換え lucene-4.3.0.tgz http://archive.apache.org/dist/lucene/java/

4.3.0/lucene-4.3.0.tgz update-lucene.sh

#!/bin/bashcd /home/hadoopwget https://s3.amazonaws.com/emr-mahout001/program/lucene-4.3.0.tgztar -xzf lucene-4.3.0.tgzcd librm lucene-*.jarcd ..cd lucene-4.3.0find . | grep lucene- | grep jar$ | xargs -I {} cp {} ../lib

テストデータを準備

テストデータを準備

• ユーザ ID

• 商品 ID

• レート(数が大きい方が評価がいい)• レート無しのデータでもいい( Boolean Data )

(ないしょ)

38

テストデータを準備

テストデータを準備

• 合計 6 千万件、レート無し( --booleanData )• 201307_reco.csv

• 9683050 件• 201308_reco.csv

• 10868457 件• 201309_reco.csv

• 11052025 件• 201310_reco.csv

• 1316778 件• 201311_reco.csv

• 10291417 件• 201312_reco.csv

• 9873681

# s3cmd put *.csv s3://emr-mahout002/input/201307_reco.csv -> s3://emr-mahout002/input/201307_reco.csv [1 of 6] 130166984 of 130166984 100% in 6s 18.33 MB/s done201308_reco.csv -> s3://emr-mahout002/input/201308_reco.csv [2 of 6] 146658501 of 146658501 100% in 7s 19.35 MB/s done201309_reco.csv -> s3://emr-mahout002/input/201309_reco.csv [3 of 6] 149486065 of 149486065 100% in 7s 18.30 MB/s done201310_reco.csv -> s3://emr-mahout002/input/201310_reco.csv [4 of 6] 153614716 of 153614716 100% in 7s 18.47 MB/s done201311_reco.csv -> s3://emr-mahout002/input/201311_reco.csv [5 of 6] 139924425 of 139924425 100% in 6s 20.11 MB/s done201312_reco.csv -> s3://emr-mahout002/input/201312_reco.csv [6 of 6] 133819195 of 133819195 100% in 6s 19.68 MB/s done

#

40

テストデータを転送

実行

$ ./elastic-mapreduce \> --create --name "mahout" \> --instance-group master \> --instance-type m1.small \> --instance-count 1 \> --bid-price 0.2 \> --instance-group core \> --instance-type m1.small \> --instance-count 3 \> --bid-price 0.2 \> --enable-debugging \> --bootstrap-action s3://point-ag-emr-s3001-imura/program/update-lucene.sh \> --bootstrap-name update-lucene.sh \> --bootstrap-action s3://ap-northeast-1.elasticmapreduce/bootstrap-actions/configurations/latest/memory-intensive \> --bootstrap-name Memory-intensive-configuration \> --log-uri s3://emr-mahout002/logs/ \> --jar s3://emr-mahout002/program/mahout-core-0.8-job.jar \> --main-class org.apache.mahout.cf.taste.hadoop.item.RecommenderJob \> --arg -Dmapred.input.dir=s3n://emr-mahout002/input \> --arg -Dmapred.output.dir=s3n://emr-mahout002/output`date +%Y%m%d%H%M%S` \> --arg --numRecommendations --arg 100 \> --arg --booleanData --arg true \> --arg --similarityClassname --arg SIMILARITY_EUCLIDEAN_DISTANCE \> --arg --input --arg s3://emr-mahout002/input \> --arg --output --arg s3://emr-mahout002/output`date +%Y%m%d%H%M%S`Created job flow j-U0Y9C97EX35E$

42

実行

43

実行

■master ノードと core ノードをスポットインスタンスで起動する > --instance-group master \ > --instance-type m1.small \ > --instance-count 1 \ > --bid-price 0.2 \★ このオプションを外すとオンデマンドインスタンスで起動 > --instance-group core \ > --instance-type m1.small \ > --instance-count 3 \ > --bid-price 0.2 \★ このオプションを外すとオンデマンドインスタンスで起動

• テストのとき• 仮に全部失敗してもまあいいかなってとき• 1時間以内に終わる処理で失敗してもいいかなってとき

• 1時間以内におわるなら、仮に高騰で停止した場合のインスタンス料は$0に!

44

オプション

■ レートがついていないデータを処理するときにつける > --booleanData \

■lucene 置換え > --bootstrap-action s3://point-ag-emr-s3001-imura/program/update-lucene.sh \ > --bootstrap-name update-lucene.sh \

■ なんかつけないとうまく動かないオプション > --bootstrap-action s3://ap-northeast-1.elasticmapreduce/bootstrap-actions/configurations/latest/memory-intensive \ > --bootstrap-name Memory-intensive-configuration \

■ 実行する jar ( mahout-core ) > --jar s3://emr-mahout002/program/mahout-core-0.8-job.jar \

45

オプション

■mahout のクラス > --main-class org.apache.mahout.cf.taste.hadoop.item.RecommenderJob \

■org.apache.mahout.cf.taste.hadoop.item.RecommenderJob のオプション

> --arg -Dmapred.input.dir=s3n://emr-mahout002/input \ > --arg -Dmapred.output.dir=s3n://emr-mahout002/output`date +%Y%m%d%H%M%S` \ > --arg --numRecommendations --arg 100 \ > --arg --similarityClassname --arg SIMILARITY_EUCLIDEAN_DISTANCE \★ 類似指標 > --arg --booleanData --arg true \ > --arg --input --arg s3://emr-mahout002/input \ > --arg --output --arg s3://emr-mahout002/output`date +%Y%m%d%H%M%S`

46

オプション

完了!

結果:出力結果

結果:出力結果

■part-r-00000 55 [110419:0.42909026,115739:0.40889424,( 省略 ),91575:0.044669896]215 [111461:0.47767943,117415:0.4510779,( 省略 ),108385:0.03656739]255 [62750:0.16326842,93342:0.13579611,( 省略 ),97662:0.06684246]

■ ユーザ数$ wc -l * 0 _SUCCESS 82266 part-r-00000 82510 part-r-00001 82476 part-r-00002 82361 part-r-00003 82237 part-r-00004 411850 total$

結果:処理時間/利用料金

処理時間•Example Jar Step   :   52 minutes•Hadoop Debugging   :   34 seconds

結果:処理時間/利用料金

料金(オンデマンドインスタンスの場合) $ 9.89

S3 : 1.6GB x 24h PC へ転送 : 1.6GB EC2 : m1.small x 4 x 1hour Elastic MapReduce

http://calculator.s3.amazonaws.com/index.html?lng=ja_JP#r=NRT&key=calc-E73CCF8C-CB10-439F-A1FD-EC1C82362ECB

考察

こんなことわかるかも

Link 集

Link 集

• Mahout• 本家 https://mahout.apache.org/• Mahout JP https://sites.google.com/site/mahoutjp/• クラスメソッド http://dev.classmethod.jp/cloud/aws/mahout-recommender-

with-amazon-elastic-mapreduce/• IBM DeveloperWorks :

http://www.ibm.com/developerworks/jp/java/library/j-mahout/

• Elastic MapReduce• http://www.slideshare.net/AmazonWebServicesJapan/20130925-aws-

meisterregenerateemrpublic

• lucene 置換えについて• http://www.warski.org/blog/2013/10/using-amazons-elastic-map-reduce-

to-compute-recommendations-with-apache-mahout-0-8

• Mahout インアクション• http://www.amazon.co.jp/gp/product/4873115841/ref=as_li_ss_tl?

ie=UTF8&camp=247&creative=7399&creativeASIN=4873115841&linkCode=as2&tag=outofboundsne-22