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 ...
随机推荐
- Start an installation from GRUB
Start an installation from GRUB Description This tip will show you how to start an installation for ...
- 从文章"避免复制与粘贴"到文章"Extract Method"的反思(1)
看了一个比我牛的人的博客园的博文"避免复制和粘贴".里面提到了重构手法Extract Method. 所以又搜了一下Extract Method. 这里先自我理解Extract ...
- 重启iis线程池和iis站点
服务器监控. 一定时间内或者iis异常,就重启线程池和站点 一般重启站点没啥用.. 重启线程池 效果明显. 重启站点: /// <summary> /// 根据名字重启站点.(没重启线程池 ...
- [BZOJ 3282] Tree 【LCT】
题目链接:BZOJ - 3282 题目分析 这道题是裸的LCT,包含 Link , Cut 和询问两点之间的路径信息. 写代码时出现的错误:Access(x) 的循环中应该切断的是原来的 Son[x] ...
- 向Python女神推荐这些年我追过的经典书籍
http://blog.csdn.net/yueguanghaidao/article/details/10416867 最近"瑞丽模特学Python"的热点牵动了大江南北程序员的 ...
- Minimal Ratio Tree
hdu2489:http://acm.hdu.edu.cn/showproblem.php?pid=2489 题意:给你一个n个节点图,图的点有边权和点权,然后选取m个节点的子图,然后求这个一棵树,然 ...
- Install php-mcrypt on CentOS 6
http://stackoverflow.com/questions/17109818/install-php-mcrypt-on-centos-6
- cout输出字符串指针
先给出通过字符型指针输出字符串的示例代码,如下: #include <iostream>using std::cout;using std::endl; int main(){ const ...
- 【HDOJ】1394 Minimum Inversion Number
逆序数的性质.1. 暴力解 #include <stdio.h> #define MAXNUM 5005 int a[MAXNUM]; int main() { int n; int i, ...
- Linux Shell编程(26)——代码块重定向
像 while, until, 和 for 循环代码块, 甚至 if/then 测试结构的代码块都能做到标准输入的重定向. 即使函数也可以使用这种重定向的格式 .所有的这些依靠代码块结尾的 < ...