簡単な語数の減速は、次のようになります。HadoopでRuby reducersを並列化する? 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として取得するこのインタフェースを見ました。つまり、中間のマップ値は縮小前のキーでグループ化され、縮小は平行して実行できます。
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 Streamingでそれを行うことはできますか?