Hadoop实战:明星搜索指数统计,找出人气王
发布日期:2021-10-09 07:57:13 浏览次数:1 分类:技术文章

本文共 4242 字,大约阅读时间需要 14 分钟。

项目介绍

        本项目我们使用明星搜索指数数据,分别统计出搜索指数最高的男明星和女明星。

数据集

明星搜索指数数据集,如下图所示。

思路分析

        基于项目的需求,我们通过以下几步完成:

1、编写 Mapper类,按需求将数据集解析为 key=gender,value=name+hotIndex,然后输出。

2、编写 Combiner 类,合并 Mapper 输出结果,然后输出给 Reducer。

3、编写 Partitioner 类,按性别,将结果指定给不同的 Reduce 执行。

4、编写 Reducer 类,分别统计出男、女明星的最高搜索指数。

5、编写 run 方法执行 MapReduce 任务。

MapReduce Java 项目

 设计的MapReduce如下所示:

Map = {key = gender, value = name+hotIndex}

Reduce = {key = name, value = gender+hotIndex}

Map

        每次调用map(LongWritable key, Text value, Context context)解析一行数据。每行数据存储在value参数值中。然后根据'\t'分隔符,解析出明星姓名,性别和搜索指数。

public static class ActorMapper extends Mapper< Object, Text, Text, Text> {	public void map(Object key, Text value, Context context) throws IOException, InterruptedException {		//value=name+gender+hotIndex		String[] tokens = value.toString().split("\t");		String gender = tokens[1].trim();//性别		String nameHotIndex = tokens[0] + "\t" + tokens[2];//名称和搜索指数		context.write(new Text(gender), new Text(nameHotIndex));	}}

map()函数期望的输出结果Map = {key = gender, value = name+hotIndex}

Combiner

        对 map 端的输出结果,先进行一次合并,减少数据的网络输出。

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) {			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);		}}

Partitioner

        根据明星性别对数据进行分区,将 Mapper 的输出结果均匀分布在 reduce 上。

public static class ActorPartitioner extends Partitioner< Text, Text> {		 	@Override	public int getPartition(Text key, Text value, int numReduceTasks) { 		String sex = key.toString();           		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;	}	 }

Reduce

        调用reduce(key, Iterable< Text> values, context)方法来处理每个key和values的集合。我们在values集合中,计算出明星的最大搜索指数。

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;		for (Text val : values) {			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));	}}

reduce()函数期望的输出结果Reduce = {key = name, value = gender+max(hotIndex)}

Run 驱动方法

        在 run 方法中,设置任务执行各种信息。

public int run(String[] args) throws Exception {		// TODO Auto-generated method stub				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, "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;}

编译和执行 MapReduce作业

1、myclipse将项目编译和打包为star.jar,使用SSH将 star.jar上传至hadoop的$HADOOP_HOME目录下。

2、使用cd $HADOOP_HOME切换到当前目录,通过命令行执行Hadoop作业

hadoop jar star.jar zimo.hadoop.Star.Star

运行结果

        你可以在DFS Locations界面下查看输出目录。

以上就是博主为大家介绍的这一板块的主要内容,这都是博主自己的学习过程,希望能给大家带来一定的指导作用,有用的还望大家点个支持,如果对你没用也望包涵,有错误烦请指出。如有期待可关注博主以第一时间获取更新哦,谢谢! 

 版权声明:本文为博主原创文章,未经博主允许不得转载。

转载地址:https://blog.csdn.net/py_123456/article/details/80289379 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:深入学习Hive应用场景及架构原理
下一篇:MapReduce 二次排序

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年03月14日 14时43分01秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

python 公众号引流_公众号引流方法有哪些? 2021-06-24
java 减少内存_java中减少内存占用小技巧 2021-06-24
centos 7 mysql图形界面_centos7-vnstat图形界面搭建 2021-06-24
java 防渗透_「java、工程师工作经验怎么写」-看准网 2021-06-24
java中跳出当前循环怎么做_在java中,如何跳出当前的多重循环? 2021-06-24
java程序中执行maven_java – 将一个enviornment变量传递给Maven中的已执行进程 2021-06-24
java16下载_java lombok下载 2021-06-24
python 图像处理与识别书籍_Python图像处理之识别图像中的文字(实例讲解) 2021-06-24
java安全初始化_java安全编码指南之:声明和初始化 2021-06-24
java jstat gc_分析JVM GC及内存情况的方法 2021-06-24
php pclzip.lib.php,php使用pclzip类实现文件压缩的方法(附pclzip类下载地址) 2021-06-24
php dns更新,php_mzdns: 站群,大量域名 通过 dns 服务商 api 批量添加 ip 工具。你懂的~ 基于 mzphp2 框架。... 2021-06-24
jdk 1.8 java.policy,JDK1.8 导致系统报错:java.security.InvalidKeyException:illegal Key Size 2021-06-24
php linux权限,Linux权限详细介绍 2021-06-24
典型环节的matlab仿真分析,典型环节的MATLAB仿真.doc 2021-06-24
Php contenttype类型,各种类型文件的Content Type 2021-06-24
php使用redis持久化,redis如何持久化 2021-06-24
php7.1解压包安装,【Swoole】php7.1安装swoole扩展 2021-06-24
linux centos删除安装的包,CentOS yum认为已删除的软件包仍在安装中 2021-06-24
酒店管理系统c语言带注释,酒店管理系统--C语言版.pdf 2021-06-24