Rubyの単純な単語数リデューサーは次のようになります。
#!/usr/bin/env ruby
wordcount = Hash.new
STDIN.each_line do |line|
keyval = line.split("|")
wordcount[keyval[0]] = wordcount[keyval[0]].to_i+keyval[1].to_i
end
wordcount.each_pair do |word,count|
puts "#{word}|#{count}"
end
STDINにすべてのマッパーの中間値を取得します。特定のキーからではありません。したがって、実際には、すべてに対して1つのレデューサーしかありません(単語ごとまたは単語のセットごとにレデューサーはありません)。
ただし、Javaの例では、キーと値のリストをinoutとして取得するこのインターフェースを見ました。つまり、中間マップ値は、reducedとreducerを並行して実行する前に、キーごとにグループ化されます。
public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
これはJavaのみの機能ですか?または、Rubyを使用したHadoopストリーミングでそれを行うことはできますか?