修改wordcount实例,改为:
1、 对词频按降序排列
2、 输出排序为前三,和后三的数据
首先是第一项:
对词频排序,主要针对的是最后输出的部分。
**
分析程序内容:
**
WordCount.java
package org.apache.hadoop.examples;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCount {
public static final Log LOG =
LogFactory.getLog(FileInputFormat.class); //定义log变量
//map过程需要继承org.apache.hadoop.mapreduce包中的Mapper类,并重写map方法。
//这里继承Mapper接口,设置map的输入类型为<Object,Text>,输出类型为<Text,IntWritable>
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1); //one表示单词只出现一次。为IntWritable类型。
private Text word = new Text(); //word用来存储切下的单词。为Text类型。
//这里map方法的前面两个参数代表输入类型为<Object,Text>,对应<key,value>。
//后面的一个参数context应该是固定要这么写的,输出<key,value>对到中间文件
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
//对输入的行进行切词。这里输入的Text为一行文本。
//若输入文件中有多行,会由map自动对其进行处理,切分成单行,每行的文本内容为value,key为这一行到文件开头的偏移量。
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken()); //将切分后的单词存到word中。
LOG.info("map过程 : " + word + "~~~~" + key + "@@@@" + one );
context.write(word, one); //将处理结果输出到中间文件,对每个单词定词频为1,中间文件中可能会存在重复行,即一个单词出现不止一次。这里暂不处理。
}
}
}
//reduce过程需要继承org.apache.hadoop.mapreduce包中的Reducer类,并重写其reduce方法。
//这里继承Reducer接口,设置Reduce的输入类型为<Text,IntWritable>,输出类型为<Text,IntWritable>
public static class IntSumR