zoukankan      html  css  js  c++  java
  • 第八周hadoop-MapReduce

    首先按照网上林子雨老师的教程把MapReduce做好

    然后再按照建民老师发的实验要求进行实验

    实验要求:对如下信息进行统计并写入out

    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  

    要求统计结果如下:

    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. import java.util.StringTokenizer;  
    4. import org.apache.hadoop.fs.Path;  
    5. import org.apache.hadoop.io.IntWritable;  
    6. import org.apache.hadoop.io.Text;  
    7. import org.apache.hadoop.mapreduce.Job;  
    8. import org.apache.hadoop.mapreduce.Mapper;  
    9. import org.apache.hadoop.mapreduce.Reducer;  
    10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
    11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
    12. public class WordCount {  
    13.     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {  
    14.         Job job = Job.getInstance();  
    15.         job.setJobName("WordCount");  
    16.         job.setJarByClass(WordCount.class);  
    17.         job.setMapperClass(doMapper.class);  
    18.         job.setReducerClass(doReducer.class);  
    19.         job.setOutputKeyClass(Text.class);  
    20.         job.setOutputValueClass(IntWritable.class);  
    21.         Path in = new Path("hdfs://localhost:9000/mymapreduce1/in/buyer_favorite1");  
    22.         Path out = new Path("hdfs://localhost:9000/mymapreduce1/out");  
    23.         FileInputFormat.addInputPath(job, in);  
    24.         FileOutputFormat.setOutputPath(job, out);  
    25.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
    26.     }  
    27.     public static class doMapper extends Mapper<Object, Text, Text, IntWritable>{  
    28.         public static final IntWritable one = new IntWritable(1);  
    29.         public static Text word = new Text();  
    30.         @Override  
    31.         protected void map(Object key, Text value, Context context)  
    32.                     throws IOException, InterruptedException {  
    33.             StringTokenizer tokenizer = new StringTokenizer(value.toString(), " ");  
    34.                 word.set(tokenizer.nextToken());  
    35.                 context.write(word, one);  
    36.         }  
    37.     }  
    38.     public static class doReducer extends Reducer<Text, IntWritable, Text, IntWritable>{  
    39.         private IntWritable result = new IntWritable();  
    40.         @Override  
    41.         protected void reduce(Text key, Iterable<IntWritable> values, Context context)  
    42.         throws IOException, InterruptedException {  
    43.         int sum = 0;  
    44.         for (IntWritable value : values) {  
    45.         sum += value.get();  
    46.         }  
    47.         result.set(sum);  
    48.         context.write(key, result);  
    49.         }  
    50.     }  

    当按照这个写下来时,程序运行后写入out的是这个:

    而这里面需要对在输出和计算上有一点点的修改,同时需要有空格。

    修改之后的代码明天再更emm

  • 相关阅读:
    Restful API设计规范
    git merge 和 git merge --no-ff
    [Machine Learning & Algorithm] 朴素贝叶斯算法(Naive Bayes)
    TF-IDF与余弦相似性的应用(三):自动摘要
    TF-IDF与余弦相似性的应用(二):找出相似文章
    TF-IDF与余弦相似性的应用(一):自动提取关键词
    LeetCode 442. Find All Duplicates in an Array
    LeetCode 29. Divide Two Integers
    LeetCode 402. Remove K Digits
    LeetCode 406. Queue Reconstruction by Height
  • 原文地址:https://www.cnblogs.com/mitang0-0/p/11768410.html
Copyright © 2011-2022 走看看