2.Mapreduce实例——求平均值


Mapreduce实例——求平均值

实验步骤

1.开启Hadoop

 

2.新建mapreduce4目录

在Linux本地新建/data/mapreduce4目录

3. 上传文件到linux中

(自行生成文本文件,放到个人指定文件夹下)

goods_click

52127 5

52120 93

52092 93

52132 38

52006 462

52109 28

52109 43

52132 0

52132 34

52132 9

52132 30

52132 45

52132 24

52009 2615

52132 25

52090 13

52132 6

52136 0

52090 10

52024 347

4.在HDFS中新建目录

首先在HDFS上新建/mymapreduce4/in目录,然后将Linux本地/data/mapreduce4目录下的goods_click文件导入到HDFS的/mymapreduce4/in目录中。

5.新建Java Project项目

新建Java Project项目,项目名为mapreduce。

在mapreduce项目下新建包,包名为mapreduce2。

在mapreduce2包下新建类,类名为MyAverage。

6.添加项目所需依赖的jar包

右键项目,新建一个文件夹,命名为:hadoop2lib,用于存放项目所需的jar包。

将/data/mapreduce2目录下,hadoop2lib目录中的jar包,拷贝到eclipse中mapreduce2项目的hadoop2lib目录下。

hadoop2lib为自己从网上下载的,并不是通过实验教程里的命令下载的

选中所有项目hadoop2lib目录下所有jar包,并添加到Build Path中。

7.编写程序代码

MyAverage.java

package mapreduce2;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class MyAverage{
    
    public static class Map extends Mapper{
        private static Text newKey=new Text();
        public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
            String line=value.toString();
            System.out.println(line);
            String arr[]=line.split("\t");
            newKey.set(arr[0]);
            int click=Integer.parseInt(arr[1]);
            context.write(newKey, new IntWritable(click));
        }
    }
    public static class Reduce extends Reducer{
        public void reduce(Text key,Iterable values,Context context) throws IOException,InterruptedException{
            int num=0;
            int count=0;
            for(IntWritable val:values){
                num+=val.get();
                count++;
            }
            int avg=num/count;
            context.write(key,new IntWritable(avg));
        }
    }
    public static void main(String[] args)throws IOException,ClassNotFoundException,InterruptedException{
        Configuration conf=new Configuration();
        System.out.println("start");
        Job job =new Job(conf,"MyAverage");
        job.setJarByClass(MyAverage.class);
        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        Path in=new Path("hdfs://192.168.109.10:9000/mymapreduce4/in/goods_click");
        Path out=new Path("hdfs://192.168.109.10:9000/mymapreduce4/out");
        FileInputFormat.addInputPath(job,in);
        FileOutputFormat.setOutputPath(job,out);
        System.exit(job.waitForCompletion(true)?0:1);
    }
}

8.运行代码

在MyAverage类文件中,右键并点击=>Run As=>Run on Hadoop选项,将MapReduce任务提交到Hadoop中。

9.查看实验结果

待执行完毕后,进入命令模式下,在HDFS中/mymapreduce4/out查看实验结果。

hadoop fs -ls /mymapreduce4/out  

hadoop fs -cat /mymapreduce4/out/part-r-00000  

图一为我的运行结果,图二为实验结果

经过对比,发现结果一样

此处为浏览器截图