wukongをMac OS Xに導入してローカルモードで動作確認するまで

RubyMapReduceするwukongについて、Hadoopを使わないローカルモードでの動作テストをしました。

Hadoopの導入

brewhadoop を導入しましたが、当面使いません。

$ brew install hadoop
$ export HADOOP_HOME="/opt/hadoop"

wukongの導入

wukong を導入して、動作モードを local にするように環境変数を設定しました。

$ sudo gem install wukong
$ export WUKONG_RUN_MODE='local'

環境変数の設定は、~/.bash_profile にも書き込んでおきます。

テストスクリプトの作成

http://mrflip.github.com/wukong/tutorial.html にある CountWords をベースに、次のようなスクリプトを作成しました。

#w.rb
require 'rubygems'
require 'wukong'

include Wukong::Streamer

class Mapper < LineStreamer
  def process line
    words = line.strip.split(/\W+/).reject(&:blank?)
    words.each {|word| yield [word, 1]}
  end
end

class Reducer < ListReducer
  def finalize
    yield [key, values.map(&:last).map(&:to_i).sum]
  end
end

Wukong::Script.new(Mapper, Reducer).run

テスト入力

テスト入力として、次のようなファイルを作成し、input.txt という名前で保存しました。

this is a pen. that is a pen.

実行

$ ruby w.rb input.txt output.txt
I, [2011-02-19T01:25:50.943709 #5767]  INFO -- :   Reading STDIN / Writing STDOUT
I, [2011-02-19T01:25:50.944960 #5767]  INFO -- : Running

 cat 'input.txt' |  /opt/local/bin/ruby /Users/hfu/improvisation/2011/02/19/w.rb --map --log_interval=10000 --log_seconds=30 | sort | /opt/local/bin/ruby /Users/hfu/improvisation/2011/02/19/w.rb --reduce --log_interval=10000 --log_seconds=30 > 'output.txt' 

UNIX の伝家の宝刀である cat と sort を使って MapReduce を実現していることが分かります。

実行結果

実行結果は、第二引数で指定した output.txt に、次のような形で現れます。

a	2
is	2
pen	2
that	1
this	1

動作確認終了です。