实现TOP K(选做):统计sogou500w中,发关键字次数最多的 *前20名用户UID和发关键字次数。
2015-09-18 19:27
423 查看
package day0917;
import java.io.IOException;
import java.util.TreeMap;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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 TopK {
/*实现TOP K(选做):统计sogou500w中,发关键字次数最多的
*前20名用户UID和发关键字次数。
*
* 思路:
*(1)得到每个关键字的次数
*(2)对其进行排序
*(3)选出Top20
*关键如何选出前二十
*使用reduce中cleanup()方法能得出是较为快捷的解决方式
*/
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Path inputPath = new Path(args[0]);
//args[1]="http:/master:9000:/out-1";
Path outputPath = new Path(args[1]);
@SuppressWarnings("deprecation")
Job job = new Job(new Configuration(),TopK.class.getName());
//FileSystem fs = new FileSystem(new Configuration(),, encoding)
job.setJarByClass(TopK.class);
job.setMapperClass(TopMapper.class);
job.setReducerClass(TopReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.addInputPath(job, inputPath);
FileOutputFormat.setOutputPath(job, outputPath);
job.waitForCompletion(true);
}
public static class TopMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
Text text =new Text();
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String[] line= value.toString().split("\t");
String keys = line[2];
text.set(keys);
context.write(text,new LongWritable(1));
}
}
public static class TopReducer extends Reducer< Text,LongWritable, Text, LongWritable>{
Text text = new Text();
TreeMap<Integer,String > map = new TreeMap<Integer,String>();
@Override
protected void reduce(Text key, Iterable<LongWritable> value, Context context)
throws IOException, InterruptedException {
int sum=0;//key出现次数
for (LongWritable ltext : value) {
sum+=ltext.get();
}
map.put(sum,key.toString());
//去前20条数据
if(map.size()>20){
map.remove(map.firstKey());
}
}
@Override
protected void cleanup(Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
for(Integer count:map.keySet()){
context.write(new Text(map.get(count)), new LongWritable(count));
}
}
}
}
import java.io.IOException;
import java.util.TreeMap;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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 TopK {
/*实现TOP K(选做):统计sogou500w中,发关键字次数最多的
*前20名用户UID和发关键字次数。
*
* 思路:
*(1)得到每个关键字的次数
*(2)对其进行排序
*(3)选出Top20
*关键如何选出前二十
*使用reduce中cleanup()方法能得出是较为快捷的解决方式
*/
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Path inputPath = new Path(args[0]);
//args[1]="http:/master:9000:/out-1";
Path outputPath = new Path(args[1]);
@SuppressWarnings("deprecation")
Job job = new Job(new Configuration(),TopK.class.getName());
//FileSystem fs = new FileSystem(new Configuration(),, encoding)
job.setJarByClass(TopK.class);
job.setMapperClass(TopMapper.class);
job.setReducerClass(TopReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.addInputPath(job, inputPath);
FileOutputFormat.setOutputPath(job, outputPath);
job.waitForCompletion(true);
}
public static class TopMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
Text text =new Text();
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String[] line= value.toString().split("\t");
String keys = line[2];
text.set(keys);
context.write(text,new LongWritable(1));
}
}
public static class TopReducer extends Reducer< Text,LongWritable, Text, LongWritable>{
Text text = new Text();
TreeMap<Integer,String > map = new TreeMap<Integer,String>();
@Override
protected void reduce(Text key, Iterable<LongWritable> value, Context context)
throws IOException, InterruptedException {
int sum=0;//key出现次数
for (LongWritable ltext : value) {
sum+=ltext.get();
}
map.put(sum,key.toString());
//去前20条数据
if(map.size()>20){
map.remove(map.firstKey());
}
}
@Override
protected void cleanup(Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
for(Integer count:map.keySet()){
context.write(new Text(map.get(count)), new LongWritable(count));
}
}
}
}
相关文章推荐
- NGUI简单背包系统的实现
- 【iOS】UIViewController生命周期
- 云海飞翔lee -- UIAlertController
- 电话通讯录用到的 Rui
- MySQL GUI Tools 使用简介
- Error:No suitable device found: no device found for connection "System eth0"
- ios系列开发之UITableView实现多行删除
- 如何设置UITextField的焦点?
- How-to: use spark to suport query across mysql tables and hbase tables
- iOS探究UITableView的内部代码,仿UITableView自定义
- QEMU Guest Agent
- ios开发系列之UITableView的移动行,编辑行,打开关闭区,刷新表
- UIView的autoresizingMask属性
- UI:沙盒
- Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
- JAVA中的优先级队列PriorityQueue
- iOS中的UITextView
- Samsung手机ClipboardUIManager引起的内存泄漏
- iOS中的UISearchBar
- iOS中的UIDatePicker 日期选择器