資源簡介
求所有文件中數(shù)值的最大三個值( Top 3 ),并按降序輸出。MR編程mapper,reducer,combiner類的源代碼
代碼片段和文件信息
package?com.top3;
import?java.io.IOException;
import?java.util.ArrayList;
import?java.util.Collections;
import?org.apache.hadoop.conf.Configuration;
import?org.apache.hadoop.fs.Path;
import?org.apache.hadoop.io.IntWritable;
import?org.apache.hadoop.io.NullWritable;
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;
public?class?TOP3?{
//@author?YQ
public?static?class?TopNMap?extends?Mapperject?Text?IntWritable?NullWritable>{
private?static?IntWritable?data?=?new?IntWritable();
@Override
????????protected?void?map(object?key?Text?valueContext?context)
????????????????throws?IOException?InterruptedException?{
?//把字符串轉(zhuǎn)為數(shù)值
??????????data.set(Integer.parseInt(value.toString().trim()));
??????????context.write(data?NullWritable.get());
?????????//?System.out.println(data);
????????}
????}
public?static?class?TopNReducer?extends?Reducer{
private?ArrayList?keyList?=?new??ArrayList();
private?IntWritable?num?=?new?IntWritable();
@Override
?????public?void?reduce(IntWritable?key?Iterable?values?Context?context)?throws?IOException?InterruptedException?{
?//int?kk?=?key.get();
?keyList.add(key.get());
?//System.out.println(kk);
?}
@Override
protected?void?cleanup(Context?context)?throws?IOException?InterruptedException{
Collections.sort(keyList);//從
評論
共有 條評論