【技术】WordCount
对wordcount例子程序分析
1)整个wordcount分为几个阶段?
三个
2)有哪几个阶段?
mapper
reducer
driver
3)每个阶段有什么作用
mapper:对数据进行打散<hello,1><mimi,1>
reducer:对数据进行聚合<hello,1+1+1>
driver:提交任务
4)详解
Mapper阶段:
将数据转换为String
对数据进行切分处理
把每个单词后加1
输出到reducer阶段
Reducer阶段:
根据key进行聚合
输出key出现总的次数
Driver阶段:
创建任务
关联使用的Mapper/Reducer类
指定mapper输出数据的kv类型
指定reducer输出的数据的kv类型
指定数据的输入路径与输出路径
提交
具体代码实现:
Mapper阶段:
package com.itstaredu.wordcount; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; /** * 数据的输入与输出以Key value进行传输 * KeyIN: LongWritable(long) 数据的起始偏移量 * valueIN:具体数据 Text * * mapper需要把数据传递到reducer阶段<hello, 1> * keyOut: 单词 Text * valueOut: 出现的次数 IntWritable */ public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> { // 对数据进行打散 @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // 1. 接入数据 String line = value.toString(); // 2. 对数据进行切分 String[] words = line.split(" "); // 3. 写出以<hello, 1> for (String w : words) { // 写出reducer端 context.write(new Text(w), new IntWritable(1)); } } }
Reducer阶段:
package com.itstaredu.wordcount; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; /** * reducer阶段接收的是Mapper输出的数据 * mapper的输出是reducer输入 * * keyIn: mapper输出的key的类型 * valueIn: mapper输出的value的类型 * * reducer端输出的数据类型,想要一个什么样的结果<hello, 1888> * keyOut: Text * valueOut: IntWritable */ public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
// key->单词 value->次数 protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
// 1. 记录出现的次数 int sum = 0; for (IntWritable v : values) { sum += v.get(); } // 2. 累加求和输出 context.write(key, new IntWritable(sum)); } }
Driver阶段:
package com.itstaredu.wordcount; 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; public class WordCountDriver { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { // 1. 创建job任务 Configuration conf = new Configuration(); Job job = Job.getInstance(conf); // 2. 指定jar包位置 job.setJarByClass(WordCountDriver.class); // 3. 关联使用的Mapper类 job.setMapperClass(WordCountMapper.class); // 4. 关联使用的Reducer类 job.setReducerClass(WordCountReducer.class); // 5. 设置mapper阶段输出的数据类型 job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); // 6. 设置reducer阶段输出的数据类型 job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); // 7. 设置数据输入的路径 FileInputFormat.setInputPaths(job, new Path(args[1])); // 8. 设置数据输出的路径 FileOutputFormat.setOutputPath(job, new Path(args[1])); // 9. 提交任务 boolean rs = job.waitForCompletion(true); System.exit(rs?0:1); } }
浙公网安备 33010602011771号