zoukankan      html  css  js  c++  java
  • WordCount实验

    实验内容:

    现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1

    buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数据以“ ”分割,样本数据及格式如下:

    1. 买家id   商品id    收藏日期  
    2. 10181   1000481   2010-04-04 16:54:31  
    3. 20001   1001597   2010-04-07 15:07:52  
    4. 20001   1001560   2010-04-07 15:08:27  
    5. 20042   1001368   2010-04-08 08:20:30  
    6. 20067   1002061   2010-04-08 16:45:33  
    7. 20056   1003289   2010-04-12 10:50:55  
    8. 20056   1003290   2010-04-12 11:57:35  
    9. 20056   1003292   2010-04-12 12:05:29  
    10. 20054   1002420   2010-04-14 15:24:12  
    11. 20055   1001679   2010-04-14 19:46:04  
    12. 20054   1010675   2010-04-14 15:23:53  
    13. 20054   1002429   2010-04-14 17:52:45  
    14. 20076   1002427   2010-04-14 19:35:39  
    15. 20054   1003326   2010-04-20 12:54:44  
    16. 20056   1002420   2010-04-15 11:24:49  
    17. 20064   1002422   2010-04-15 11:35:54  
    18. 20056   1003066   2010-04-15 11:43:01  
    19. 20056   1003055   2010-04-15 11:43:06  
    20. 20056   1010183   2010-04-15 11:45:24  
    21. 20056   1002422   2010-04-15 11:45:49  
    22. 20056   1003100   2010-04-15 11:45:54  
    23. 20056   1003094   2010-04-15 11:45:57  
    24. 20056   1003064   2010-04-15 11:46:04  
    25. 20056   1010178   2010-04-15 16:15:20  
    26. 20076   1003101   2010-04-15 16:37:27  
    27. 20076   1003103   2010-04-15 16:37:05  
    28. 20076   1003100   2010-04-15 16:37:18  
    29. 20076   1003066   2010-04-15 16:37:31  
    30. 20054   1003103   2010-04-15 16:40:14  
    31. 20054   1003100   2010-04-15 16:40:16  

    要求编写MapReduce程序,统计每个买家收藏商品数量。

    统计结果数据如下:

    1. 买家id 商品数量  
    2. 10181   1  
    3. 20001   2  
    4. 20042   1  
    5. 20054   6  
    6. 20055   1  
    7. 20056   12  
    8. 20064   1  
    9. 20067   1  
    10. 20076   5 

    代码:

     1 package mapreduce;
     2 import java.io.IOException;  
     3 
     4 import java.util.StringTokenizer;  
     5 import org.apache.hadoop.fs.Path;  
     6 import org.apache.hadoop.io.IntWritable;  
     7 import org.apache.hadoop.io.Text;  
     8 import org.apache.hadoop.mapreduce.Job;  
     9 import org.apache.hadoop.mapreduce.Mapper;  
    10 import org.apache.hadoop.mapreduce.Reducer;  
    11 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
    12 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
    13 
    14 public class WordCount {
    15      public static class doMapper extends Mapper<Object, Text, Text, IntWritable>{  
    16             //第一个Object表示输入key的类型;第二个Text表示输入value的类型;第三个Text表示表示输出键的类型;第四个IntWritable表示输出值的类型  
    17             public static final IntWritable one = new IntWritable(1);  
    18                     public static Text word = new Text();  
    19                     @Override  
    20                     protected void map(Object key, Text value, Context context) throws IOException, InterruptedException  //抛出异常  
    21                     {  
    22                         StringTokenizer tokenizer = new StringTokenizer(value.toString(),"	");  
    23                       //StringTokenizer是Java工具包中的一个类,用于将字符串进行拆分  
    24                        word.set(tokenizer.nextToken());  
    25                              //返回当前位置到下一个分隔符之间的字符串  
    26                             context.write(word, one);  
    27                              //将word存到容器中,记一个数  
    28                       }
    29          }             
    30      public static class doReducer extends Reducer<Text, IntWritable, Text, IntWritable>{  
    31             //参数同Map一样,依次表示是输入键类型,输入值类型,输出键类型,输出值类型  
    32             private IntWritable result = new IntWritable();  
    33                     @Override  
    34                     protected void reduce(Text key, Iterable<IntWritable> values, Context context)  
    35                 throws IOException, InterruptedException {  
    36                 int sum = 0;  
    37                 for (IntWritable value : values) {  
    38                 sum += value.get();  
    39                 }  
    40                 //for循环遍历,将得到的values值累加  
    41                 result.set(sum);  
    42                 context.write(key, result);  
    43                 }  
    44                 } 
    45      public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {  
    46             Job job = Job.getInstance();  
    47             job.setJobName("WordCount");  
    48             job.setJarByClass(WordCount.class);  
    49             job.setMapperClass(doMapper.class);  
    50             job.setReducerClass(doReducer.class);  
    51             job.setOutputKeyClass(Text.class);  
    52             job.setOutputValueClass(IntWritable.class);  
    53             Path in = new Path("hdfs://localhost:9000/mymapreduce1/in");  
    54             Path out = new Path("hdfs://localhost:9000/mymapreduce1/out");  
    55             FileInputFormat.addInputPath(job, in);  
    56             FileOutputFormat.setOutputPath(job, out);  
    57             System.exit(job.waitForCompletion(true) ? 0 : 1);  
    58         }  
    59 }

    最终结果截图:

  • 相关阅读:
    进程的两种开启方法,进程的常用方法,进程的常用属性
    并发编程简介
    周鸿祎:互联网成功十大案例
    sed用法详解
    awk与sed:一个关于多行处理的例子
    igmpproxy源代码学习——igmpProxyInit()
    获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf
    unix网络编程——ioctl 函数的用法详解
    九大排序算法再总结
    浅谈《剑指offer》原题:不使用条件、循环语句求1+2+……+n
  • 原文地址:https://www.cnblogs.com/liyuchao/p/11767262.html
Copyright © 2011-2022 走看看