Hadoop Demo(一)【统计文件中单词出现的频率】
2017-10-17 21:26
766 查看
TokenizerMapper.java
package com.hadoop.demo1.word; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; import java.util.StringTokenizer; /** * @Description: Map类 */ public class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); @Override protected void map(Object key, Text value, Context context) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } }
IntSumReducer.java
package com.hadoop.demo1.word; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; /** * @Description: Reducer */ public class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); @Override protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable i : values) { sum += i.get(); } result.set(sum); context.write(key, result); } }
Main.java
package com.hadoop.demo1.word; 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.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import java.io.IOException; /** * @Description: 统计单词中出现的频率 */ public class Main { public static void main(String[] args) { Configuration conf = new Configuration(); try { Job job = Job.getInstance(conf, "Words"); job.setJarByClass(Main.class); job.setMapperClass(TokenizerMapper.class); job.setReducerClass(IntSumReducer.class); job.setCombinerClass(IntSumReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/demo/data/hadoop/input")); FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/demo/data/hadoop/output_words")); System.exit(job.waitForCompletion(true) ? 0 : 1); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
运行结果如图:
相关文章推荐
- Hadoop:使用原生python编写MapReduce来统计文本文件中所有单词出现的频率功能
- 统计文本文件中单词出现频率(用java集合框架编写)
- Hadoop 统计文件中某个单词出现的次数
- Hadoop 统计文件中某个单词出现的次数
- 统计英文文件中单词数和各单词出现的频率(次数)
- python---字符串的拼接、去重、反转、字母花样排序、单词出现判断、统计文件特定单词频率lambda、硬盘容量、列表转字符串
- 统计文本文件中单词出现频率(用java集合框架编写)
- 用hash表统计文本文件中每个单词出现的频率
- 针对一个文件 统计其中的单词和字符数 找出文件中出现次数最多的单词数 将文件中出现的单词按频率进行排序并输出
- Java编程:统计文本文件中单词出现频率
- 统计文件中各单词出现的频率(二叉排序树实现)
- 编写一个程序,统计给定文件中包含的每个单词出现的频率,并按单词表的顺序显示统计结果
- 统计文件中各单词出现的频率(Hash表实现)
- 统计文本文件中单词出现频率,自己编写的Java小程序
- Hadoop 统计文件中单词出现的次数
- 『转』统计一个日志文件里,单词出现频率的shell脚本
- hadoop-python——统计单词出现的频率
- 统计文件中单词出现的频率
- Linux下统计文本文件中前n个出现频率最高的单词
- 统计一篇文章中单词出现的频率(java 版)