简单的wordcout的MapReduce学习实现
来源:互联网 发布:淘宝新开店铺旺旺采集 编辑:程序博客网 时间:2024/06/11 07:26
package com.hadoop.wordcount;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import com.hadoop.mapper.Mapper;import com.hadoop.reducer.Reducer;public class WordCount { public static void main(String[] args) throws IOException { //创建任务 Job job=Job.getInstance(new Configuration()); job.setJarByClass(WordCount.class); //Map job.setMapperClass(Mapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(LongWritable.class); FileInputFormat.setInputPaths(job, "/word.txt"); job.setReducerClass(Reducer.class); //Reduce job.setReducerClass(Reducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(LongWritable.class); FileOutputFormat.setOutputPath(job, new Path("/wcount")); }}
package com.hadoop.mapper;import java.io.IOException;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;public class Mapper extends org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, LongWritable>{ @Override protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException { String line=value.toString(); String[] words=line.split(" "); for(String w:words){ context.write(new Text(w), new LongWritable(1)); } }}
package com.hadoop.reducer;import java.io.IOException;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;public class Reducer extends org.apache.hadoop.mapreduce.Reducer<Text, LongWritable, Text, LongWritable>{ @Override protected void reduce(Text key, Iterable<LongWritable> value, org.apache.hadoop.mapreduce.Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException { long counter=0; for(LongWritable i:value){ counter+=i.get(); } context.write(key, new LongWritable(counter)); }}将上述工程打成jar包,在hadoop上运行: hadoop -jar 相应的包既可得到wordcount计算结果
0 0
- 简单的wordcout的MapReduce学习实现
- MapReduce实现简单的Wordcount
- MapReduce 的简单例子 WordCount的实现
- mapreduce实现简单的流量统计功能
- 学习运行简单的mapreduce计算
- mapreduce的简单应用
- MapReduce的简单讲解
- Hadoop-简单的MapReduce
- 简单的单词个数统计的mapreduce 的代码实现
- MapReduce简单实现基于物品的协同过滤算法
- mongoDB使用mapreduce实现简单的统计功能
- mapreduce实现搜索引擎简单的倒排索引
- 使用MapReduce实现简单的倒排索引
- Hadoop for .NET Developers(十二):实现简单的MapReduce作业
- PageRank的MapReduce实现
- 索引的mapreduce实现
- MapReduce的Erlang实现
- MapReduce模型的简单理解
- Codeforces 279B books
- 在PHP代码中取得当前日期时间再插入数据库
- UVa 10054 项链(欧拉路)
- git远程版本库
- 第十二节:Scanner类与String类介绍(讲师笔记)
- 简单的wordcout的MapReduce学习实现
- CODE[VS] 2273 USACO 扬帆远洋大战牧师妹酱 双端队列优化SPFA
- 6-简单选择结构C
- java内部类的应用场景
- 一个简单的IP地址输入控件
- UVALive 6680-动态规划dp
- MySQL排序内部原理探秘
- vs2015加载符号慢,请问怎么解决
- 第十二节:Scanner类与String类介绍(个人总结)