Hadoop MapReduce编程 API入门系列之分区和合并(十四)
不多说,直接上代码。






代码
package zhouls.bigdata.myMapReduce.Star; 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;
/**
*
* @function 统计分别统计出男女明星最大搜索指数
* @author 小讲
*/ /*
姓名 性别 搜索指数
李易峰 male 32670
朴信惠 female 13309
林心如 female 5242
黄海波 male 5505
成龙 male 7757
刘亦菲 female 14830
angelababy female 55083
王宝强 male 9472
郑爽 female 9279
周杰伦 male 42020
莫小棋 female 13978
朱一龙 male 10524
宋智孝 female 12494
吴京 male 6684
赵丽颖 female 24174
尹恩惠 female 5985
李金铭 female 5925
关之琳 female 7668
邓超 male 11532
钟汉良 male 8289
周润发 male 4808
甄子丹 male 5479
林妙可 female 5306
柳岩 female 8221
蔡琳 female 7320
张佳宁 female 6628
裴涩琪 female 5658
李晨 male 9559
周星驰 male 11483
杨紫 female 11094
全智贤 female 5336
张柏芝 female 9337
孙俪 female 7295
鲍蕾 female 5375
杨幂 female 20238
刘德华 male 19786
柯震东 male 6398
张国荣 male 5013
王阳 male 5169
李小龙 male 6859
林志颖 male 4512
林正英 male 5832
吴秀波 male 5668
陈伟霆 male 12817
陈奕迅 male 10472
赵又廷 male 5190
张馨予 female 35062
陈晓 male 17901
赵韩樱子 female 7077
乔振宇 male 8877
宋慧乔 female 5708
韩艺瑟 female 5426
张翰 male 7012
谢霆锋 male 6654
刘晓庆 female 5553
陈翔 male 7999
陈学冬 male 8829
秋瓷炫 female 6504
王祖蓝 male 6662
吴亦凡 male 16472
陈妍希 female 32590
倪妮 female 9278
高梓淇 male 7101
赵奕欢 female 7197
赵本山 male 12655
高圆圆 female 13688
陈赫 male 6820
鹿晗 male 32492
贾玲 female 5304
宋佳 female 6202
郭碧婷 female 5295
唐嫣 female 12055
杨蓉 female 10512
李钟硕 male 26278
郑秀晶 female 10479
熊黛林 female 26732
金秀贤 male 11370
古天乐 male 4954
黄晓明 male 10964
李敏镐 male 10512
王丽坤 female 5501
谢依霖 female 7000
陈冠希 male 9135
范冰冰 female 13734
姚笛 female 6953
彭于晏 male 14136
张学友 male 4578
谢娜 female 6886
胡歌 male 8015
古力娜扎 female 8858
黄渤 male 7825
周韦彤 female 7677
刘诗诗 female 16548
郭德纲 male 10307
郑恺 male 21145
赵薇 female 5339
李连杰 male 4621
宋茜 female 11164
任重 male 8383
李若彤 female 9968 得到:
angelababy female 55083
周杰伦 male 42020
*/
public class Star extends Configured implements Tool{
/**
* @function Mapper 解析明星数据
* @input key=偏移量 value=明星数据
* @output key=gender value=name+hotIndex
*/
public static class ActorMapper extends Mapper<Object,Text,Text,Text>{
//在这个例子里,第一个参数Object是Hadoop根据默认值生成的,一般是文件块里的一行文字的行偏移数,这些偏移数不重要,在处理时候一般用不上
public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
//拿:周杰伦 male 42020
//value=name+gender+hotIndex
String[] tokens = value.toString().split("\t");//使用分隔符\t,将数据解析为数组 tokens
String gender = tokens[1].trim();//性别,trim()是去除两边空格的方法
//tokens[0] tokens[1] tokens[2]
//周杰伦 male 42020
String nameHotIndex = tokens[0] + "\t" + tokens[2];//名称和关注指数
//输出key=gender value=name+hotIndex
context.write(new Text(gender), new Text(nameHotIndex));//写入gender是k2,nameHotIndex是v2
// context.write(gender,nameHotIndex);等价
//将gender和nameHotIndex写入到context中
}
} /**
* @function Partitioner 根据sex选择分区
*/
public static class ActorPartitioner extends Partitioner<Text, Text>{
@Override
public int getPartition(Text key, Text value, int numReduceTasks){
String sex = key.toString();//按性别分区 // 默认指定分区 0
if(numReduceTasks==0)
return 0; //性别为male 选择分区0
if(sex.equals("male"))
return 0;
//性别为female 选择分区1
if(sex.equals("female"))
return 1 % numReduceTasks;
//其他性别 选择分区2
else
return 2 % numReduceTasks; }
} /**
* @function 定义Combiner 合并 Mapper 输出结果
*/
public static class ActorCombiner extends Reducer<Text, Text, Text, Text>{
private Text text = new Text();
@Override
public void reduce(Text key, Iterable<Text> values, Context context)throws IOException, InterruptedException{
int maxHotIndex = Integer.MIN_VALUE;
int hotIndex = 0;
String name="";
for (Text val : values){//星型for循环,即把values的值传给Text val
String[] valTokens = val.toString().split("\\t");
hotIndex = Integer.parseInt(valTokens[1]);
if(hotIndex>maxHotIndex){
name = valTokens[0];
maxHotIndex = hotIndex;
}
}
text.set(name+"\t"+maxHotIndex);
context.write(key, text);
}
} /**
* @function Reducer 统计男、女明星最高搜索指数
* @input key=gender value=name+hotIndex
* @output key=name value=gender+hotIndex(max)
*/
public static class ActorReducer 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 = " ";
int hotIndex = 0;
// 根据key,迭代 values 集合,求出最高搜索指数
for (Text val : values){//星型for循环,即把values的值传给Text val
String[] valTokens = val.toString().split("\\t");
hotIndex = Integer.parseInt(valTokens[1]);
if (hotIndex > maxHotIndex){
name = valTokens[0];
maxHotIndex = hotIndex;
}
}
context.write(new Text(name), new Text(key + "\t"+ maxHotIndex));//写入name是k3,key + "\t"+ maxHotIndex是v3
// context.write(name,key + "\t"+ maxHotIndex);//等价
}
} /**
* @function 任务驱动方法
* @param args
* @return
* @throws Exception
*/ public int run(String[] args) throws Exception{
// TODO Auto-generated method stub Configuration conf = new Configuration();//读取配置文件,比如core-site.xml等等
Path mypath = new Path(args[1]);//Path对象mypath
FileSystem hdfs = mypath.getFileSystem(conf);//FileSystem对象hdfs
if (hdfs.isDirectory(mypath)){
hdfs.delete(mypath, true);
} Job job = new Job(conf, "star");//新建一个任务
job.setJarByClass(Star.class);//主类 job.setNumReduceTasks(2);//reduce的个数设置为2
job.setPartitionerClass(ActorPartitioner.class);//设置Partitioner类 job.setMapperClass(ActorMapper.class);//Mapper
job.setMapOutputKeyClass(Text.class);//map 输出key类型
job.setMapOutputValueClass(Text.class);//map 输出value类型 job.setCombinerClass(ActorCombiner.class);//设置Combiner类 job.setReducerClass(ActorReducer.class);//Reducer
job.setOutputKeyClass(Text.class);//输出结果 key类型
job.setOutputValueClass(Text.class);//输出结果 value类型 FileInputFormat.addInputPath(job, new Path(args[0]));// 输入路径
FileOutputFormat.setOutputPath(job, new Path(args[1]));// 输出路径
job.waitForCompletion(true);//提交任务
return 0;
} /**
* @function main 方法
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception{
// String[] args0 = { "hdfs://HadoopMaster:9000/star/star.txt",
// "hdfs://HadoopMaster:9000/out/star/" };
String[] args0 = { "./data/star/star.txt",
"./out/star" }; int ec = ToolRunner.run(new Configuration(), new Star(), args0);
System.exit(ec);
}
}
Hadoop MapReduce编程 API入门系列之分区和合并(十四)的更多相关文章
- Hadoop MapReduce编程 API入门系列之倒排索引(二十四)
不多说,直接上代码. 2016-12-12 21:54:04,509 INFO [org.apache.hadoop.metrics.jvm.JvmMetrics] - Initializing JV ...
- Hadoop MapReduce编程 API入门系列之join(二十六)(未完)
不多说,直接上代码. 天气记录数据库 Station ID Timestamp Temperature 气象站数据库 Station ID Station Name 气象站和天气记录合并之后的示意图如 ...
- Hadoop MapReduce编程 API入门系列之小文件合并(二十九)
不多说,直接上代码. Hadoop 自身提供了几种机制来解决相关的问题,包括HAR,SequeueFile和CombineFileInputFormat. Hadoop 自身提供的几种小文件合并机制 ...
- Hadoop MapReduce编程 API入门系列之压缩和计数器(三十)
不多说,直接上代码. Hadoop MapReduce编程 API入门系列之小文件合并(二十九) 生成的结果,作为输入源. 代码 package zhouls.bigdata.myMapReduce. ...
- Hadoop MapReduce编程 API入门系列之挖掘气象数据版本3(九)
不多说,直接上干货! 下面,是版本1. Hadoop MapReduce编程 API入门系列之挖掘气象数据版本1(一) 下面是版本2. Hadoop MapReduce编程 API入门系列之挖掘气象数 ...
- Hadoop MapReduce编程 API入门系列之挖掘气象数据版本2(十)
下面,是版本1. Hadoop MapReduce编程 API入门系列之挖掘气象数据版本1(一) 这篇博文,包括了,实际生产开发非常重要的,单元测试和调试代码.这里不多赘述,直接送上代码. MRUni ...
- Hadoop MapReduce编程 API入门系列之网页流量版本1(二十一)
不多说,直接上代码. 对流量原始日志进行流量统计,将不同省份的用户统计结果输出到不同文件. 代码 package zhouls.bigdata.myMapReduce.areapartition; i ...
- Hadoop MapReduce编程 API入门系列之统计学生成绩版本2(十八)
不多说,直接上代码. 统计出每个年龄段的 男.女 学生的最高分 这里,为了空格符的差错,直接,我们有时候,像如下这样的来排数据. 代码 package zhouls.bigdata.myMapRedu ...
- Hadoop MapReduce编程 API入门系列之MapReduce多种输入格式(十七)
不多说,直接上代码. 代码 package zhouls.bigdata.myMapReduce.ScoreCount; import java.io.DataInput; import java.i ...
随机推荐
- 三维重建面试13X:一些算法试题-今日头条AI-Lab
被人牵着鼻子走,到了地方还墨明棋妙地吃一顿砖头.今日头条AI-Lab,其实我一直发现,最擅长的还是点云图像处理,且只是点云处理. 一.C++题目 New 与Malloc的区别: ...
- 图像局部显著性—点特征(SURF)
1999年的SIFT(ICCV 1999,并改进发表于IJCV 2004,本文描述):参考描述:图像特征点描述. 参考原文:SURF特征提取分析 本文有大量删除,如有疑义,请参考原文. SURF对SI ...
- PAT_A1148#Werewolf - Simple Version
Source: PAT 1148 Werewolf - Simple Version (20 分) Description: Werewolf(狼人杀) is a game in which the ...
- pymysql.err.ProgrammingError: (1064)(字符串转译问题)
代码: sql = "insert into dm_copy(演出类型,演出场馆,剧目名称,演出地点,演出时间,演出票价,演出团体,创建时间, url)values('%s','%s','% ...
- [系统资源]port range
ip_local_port_range 端口范围 sysctl Linux中有限定端口的使用范围,如果我要为我的程序预留某些端口,那么我需要控制这个端口范围, 本文主要描述如何去修改端口范围. /pr ...
- 多种方法爬取猫眼电影Top100排行榜,保存到csv文件,下载封面图
参考链接: https://blog.csdn.net/BF02jgtRS00XKtCx/article/details/83663400 https://www.makcyun.top/web_sc ...
- Linux 常用密令总结 ------随用随记吧
ubuntu or uqilin 目录文件类ls 查看目录cd 转到目录ps -aux查看所有进程grep 查找| 管道符 用户账户类 su 切换用户sudo 以管理员权限运行命令重启机器 reboo ...
- 2017年JavaScript框架---Top5
前言 个人观点,供您参考 观点源自作者的使用经验和日常研究 排名基于框架的受欢迎度, 语法结构, 易用性等特性 希望大家能够基于此视频找到最适合自己的框架 下面介绍的都是严格的前端框架和库 前言 To ...
- 0808关于RDS如何恢复到本地教程
转自http://www.cnblogs.com/ilanni/archive/2016/02/25/5218129.html 公司目前使用的数据库是阿里云的RDS,目前RDS的版本为mysql5.6 ...
- javascript-jsonp的用法
原文地址 $.ajax({ type: "get", async: false, url: "ajax.ashx", dataType: "jsonp ...