八、手把手教MapReduce 单词统计案例编程

来源:互联网 发布:删除表数据的sql语句 编辑:程序博客网 时间:2024/05/17 07:33

1、在
Linux 系统中搭建Eclipse Maven 环境,创建Maven Project

      2、安装jdk,并配置环境变量。

      3、配置maven 配置环境变量,用root用户身份。

      4.配置Maven仓库

      5.解压eclipse


      6.以普通用户打开eclipse,配置maven





      修改pom.xml


      配置输出路径


      二、基于八古文格式编写WordCount 程序

      一个简单的MapReduce程序只需要指定map()reduce()inputoutput,剩下的事由框架完成。

      package org.apache.hadoop.mr01;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configured;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;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;import org.apache.hadoop.util.Tool;import org.apache.hadoop.util.ToolRunner;public class WordCount extends Configured implements Tool {//四个泛型,前两个是输入类型,后两个是输出类型。    public static class WordCountMapper extends Mapper<LongWritable,Text,Text,IntWritable> {        private final static IntWritable mapOutputValue=new IntWritable(1);        private Text mapOutputKey=new Text();        //每次读一行数据就调用一次该方法        @Override        public void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException{            String line=value.toString();            String[] words=line.split(" ");            for(String word:words){                mapOutputKey.set(word);                context.write(mapOutputKey, mapOutputValue);            }        }    }   public static class WordCountReducer extends Reducer<Text,IntWritable,Text,IntWritable>{    private IntWritable outputValue = new IntWritable() ;    @Override    public void reduce(Text key, Iterable<IntWritable> values,Context context)            throws IOException, InterruptedException {        int sum = 0 ;        //遍历value的list,累加求和        for(IntWritable value: values){            sum += value.get() ;        }        outputValue.set(sum);        //输出一个单词的统计结果        context.write(key, outputValue);    }}   public int run(String[] args) throws Exception {        //获取configuration      Configuration configuration = super.getConf() ;        // 创建job        Job job = Job.getInstance(configuration,this.getClass().getSimpleName());        job.setJarByClass(this.getClass());        //input        Path inPath = new Path(args[0]) ;        FileInputFormat.addInputPath(job, inPath);        // mapper        job.setMapperClass(WordCountMapper.class);        job.setMapOutputKeyClass(Text.class);        job.setMapOutputValueClass(IntWritable.class);        // reducer        job.setReducerClass(WordCountReducer.class);        job.setOutputKeyClass(Text.class);        job.setOutputValueClass(IntWritable.class);        //output        Path outPath = new Path(args[1]);        FileOutputFormat.setOutputPath(job, outPath);        //提交job         boolean isSuccess = job.waitForCompletion(true);        return isSuccess ? 0 : 1 ;    }   public static void main(String[] args) throws Exception {        Configuration configuration = new Configuration();        int status = ToolRunner.run(configuration,new WordCount(), args) ;        System.exit(status);    }}

      打包JAR,在YARN 运行测试


      三、以WordCount 程序为例,理解MapReduce 如何并行分析数


      默认一个分片split进行一个map处理,多个map经过中间的数据转换进入一个或多个reduce处理。

      1、input读入数据,按行读取,转换格式为<key,value>为map的输入文件,key是这行数据在文件中的偏移量。Value是这行数据的内容。
      2、在map中对这行数据进行split分割,形成map的输出文件类似为<word,1>。
      3、经过中间过程的转换,进入reduce中,reduce的输入格式<key,values>,类似为<hadoop,list(1,1,1,1)>。Reduce对value值进行累加。
      4、output输入数据,默认情况下每个<Key,Value>输出一行数据。
             key和value之间分隔符为制表符。




      0 0
      原创粉丝点击