MapReduce算法一、简单求和计数(类似WordCount)
2016-10-24 20:30
561 查看
package MRDemo; 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.io.LongWritable; import org.apache.hadoop.io.Text; 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; public class WordCount { public static void main(String[] args) throws Exception { //地址的个数为两个, if(args.length!=2){ System.err.println("please input full path!"); System.exit(0); } Job job=new Job(new Configuration(), "WordCount"); // 将wordCount类打包运行 job.setJarByClass(WordCount.class); // 设置输入路径 FileInputFormat.setInputPaths(job, new Path(args[0])); // 设置输出路径 FileOutputFormat.setOutputPath(job, new Path(args[1])); // 设置运行map函数的类 job.setMapperClass(WordCountMap.class); // 设置运行reduce函数的类 job.setReducerClass(WordCountReduce.class); // 设置map阶段输出的key、value的数据类型 job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); // 设置reduce阶段输出的key、value的数据类型 job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); // 提交运行job作业 job.waitForCompletion(true); } /** * KeyIn:每行文本偏移量的数据类型 * ValueIn:每行文本内容的数据类型 * KeyOut:Map输出的中间结果的key的数据类型 * ValueOut:Map输出的中间结果的value的数据类型 * * */ public static class WordCountMap extends Mapper<LongWritable, Text, Text, IntWritable>{ protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,IntWritable>.Context context) throws java.io.IOException ,InterruptedException { String[] lines = value.toString().split(" "); for (String info : lines) { // 写成<hello , 1 > <world ,1>这样的形式 context.write(new Text(info), new IntWritable(1)); } }; } /** * KeyIn:输入给Reduce处理的key的数据类型 * ValueIn:输入给Reduce处理的value的数据类型 * KeyOut:输出的最终结果的key的数据类型 * ValueOut:输出的最终结果的value的数据类型 * * *Map产生的中间结果,需要经过Shuffle阶段进行处理, *将相同key值的value放到同一个values集合中,处理之后变成以下形式: *<hello,{1,1}> <world,{1}> */ public static class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable>{ protected void reduce(Text key, java.lang.Iterable<IntWritable> values, org.apache.hadoop.mapreduce .Reducer<Text,IntWritable,Text,IntWritable>.Context context) throws java.io.IOException ,InterruptedException { int sum = 0; for (IntWritable count : values) { sum =sum+count.get();//将HDFS格式通过get方法转化为java格式, } context.write(key, new IntWritable(sum));//通过new 的方式把java格式转为java类型 }; } }
参考:唐老师资料
相关文章推荐
- Hadoop2.4.1 简单的wordCount的MapReduce程序
- hadoop学习(7)—— 使用yarn运行mapreduce一个简单的wordcount示例
- mapreduce中最简单的wordcount程序
- MapReduce之WordCount单词计数(下)
- hadoop hdfs搭建 mapreduce环境搭建 wordcount程序简单注释
- MapReduce之WordCount单词计数(上)
- Hadoop1.x 的MapReduce 简单例子WordCount
- Mapreduce和HBase新版本整合之WordCount计数案例
- 简单的MapReduce程序wordCount
- 如何编写最简单的MapReduce之WordCount程序
- MapReduce 的简单例子 WordCount的实现
- MapReduce简单入门 wordcount
- hadoop入门(六)JavaAPI+Mapreduce实例wordCount单词计数详解
- 【学习笔记】用Hadoop在MapReduce中WordCount简单程序运行详细流程
- MapReduce入门级之WordCount单词计数
- 【C语言】 计数、求和、求阶乘等简单算法
- 通过简单的Word Count讲解MapReduce原理以及Java实现
- MapReduce之WordCount单词计数
- Hadoop之道--MapReduce之Hello World实例wordcount
- 运行mapreduce实例wordcount时, 出现ClassNotFoundException: org.myorg.WordCount错误解决方法