クラウドウォッチ

Amazon Elastic MapReduce が Pigをサポート⇒サンプルを動かしてみた

2009年08月14日

Amazon Elastic MapReduceがApache Pigをサポートするようになったようです。

早速触ってみましょう。

image  

AWS Consoleの[Amazon Elastic MapReduce]をクリックして、[Create New Job Flow]をクリック

※Amazon Elastic MapReduceの登録ができないない場合は、SignUPのボタンが出ますが、クリックするとすぐに登録が完了します。

image

一番下にある[Sample Applications]から[Apache Log Reports(Pig Script)]を選択してみました。

 

image

[Output Location]の<yourbucket>のところにS3のバケット名を入力して次に進みます。

image

インスタンスの数とタイプを選びます。

image

問題なければ、これで完了です。

image

インスタンス起動時のようにjobのスタートには数分かかります。

image

数分たつと、statusがRUNNINGに変わりました。

image

RUNNINGに変わってから、行をクリックすると、下に詳細が表示され、処理の状況がわかります。

 

image

私がしてみた処理はサンプル用のアカウントでしたためか、7分で完了しました。

そして、先ほど指定したS3のバケットを見てみると、何やらアウトプットが...。

image

image

image

image 

image

[top_50_search_terms_from_bing_google]の中のファイルをダウンロードしてみてみると、

以下のような中身

------------------------------

value    625
views    426
login    224
search    195
items    112
bigtable    68
google+bigtable    59
%23%21%2Fusr%2Fbin%2Fperl+-w    46
philmont+pictures    45
%23%21%2Fusr%2Fbin%2Fperl    44
philmont    37
pvc+instrument    33
google+quick+links    33
pig    30
walla    28
vegas    27
about+me+website    26
pig+the+pc+nerd    26
homemade    24
fishing    23
comments    23
travis    21
hadoop+0.20    21
google+big+table    21
seattle    19
miscellaneous    19
m0n0wall+ipv6    19
pebble    19
biking    18
seahawks    18
Andrew+sample    16
%23!%2Fusr%2Fbin%2Fperl+-w    16
bigtable+google    15
bikes    15
powerful    15
pig+sample    15
%23%21%2Fusr%2Fbin%2Fperl+-wT    14
parade    14
majors    14
balam    13
racing    13
bigtable+example    13
escalator    13
wic+wac+woe    13
hadoop+0.20+mapper+example    13
robert    12
keiko    12
perl+%23%21%2Fusr%2Fbin%2Fperl    11
example    11
parents    11

------------------------------

検索エンジンからどういうキーワードで来ているかを解析した結果なんだろうなと。

image

AWS Consoleの詳細部分にあった、S3のバケットをS3FOXからみてみると、apacheのログらしきものが。

※S3foxで、/elasticmapreduce/samples/pig-apache/input/にアクセス

image

ダウンロードしてみたら、中身は単なるApacheのログでした。

 

もしかして、Pigスクリプトが多少書き直せれば、何とかなるかもと思い、サンプルスクリプトを見てみると、

※S3foxで、//elasticmapreduce/samples/pig-apache/にアクセスして、do-reports.pigをダウンロード

以下のような内容。

------------------------------

--
-- setup piggyback functions
--
register file:/home/hadoop/lib/pig/piggybank.jar
DEFINE EXTRACT org.apache.pig.piggybank.evaluation.string.EXTRACT();
DEFINE FORMAT org.apache.pig.piggybank.evaluation.string.FORMAT();
DEFINE REPLACE org.apache.pig.piggybank.evaluation.string.REPLACE();
DEFINE DATE_TIME org.apache.pig.piggybank.evaluation.datetime.DATE_TIME();
DEFINE FORMAT_DT org.apache.pig.piggybank.evaluation.datetime.FORMAT_DT();

--
-- import logs and break into tuples
--
raw_logs =
  -- load the weblogs into a sequence of one element tuples
  LOAD '$INPUT' USING TextLoader AS (line:chararray);

logs_base =
  -- for each weblog string convert the weblong string into a
  -- structure with named fields
  FOREACH
    raw_logs
  GENERATE  
    FLATTEN (
      EXTRACT(
        line,
        '^(\\S+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] "(.+?)" (\\S+) (\\S+) "([^"]*)" "([^"]*)"'
      )
    )
    AS (
      remoteAddr: chararray, remoteLogname: chararray, user: chararray, time: chararray,
      request: chararray, status: int, bytes_string: chararray, referrer: chararray,
      browser: chararray
    )
  ;

