MapReduce编程小案例.2nd—求取网站访问次数Top.N

MapReduce编程小案例.2nd—求取网站访问次数Top.N

利用MapReduce处理一个小案例,如下是一批网站的访问次数信息:

2018/05/11 qq.com/a
2018/05/11 qq.com/bx
2018/05/11 qq.com/by
2018/05/11 qq.com/by3
2018/05/11 qq.com/news
2018/05/11 sina.com/news/socail
2018/05/11 163.com/ac
2018/05/11 sina.com/news/socail
2018/05/11 163.com/sport
2018/05/11 163.com/ac
2018/05/11 sina.com/play
2018/05/11 163.com/sport
2018/05/11 163.com/ac
2018/05/11 sina.com/movie
2018/05/11 sina.com/play
2018/05/11 sina.com/movie
2018/05/11 163.com/sport
2018/05/11 sina.com/movie
2018/05/11 163.com/ac
2018/05/11 163.com/ac
2018/05/11 163.com/acc
2018/05/11 qq.com/by
2018/05/11 qq.com/by3
2018/05/11 qq.com/news
2018/05/11 163.com/sport
2018/05/11 sina.com/news/socail
2018/05/11 163.com/sport
2018/05/11 sina.com/movie
2018/05/11 sina.com/news/socail
2018/05/11 sina.com/movie
2018/05/11 qq.com/news
2018/05/11 163.com/bb
2018/05/11 163.com/cc
2018/05/11 sina.com/lady/
2018/05/11 163.com/cc
2018/05/11 qq.com/news
2018/05/11 qq.com/by
2018/05/11 qq.com/by3
2018/05/11 sina.com/lady/
2018/05/11 qq.com/by3
2018/05/11 sina.com/lady/
2018/05/11 qq.com/by3
2018/05/11 qq.com/news
2018/05/11 qq.com/by3
2018/05/11 163.com/sport
2018/05/11 163.com/sport
2018/05/11 sina.com/news/socail
2018/05/11 sina.com/lady/
2018/05/11 sina.com/play
2018/05/11 sina.com/movie
2018/05/11 sina.com/music
2018/05/11 sina.com/sport
2018/05/11 sina.com/sport
2018/05/11 163.com/sport
2018/05/11 sina.com/news/socail
2018/05/11 sohu.com/lady/
2018/05/11 sohu.com/play
2018/05/11 sohu.com/movie
2018/05/11 sohu.com/music
2018/05/11 sohu.com/sport
2018/05/11 sohu.com/sport
2018/05/11 sina.com/news/socail
2018/05/11 baidu.com/lady/
2018/05/11 baidu.com/play
2018/05/11 baidu.com/movie
2018/05/11 baidu.com/music
2018/05/11 baidu.com/movie
2018/05/11 baidu.com/music
2018/05/11 baidu.com/movie
2018/05/11 baidu.com/music
2018/05/11 baidu.com/movie
2018/05/11 baidu.com/music
2018/05/11 baidu.com/movie
2018/05/11 baidu.com/music
2018/05/11 baidu.com/music
2018/05/11 baidu.com/movie
2018/05/11 baidu.com/music
2018/05/11 baidu.com/sport
2018/05/11 baidu.com/sport

设计Mapper类

package cn.edu360.mr.page.topn;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class PageTopnMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
	
	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
          String line = value.toString();
          String[] split = line.split(" ");
          
          context.write(new Text(split[1]), new IntWritable(1));
	}

}

设计Reducer类,这里注意一下,覆写一个cleanup()方法:

package cn.edu360.mr.page.topn;

import java.io.IOException;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class PageTopnReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
	
	TreeMap<PageCount, Object> treeMap = new TreeMap<PageCount, Object>();
	
	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,
			Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
    
		int count = 0;
		for (IntWritable value : values) {
			count += value.get();
			
		}
		PageCount pageCount = new PageCount();
		pageCount.set(key.toString(), count);
		
		treeMap.put(pageCount, null);
		
	}
	
	
	@Override
	protected void cleanup(Context context)
			throws IOException, InterruptedException {
            
		Configuration conf = context.getConfiguration();
		int topn = conf.getInt("top.n", 5);
		
		Set<Entry<PageCount, Object>> entrySet = treeMap.entrySet();
		
		int i = 0;
		
		for (Entry<PageCount, Object> entry : entrySet) {
			context.write(new Text(entry.getKey().getPage()), new IntWritable(entry.getKey().getCount()));
			i++;
			if(i == topn) return;
		}
	}
	

}


新建一个类PageCount封装信息,因为不用在MapReduce里面传递数据,所以不用实现序列化接口,但是因为设计比较,所以要实现Comparable接口:

package cn.edu360.mr.page.topn;

public class PageCount implements Comparable<PageCount>{
	
	private String page;
	private int count;
	
	public void set(String page, int count) {
		this.page = page;
		this.count = count;
		
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
	
	public int compareTo(PageCount o) {
		return o.getCount() - this.count == 0?this.page.compareTo(o.getPage()):o.getCount()-this.count;
	}

}

最后,老规矩,实现JobSubmitter类

package cn.edu360.mr.page.topn;

import java.io.IOException;

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.v2.app.job.event.JobDiagnosticsUpdateEvent;



public class JobSubmitter {
	
    public static void main(String[] args) throws Exception {
		
    	/*
    	 * 通过加载classpath下的*-site.xml文件解析参数
    	 */
    	Configuration conf = new Configuration();
    	conf.addResource("core-site.xml");
    	
    	/*
    	 * 通过代码设置参数
    	 */
    	//conf.setInt("top.n",3);
    	//conf.setInt("top.n",Integer.parseInt(args[0]));
    	
    	/*
    	 * 通过属性配置文件获取参数
    	 */
    	//Properties props = new Properties();
    	//props.load(JobSubmitter.class.getClassLoader().getResourceAsStream("topn.properties"));
    	//conf.setInt("top.n", Integer.parseInt(pros.getProperty("top.n")))
    	
    	
    	Job job = Job.getInstance(conf);
    	
    	job.setJarByClass(JobDiagnosticsUpdateEvent.class);
    	
    	job.setMapperClass(PageTopnMapper.class);
    	job.setReducerClass(PageTopnReducer.class);
    	
    	job.setOutputKeyClass(Text.class);
    	job.setOutputValueClass(IntWritable.class);
    	
    	FileInputFormat.setInputPaths(job, new Path("F:\\mrdata\\url\\input"));
    	FileOutputFormat.setOutputPath(job, new Path("F:\\mrdata\\url\\output"));
    	
    	job.waitForCompletion(true);
	}
	

}

阅读更多
换一批

没有更多推荐了,返回首页