MapReduce明星搜索指数统计,找出人气王
我们继续通过项目强化掌握Combiner和Partitioner优化Hadoop性能
1、项目介绍
本项目我们使用明星搜索指数数据,分别统计出搜索指数最高的男明星和女明星。
2、数据集
3、分析
基于项目的需求,我们通过以下几步完成:
1、编写Mapper类,按需求将数据集解析为key=gender,value=name+hotIndex,然后输出。
2、编写Combiner类,合并Mapper输出结果,然后输出给Reducer。
3、编写Partitioner类,按性别,将结果指定给不同的Reduce执行。
4、编写Reducer类,分别统计出男、女明星的最高搜索指数。
5、编写run方法执行MapReduce任务
4、实现
package com.buaa; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
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.Tool;
import org.apache.hadoop.util.ToolRunner; /**
* @ProjectName CountStarSearchIndex
* @PackageName com.buaa
* @ClassName SearchStarIndex
* @Description 统计分别统计出男女明星最大搜索指数
* @Author 刘吉超
* @Date 2016-05-12 16:30:23
*/
public class SearchStarIndex extends Configured implements Tool {
// 分隔符\t
private static String TAB_SEPARATOR = "\t";
// 男
private static String MALE = "male";
// 女
private static String FEMALE = "female"; /*
* 解析明星数据
*/
public static class IndexMapper extends Mapper<Object, Text, Text, Text> {
/*
* 每次调用map(LongWritable key, Text value, Context context)解析一行数据。
* 每行数据存储在value参数值中。然后根据'\t'分隔符,解析出明星姓名,性别和搜索指数
*/
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
// 将数据解析为数组
String[] tokens = value.toString().split(TAB_SEPARATOR); if(tokens != null && tokens.length >= 3){
// 性别
String gender = tokens[1].trim();
// 名称、关注指数
String nameHotIndex = tokens[0].trim() + TAB_SEPARATOR + tokens[2].trim(); // 输出key=gender value=name+hotIndex
context.write(new Text(gender), new Text(nameHotIndex));
}
}
} /*
* 根据性别对数据进行分区,将 Mapper的输出结果均匀分布在 reduce上
*/
public static class IndexPartitioner extends Partitioner<Text, Text> {
@Override
public int getPartition(Text key, Text value, int numReduceTasks) {
// 按性别分区
String sex = key.toString(); // 默认指定分区 0
if(numReduceTasks == 0)
return 0; // 性别为男,选择分区0
if(MALE.equals(sex)){
return 0;
}else if(FEMALE.equals(sex)){ // 性别为女,选择分区1
return 1 % numReduceTasks;
}else // 性别未知,选择分区2
return 2 % numReduceTasks; }
} /*
* 定义Combiner,对 map端的输出结果,先进行一次合并,减少数据的网络输出
*/
public static class IndexCombiner extends Reducer<Text, Text, Text, Text> { @Override
public void reduce(Text key, Iterable<Text> values, Context context)throws IOException, InterruptedException {
int maxHotIndex = Integer.MIN_VALUE;
String name= ""; for (Text val : values) {
String[] valTokens = val.toString().split(TAB_SEPARATOR); int hotIndex = Integer.parseInt(valTokens[1]); if(hotIndex > maxHotIndex){
name = valTokens[0];
maxHotIndex = hotIndex;
}
} context.write(key, new Text(name + TAB_SEPARATOR + maxHotIndex));
}
} /*
* 统计男、女明星最高搜索指数
*/
public static class IndexReducer extends Reducer<Text, Text, Text, Text> {
/*
* 调用reduce(key, Iterable< Text> values, context)方法来处理每个key和values的集合。
* 我们在values集合中,计算出明星的最大搜索指数
*/
@Override
public void reduce(Text key, Iterable<Text> values, Context context)throws IOException, InterruptedException {
int maxHotIndex = Integer.MIN_VALUE;
String name = " "; // 根据key,迭代 values集合,求出最高搜索指数
for (Text val : values) {
String[] valTokens = val.toString().split(TAB_SEPARATOR); int hotIndex = Integer.parseInt(valTokens[1]); if (hotIndex > maxHotIndex) {
name = valTokens[0];
maxHotIndex = hotIndex;
}
} context.write(new Text(name), new Text(key + TAB_SEPARATOR + maxHotIndex));
}
} @SuppressWarnings("deprecation")
@Override
public int run(String[] args) throws Exception {
// 读取配置文件
Configuration conf = new Configuration(); // 如果目标文件夹存在,则删除
Path mypath = new Path(args[1]);
FileSystem hdfs = mypath.getFileSystem(conf);
if (hdfs.isDirectory(mypath)) {
hdfs.delete(mypath, true);
} // 新建一个任务
Job job = new Job(conf, "searchStarIndex");
// 主类
job.setJarByClass(SearchStarIndex.class); // reduce的个数设置为2
job.setNumReduceTasks(2);
// 设置Partitioner类
job.setPartitionerClass(IndexPartitioner.class); // Mapper
job.setMapperClass(IndexMapper.class);
// Reducer
job.setReducerClass(IndexReducer.class); // map 输出key类型
job.setMapOutputKeyClass(Text.class);
// map 输出value类型
job.setMapOutputValueClass(Text.class); // 设置Combiner类
job.setCombinerClass(IndexCombiner.class); // 输出结果 key类型
job.setOutputKeyClass(Text.class);
// 输出结果 value类型
job.setOutputValueClass(Text.class); // 输入路径
FileInputFormat.addInputPath(job, new Path(args[0]));
// 输出路径
FileOutputFormat.setOutputPath(job, new Path(args[1])); // 提交任务
return job.waitForCompletion(true) ? 0 : 1;
} public static void main(String[] args) throws Exception {
String[] args0 = {
"hdfs://ljc:9000/buaa/index/index.txt",
"hdfs://ljc:9000/buaa/index/out/"
};
int ec = ToolRunner.run(new Configuration(), new SearchStarIndex(), args0);
System.exit(ec);
}
}
5、运行效果
MapReduce明星搜索指数统计,找出人气王的更多相关文章
- Hadoop实战:明星搜索指数统计,找出人气王
项目介绍 本项目我们使用明星搜索指数数据,分别统计出搜索指数最高的男明星和女明星. 数据集 明星搜索指数数据集,如下图所示.猛戳此链接下载数据集 思路分析 基于项目的需求,我们通过以下几步完成: 1. ...
- MapReduce 找出共同好友
这个前提需要注意:好友之间的关系是单向的,我的好友队列里有你,你的里面不一定有我.所以思考方式需要改变. 共同好友: 某两个人的好友队列里都有的人. 第一个mapper 和 reducer 简单说:找 ...
- Dijkstra 算法,用于对有权图进行搜索,找出图中两点的最短距离
Dijkstra 算法,用于对有权图进行搜索,找出图中两点的最短距离,既不是DFS搜索,也不是BFS搜索. 把Dijkstra 算法应用于无权图,或者所有边的权都相等的图,Dijkstra 算法等同于 ...
- C语言:对传入sp的字符进行统计,三组两个相连字母“ea”"ou""iu"出现的次数,并将统计结果存入ct所指的数组中。-在数组中找出最小值,并与第一个元素交换位置。
//对传入sp的字符进行统计,三组两个相连字母“ea”"ou""iu"出现的次数,并将统计结果存入ct所指的数组中. #include <stdio.h& ...
- Python list去重及找出,统计重复项
http://bbs.chinaunix.net/thread-1680208-1-1.html 如何找出 python list 中有重复的项 http://www.cnblogs.com/feis ...
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- 海量数据处理 - 10亿个数中找出最大的10000个数(top K问题)
前两天面试3面学长问我的这个问题(想说TEG的3个面试学长都是好和蔼,希望能完成最后一面,各方面原因造成我无比想去鹅场的心已经按捺不住了),这个问题还是建立最小堆比较好一些. 先拿10000个数建堆, ...
- 海量数据中找出前k大数(topk问题)
海量数据中找出前k大数(topk问题) 前两天面试3面学长问我的这个问题(想说TEG的3个面试学长都是好和蔼,希望能完成最后一面,各方面原因造成我无比想去鹅场的心已经按捺不住了),这个问题还是建立最小 ...
- S关于使用QL声明 找出同时满足多个tag拍摄条件设置算法
表结构 Tag Table:{tag_id, tag_name} #标签表 News Table:{news_id, title,......} #新闻列表 NewsTags Table:{tag ...
随机推荐
- SEMAT[软件工程方法和理论 Software Engineering Method and Theory]
Agile software development Agile software development is a group of software development methods bas ...
- Js 简单分页(一)
网上有很多分页的插件 但是没有自己想要的 上代码吧,只有分页部分代码,css 省略了 html <div class="bar2 fr" id="pagecontr ...
- CSS font-family的順序
2016年09月07日 13時51分 wanglinqiang整理 相信大家都知道基本的用法是這樣: font-family:font1,font2,serif; 系統有font1就先用font1 如 ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- autoconf automake libtool
这是一个 autoconf / automake 的 "Hello World"gztt.ll@gmail.com 主要步骤是- 准备工程目录结构和程序- autoscan 生成 ...
- salt-API基本验证命令
配置SALT-API,网上有很多,作下来也很顺利. 我的参考: 作一下验证的记录: curl -k https://x.x.x.x:8000/login -H "Accept: applic ...
- 【UVA11324】The Largest Clique (SCC)
题意: 给一张有向图G,求一个结点数最大的结点集,使得该结点中任意两个结点 u 和 v满足:要么 u 可以到达 v, 要么 v 可以到达 u(u 和 v 相互可达也可以). 分析: Tarjan求SC ...
- Xstream之常用方式与常用注解
示例代码 Blog teamBlog = new Blog(new Author("Guilherme Silveira")); teamBlog.add(new Entry(&q ...
- c++virtual inline 是否冲突
关于inline关键字:effective c++ item33:明智运用inlining.说到:inline指令就像register指令一样,只是对编译器的一种提示,而不是一个强制命令,意思是编译器 ...
- tomcat 系统架构与设计模式 第一部分 系统架构工作原理 转
Tomcat 系统架构与设计模式,第 1 部分: 工作原理 许 令波, Java 开发工程师, 淘宝网 许令波,现就职于淘宝网,是一名 Java 开发工程师.对大型互联网架构设计颇感兴趣,并对一些开源 ...