logs =
  -- convert from string values to typed values such as date_time and integers
  FOREACH
    logs_base
  GENERATE
    *,
    DATE_TIME(time, 'dd/MMM/yyyy:HH:mm:ss Z', 'UTC') as datetime,
    (int)REPLACE(bytes_string, '-', '0')          as bytes
  ;

--
-- determine total number of requests and bytes served by UTC hour of day
-- aggregating as a typical day across the total time of the logs
--
by_hour_count =
  -- group logs by their hour of day, counting the number of logs in that hour
  -- and the sum of the bytes of rows for that hour
  FOREACH
    (GROUP logs BY FORMAT_DT('HH',datetime))
  GENERATE
    $0,
    COUNT($1) AS num_requests,
    SUM($1.bytes) AS num_bytes
  ;

STORE by_hour_count INTO '$OUTPUT/total_requests_bytes_per_hour';

--
-- top 50 X.X.X.* blocks
--
by_ip_count =
  -- group weblog entries by the ip address from the remote address field
  -- and count the number of entries for each address as well as
  -- the sum of the bytes
  FOREACH
    (GROUP logs BY FORMAT('%s.*', EXTRACT(remoteAddr, '(\\d+\\.\\d+\\.\\d+)')))
  GENERATE
    $0,
    COUNT($1) AS num_requests,
    SUM($1.bytes) AS num_bytes
  ;

by_ip_count_sorted =
  -- order ip by the number of requests they make
  LIMIT (ORDER by_ip_count BY num_requests DESC) 50;

STORE by_ip_count_sorted into '$OUTPUT/top_50_ips';

--
-- top 50 external referrers
--
by_referrer_count =
  -- group by the referrer URL and count the number of requests
  FOREACH
    (GROUP logs BY EXTRACT(referrer, '(http:\\/\\/[a-z0-9\\.-]+)'))
  GENERATE
    FLATTEN($0),
    COUNT($1) AS num_requests
  ;

by_referrer_count_filtered =
  -- exclude matches for example.org
  FILTER by_referrer_count BY NOT $0 matches '.*example\\.org';

by_referrer_count_sorted =
  -- take the top 50 results
  LIMIT (ORDER by_referrer_count_filtered BY num_requests DESC) 50;

STORE by_referrer_count_sorted INTO '$OUTPUT/top_50_external_referrers';
--
-- top search terms coming from bing or google
--
google_and_bing_urls =
  -- find referrer fields that match either bing or google
  FILTER
    (FOREACH logs GENERATE referrer)
  BY
    referrer matches '.*bing.*'
  OR
    referrer matches '.*google.*'
  ;

search_terms =
  -- extract from each referrer url the search phrases
  FOREACH
    google_and_bing_urls
  GENERATE
    FLATTEN(EXTRACT(referrer, '.*[&\\?]q=([^&]+).*')) as (term:chararray)
  ;

search_terms_filtered =
  -- reject urls that contained no search terms
  FILTER search_terms BY NOT $0 IS NULL;

search_terms_count =
  -- for each search phrase count the number of weblogs entries that contained it
  FOREACH
    (GROUP search_terms_filtered BY $0)
  GENERATE
    $0,
    COUNT($1) AS num
  ;

search_terms_count_sorted =
  -- take the top 50 results
  LIMIT (ORDER search_terms_count BY num DESC) 50;

STORE search_terms_count_sorted INTO '$OUTPUT/top_50_search_terms_from_bing_google';

-----------------------------

お!「Pigって何?」と前提の知識は全くないけど、多少いじるくらいなら何とかなりそう。

せっかくだから、今後本番環境で動かしている大量のログを解析してみる予定。

 

MapReduceは使ったことがなかったけど、実は「MapReduceって何?」「Pigって何?」な僕でも、AWS Consoleでぽちぽちしていくだけで使えそうな予感...

 

ちなみに、AWSの英語のチュートリアル動画チュートリアルもあります。

image

Tags:Amazon, EC2, Elastic MapReduce

上へもどる