Mapreduce实现WordCount

buyer_favorite文件如下:

10181	1000481	2010-04-04 16:54:31
20001	1001597	2010-04-07 15:07:52
20001	1001560	2010-04-07 15:08:27
20042	1001368	2010-04-08 08:20:30
20067	1002061	2010-04-08 16:45:33
20056	1003289	2010-04-12 10:50:55
20056	1003290	2010-04-12 11:57:35
20056	1003292	2010-04-12 12:05:29
20054	1002420	2010-04-14 15:24:12
20055	1001679	2010-04-14 19:46:04
20054	1010675	2010-04-14 15:23:53
20054	1002429	2010-04-14 17:52:45
20076	1002427	2010-04-14 19:35:39
20054	1003326	2010-04-20 12:54:44
20056	1002420	2010-04-15 11:24:49
20064	1002422	2010-04-15 11:35:54
20056	1003066	2010-04-15 11:43:01
20056	1003055	2010-04-15 11:43:06
20056	1010183	2010-04-15 11:45:24
20056	1002422	2010-04-15 11:45:49
20056	1003100	2010-04-15 11:45:54
20056	1003094	2010-04-15 11:45:57
20056	1003064	2010-04-15 11:46:04
20056	1010178	2010-04-15 16:15:20
20076	1003101	2010-04-15 16:37:27
20076	1003103	2010-04-15 16:37:05
20076	1003100	2010-04-15 16:37:18
20076	1003066	2010-04-15 16:37:31
20054	1003103	2010-04-15 16:40:14
20054	1003100	2010-04-15 16:40:16

第一列代表商品id,求商品id 出现的次数。
在这里插入图片描述
分析:

map()

在map函数里有三个参数,前面两个Object key,Text value就是输入的key和value,第三个参数Context context是可以记录输入的key和value。例如context.write(word,one);此外context还会记录map运算的状态。map阶段采用Hadoop的默认的作业输入方式,把输入的value用StringTokenizer()方法截取出的买家id字段设置为key,设置value为1,然后直接输出<key,value>。

reduce()

在map函数里有三个参数,前面两个Object key,Text value就是输入的key和value,第三个参数Context context是可以记录输入的key和value。例如context.write(word,one);此外context还会记录map运算的状态。map阶段采用Hadoop的默认的作业输入方式,把输入的value用StringTokenizer()方法截取出的买家id字段设置为key,设置value为1,然后直接输出<key,value>。

首先启动hadoop,然后将这个文件上传到hdfs上:

hadoop fs -mkdir -p /mymapreduce1/in
hadoop fs -put /buyer_favorite /mymapreduce1/in

代码

package mapreduce;
import java.io.IOException;
import java.util.StringTokenizer;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCount {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Job job = Job.getInstance();
        job.setJobName("WordCount");//Job的名字
        job.setJarByClass(WordCount.class);//
        job.setMapperClass(doMapper.class);
        job.setReducerClass(doReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        Path in = new Path("hdfs://localhost:9000/mymapreduce1/in/buyer_favorite1");//原本文件的位置
        Path out = new Path("hdfs://localhost:9000/mymapreduce1/out");//输出结果文件的位置
        FileInputFormat.addInputPath(job, in);
        FileOutputFormat.setOutputPath(job, out);
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
    public static class doMapper extends Mapper<Object, Text, Text, IntWritable>{
    	//第一个Object表示输入key的类型;第二个Text表示输入value的类型;第三个Text表示输出键的类型;第四个IntWritable表示输出值的类型  
        public static final IntWritable one = new IntWritable(1);//one 就是 1,是等会输出的value,不需要改变,所以设置为final
        public static Text word = new Text(); //等会输出的key
        @Override
        //重写map方法并抛出异常
        protected void map(Object key, Text value, Context context)
                    throws IOException, InterruptedException {
            StringTokenizer tokenizer = new StringTokenizer(value.toString(), "\t");//将输入的vlaue按照"\t" 分割
                word.set(tokenizer.nextToken());//将word 的内容设置为分割的字符串
                context.write(word, one);//输出map后的数据,key是word,value是1,如[100181, 1]....
        }
    }
    public static class doReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
        private IntWritable result = new IntWritable();//等会输出结果的value
        @Override
        //重写reduce并抛出异常
        protected void reduce(Text key, Iterable<IntWritable> values, Context context)
        throws IOException, InterruptedException {
        int sum = 0;//每个商品出现的次数
        for (IntWritable value : values) {
        	sum += value.get();//遍历map后传过来的values,并将值相同的value相加
        }
        result.set(sum);//设置result,其结果就是每种商品出现的次数之和
        context.write(key, result);//输出结果
        }
    }
}

结果

运行上面的程序
在Linux终端上查看:

hadoop fs -ls -R /mymapreduce1

在这里插入图片描述
查看内容

hadoop fs -ls /mymapreduce1/out
hadoop fs -cat /mymapreduce1/out/part-r-00000

可以看到:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值