Hapdoop的一个Mapreduce示例代码--统计单词个数
2014-04-29 17:06
561 查看
package com.mzsx.hadoop; import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; 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 MyWordCount { public static class MyMapper extends Mapper<Object, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1);// 类似于int类型 private Text word = new Text(); // 可以理解成String类型 public void map(Object key, Text value, Context context) throws IOException, InterruptedException { System.err.println(key + "," + value); // 默认情况下即根据空格分隔字符串 String tmp=value.toString(); tmp=tmp.replace('\'', ' '); tmp=tmp.replace('.', ' '); tmp=tmp.replace(',', ' '); tmp=tmp.replace(':', ' '); tmp=tmp.replace('!', ' '); tmp=tmp.replace(';', ' '); tmp=tmp.replace('?', ' '); tmp=tmp.replace('`', ' '); tmp=tmp.replace('"', ' '); tmp=tmp.replace('&', ' '); tmp=tmp.replace('(', ' '); tmp=tmp.replace(')', ' '); tmp=tmp.replace('-', ' '); StringTokenizer itr = new StringTokenizer(tmp); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } }; } // Reducer<KEYIN, VALUEIN, KEYOUT, VALUEOUT> public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { System.err.println(key + "," + values); int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); ; context.write(key, result);// 这是最后结果 }; } public static void main(String[] args) throws Exception { // 声明配置信息 Configuration conf = new Configuration(); // 声明Job Job job = new Job(conf, "Word Count"); // 设置工作类 job.setJarByClass(MyWordCount.class); // 设置mapper类 job.setMapperClass(MyMapper.class); // 可选 job.setCombinerClass(MyReducer.class); // 设置合并计算类 job.setReducerClass(MyReducer.class); // 设置key为String类型 job.setOutputKeyClass(Text.class); // 设置value为int类型 job.setOutputValueClass(IntWritable.class); //job.setInputFormatClass(KeyValueTextInputFormat.class); // 设置或是接收输入输出 FileInputFormat.setInputPaths(job, new Path("/user/root/aoman.txt")); FileOutputFormat.setOutputPath(job, new Path("/user/root/out")); // 执行 System.exit(job.waitForCompletion(true) ? 0 : 1); } }
相关文章推荐
- Hapdoop的一个Mapreduce示例代码--统计单词个数有排序功能
- 一个单词统计的实例,怎样通过MapReduce完成排序?
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- 一个单词统计的实例,怎样通过MapReduce完成排序? 推荐
- 【hadoop】 3002-mapreduce程序统计单词个数示例
- Hadoop示例程序之单词统计MapReduce
- 简单的单词个数统计的mapreduce 的代码实现
- Asp.net 2.0 自定义控件开发[开发一个图表(WebChart)控件(柱状图示例)](示例代码下载)
- Wordcount on YARN 一个MapReduce示例
- c#英文单词分类统计示例分享
- 第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。
- 脚本演示的天堂:共享一个可以提供代码示例和运行结果的非常不错的网站
- 针对一个文件 统计其中的单词和字符数 找出文件中出现次数最多的单词数 将文件中出现的单词按频率进行排序并输出
- 深入浅出学习Struts框架(一):一个简单mvc模式代码示例开始
- 演示事件(Event)怎样使用以及怎样为用户控件添加一个事件(示例代码下载)
- Asp.net 2.0 自定义控件开发[开发一个图表(WebChart)控件(柱状图示例)](示例代码下载)
- 一个非常简单的着色器示例代码
- 统计一个字符串中的单词的个数,并打印各个单词