博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hadoop分布式WordCount代码详解
阅读量:4094 次
发布时间:2019-05-25

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

         Wordcount 号称Hadoop中的HelloWord,花时间好好研究了以下程序的细节,这研究之前必须搞懂MapReduce工作原理,网上有很多讲解。其实MapReduce就是个分治的思想,将文件分在不同的从节点上进行处理(Map),然后排序合并,最后进行汇总的过程(Reduce)。

MapReduce的总体流程如下:

        研究代码的过程中Reduce程序段不太懂,查看了Hadoop官网,官网的解释还是最好的,以后对于不会的知识要学会在官网找答案。

下面送上官网链接:

Map和Reduce两主类对应的参数解释:

下面是代码(java)的详细解释:

package com.yootk.mr.demo;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.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;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.GenericOptionsParser;import org.apache.hadoop.io.Text;/** *  * it is a note. * @author  *需要在新建工程中提前配置(导入)hadoop文件中的jar包,/hadoop2.7.3-share-hadoop/common	hadoop2.7.3-share-hadoop/common/lib	hadoop2.7.3-share-hadoop/common/jdiff	hadoop2.7.3-share-hadoop/mapreduce	hadoop2.7.3-share-hadoop/mapreduce/lib * Data: 2017年5月23日下午3:21:05 */public class Wordcount {        //实现单词统计	//最为关键的Map部分与Reduce部分,需要用户自己来写		/**	 * 本操作主要是进行map的数据处理	 * 在Mapper的父类里面接收的内容如下:	 * Object:输入数据的具体内容;	 * Text:每行的文本数据;	 * Text:每个单词分解后的统计结果;	 * IntWritable:输出Map记录的结果;	 * 	 * it is a note.	 * @author 	 *	 * Data: 2017年5月23日下午3:22:57	 */	private static class WordcountMapper extends Mapper
{ @Override protected void map(Object key, Text value, Mapper
.Context context) throws IOException, InterruptedException { // 默认情况下是取得每行的数据,所以每行的数据里面都会存在有空格,按照空格进行拆分,每当出现一个单词就做一个统计的1 String lineContent = value.toString(); //取出每行的数据 String result[] = lineContent.split(" "); //进行每行数据的拆分 for(int x = 0;x
{ @Override protected void reduce(Text key, Iterable
values, Reducer
.Context context) throws IOException, InterruptedException { // TODO Auto-generated method stub int sum = 0 ; //保存每个单词出现的总次数 /** * 对于此处的foreach循环,官网上找到的解释,此时传入Reduce的数据已经是 *
的结构,所以Map函数循环对每个可以值进行处理, * 对每个key值的value list进行循环加和统计。 * 官网http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html * In this phase the reduce(WritableComparable, Iterable
, Context) * method is called for each
pair in the grouped inputs. */ for(IntWritable count : values){ sum += count.get(); } context.write(key, new IntWritable(sum)); } } public static void main(String[] args) throws Exception{ // 输入、输出路径由命令行参数给出,这里判断是否有两个参数 if(args.length != 2) { System.out.println("本程序需要两个参数,执行,hadoop jar yootk.jar /input/info.txt /output"); System.exit(1); } // 假设文件保存在HDFS /input/info.txt 中,且最终的输出结果也将保存在HDFS的 /output 目录中 Configuration conf = new Configuration(); //进行相关的配置使用 // 下面的函数对命令行传入的参数进行处理 String [] argArray = new GenericOptionsParser(conf, args).getRemainingArgs(); // 定义一个名为hadoop的作业 Job job = Job.getInstance(conf, "hadoop"); // 设置执行的jar文件程序类 job.setJarByClass(Wordcount.class); // Map处理 job.setMapperClass(WordcountMapper.class); // 指定Mapper的处理类 job.setMapOutputKeyClass(Text.class); // 设置输出的Key的类型 job.setMapOutputValueClass(IntWritable.class); // 设置输出的value的类型 // Reducer处理 job.setReducerClass(WordcountReducer.class); // 设置Reduce的处理类 job.setOutputKeyClass(Text.class); // 最终输出Ke信息,设置为文本 job.setOutputValueClass(IntWritable.class); // 最终输出Value内容设置为一个整型数据 //设置输入输出路径,命令行参数argArray[0] argArray[1] FileInputFormat.addInputPath(job, new Path(argArray[0])); FileOutputFormat.setOutputPath(job, new Path(argArray[1])); //等待执行完毕 System.exit(job.waitForCompletion(true) ? 0 : 1); // 执行完后退出 }}
你可能感兴趣的文章
Vue全家桶+Mint-Ui打造高仿QQMusic,搭配详细说明
查看>>
React Native应用部署/热更新-CodePush最新集成总结(新)
查看>>
react-native-wechat
查看>>
基于云信的react-native聊天系统
查看>>
网易云音乐移动客户端Vue.js
查看>>
ES7 await/async
查看>>
ES7的Async/Await
查看>>
React Native WebView组件实现的BarCode(条形码)、(QRCode)二维码
查看>>
每个人都能做的网易云音乐[vue全家桶]
查看>>
Immutable.js 以及在 react+redux 项目中的实践
查看>>
Vue2.0全家桶仿腾讯课堂(移动端)
查看>>
React+Redux系列教程
查看>>
19 个 JavaScript 常用的简写技术
查看>>
微信小程序:支付系列专辑(开发指南+精品Demo)
查看>>
iOS应用间相互跳转
查看>>
iOS开发之支付宝集成
查看>>
iOS开发 支付之银联支付集成
查看>>
iOS开发支付集成之微信支付
查看>>
浅谈JavaScript--声明提升
查看>>
React非嵌套组件通信
查看>>