-
Input : 读取文本文件,按行处理;
-
Splitting : 将文件按照文件块(block)进行拆分,但是读取的时候还是一行行地读,此时得到的 K1偏移量,V1表示对应行的文本内容
-
Mapping : 并行将每一行按照空格进行拆分,拆分得到的 List(K2,V2),其中 K2 代表每一个单词,由于是做词频统计,所以 V2 的值为 1,代表出现 1 次;
-
Shuffling:即将map输出作为输入传给reducer称为shuffle,由于 Mapping 操作可能是在不同的机器上并行处理的,所以需要通过 shuffling 将相同 key 值的数据分发到一起合并(默认处理方式),这样才能统计出最终的结果,此时得到 K2 为每一个单词,List(V2) 为可迭代集合,V2 就是 Mapping 中的 V2;
-
Reducing : 这里的案例是统计单词出现的总次数,所以 Reducing 对 List(V2) 进行归约求和操作,最终输出。
Mapper程序编写:
继承org.apache.hadoop.mapreduce.Mapper类,实现map方法。
Reducer程序编写:
继承org.apache.hadoop.mapreduce.Reducer类,实现reduce方法
本地运行测试:
package com.hj.mr; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; 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.net.URI; /** * 使用MR统计HDFS上的文件对应的词频 * * 使用本地文件进行统计,然后统计结果输出到本地路径 */ public class WordCountLocalApp { public static void main(String[] args) throws Exception{ Configuration configuration = new Configuration(); // 创建一个Job Job job = Job.getInstance(configuration); // 设置Job对应的参数: 主类 job.setJarByClass(WordCountLocalApp.class); // 设置Job对应的参数: 设置自定义的Mapper和Reducer处理类 job.setMapperClass(WordCountMapper.class); job.setReducerClass(WordCountReducer.class); // 添加Combiner的设置即可 job.setCombinerClass(WordCountReducer.class); // 设置Job对应的参数: Mapper输出key和value的类型 job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); // 设置Job对应的参数: Reduce输出key和value的类型 job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); // 设置Job对应的参数: Mapper输出key和value的类型:作业输入和输出的路径 FileInputFormat.setInputPaths(job, new Path("input")); FileOutputFormat.setOutputPath(job, new Path("output")); // 提交job boolean result = job.waitForCompletion(true); System.exit(result ? 0 : -1); } }
HDFS运行测试:
-
package com.hj.mr; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; 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.net.URI; /** * 使用MR统计HDFS上的文件对应的词频 * * Driver: 配置Mapper,Reducer的相关属性 * * 提交到本地运行:开发过程中使用 */ public class WordCountHDFSApp { public static void main(String[] args) throws Exception{ System.setProperty("HADOOP_USER_NAME", "root"); Configuration configuration = new Configuration(); configuration.set("fs.defaultFS","hdfs://192.168.121.131:9000"); // 创建一个Job Job job = Job.getInstance(configuration); // 设置Job对应的参数: 主类 job.setJarByClass(WordCountHDFSApp.class); // 设置Job对应的参数: 设置自定义的Mapper和Reducer处理类 job.setMapperClass(WordCountMapper.class); job.setReducerClass(WordCountReducer.class); // 添加Combiner的设置即可 job.setCombinerClass(WordCountReducer.class); // 设置Job对应的参数: Mapper输出key和value的类型 job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); // 设置Job对应的参数: Reduce输出key和value的类型 job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); // 如果输出目录已经存在,则先删除 FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.121.131:9000"),configuration, "root"); Path outputPath = new Path("/wordcount/output"); if(fileSystem.exists(outputPath)) { fileSystem.delete(outputPath,true); } // 设置Job对应的参数: Mapper输出key和value的类型:作业输入和输出的路径 FileInputFormat.setInputPaths(job, new Path("/wordcount/input")); FileOutputFormat.setOutputPath(job, outputPath); // 提交job boolean result = job.waitForCompletion(true); System.exit(result ? 0 : -1); } }
day3入门案例——wordcount词频统计分析
最新推荐文章于 2024-08-10 17:13:44 发布