package org.apache.hadoop.examples;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map.Entry;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.lib.aggregate.ValueAggregatorBaseDescriptor;
import org.apache.hadoop.mapred.lib.aggregate.ValueAggregatorJob;
/**
hadoop的map/reduce例子,求的是单词在文本中的直方图,本人不解其意。
执行的命令也是需要-libjars参数:
hadoop jar hadoop-example.jar -libjars hadoop-example.jar shakepoems.text out_aggregate_his 3 textinputformat
利用系统已经实现的算法来写map/reduce程序简化开发,算法类型如下:
UniqValueCount
LongValueSum
DoubleValueSum
ValueHistogram
LongValueMax
LongValueMin
StringValueMax
StringValueMin
运行的结果是:
WORD_HISTOGRAM 11247 1 1 1710 4.4786165199608785 32.29810604730843
这个和直方图有啥关系?希望高人指点迷津。
* This is an example Aggregated Hadoop Map/Reduce application. Computes the
* histogram of the words in the input texts.
*
* To run: bin/hadoop jar hadoop-*-examples.jar aggregatewordhist <i>in-dir</i>
* <i>out-dir</i> <i>numOfReducers</i> textinputformat
*
*/
public class AggregateWordHistogram {
public static class AggregateWordHistogramPlugin
extends ValueAggregatorBaseDescriptor {
/**指定算法类型是VALUE_HISTOGRAM
* Parse the given value, generate an aggregation-id/value pair per word.
* The ID is of type VALUE_HISTOGRAM, with WORD_HISTOGRAM as the real id.
* The value is WORD\t1.
*
* @return a list of the generated pairs.
*/
@Override
public ArrayList<Entry<Text, Text>> generateKeyValPairs(Object key, Object val) {
String words[] = val.toString().split(" |\t");
ArrayList<Entry<Text, Text>> retv = new ArrayList<Entry<Text, Text>>();
for (int i = 0; i < words.length; i++) {
Text valCount = new Text(words[i] + "\t" + "1");
Entry<Text, Text> en = generateEntry(VALUE_HISTOGRAM, "WORD_HISTOGRAM",
valCount);
retv.add(en);
}
return retv;
}
}
/**
* The main driver for word count map/reduce program. Invoke this method to
* submit the map/reduce job.
*
* @throws IOException
* When there is communication problems with the job tracker.
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
JobConf conf = ValueAggregatorJob.createValueAggregatorJob(args
, new Class[] {AggregateWordHistogramPlugin.class});
JobClient.runJob(conf);
}
}