1. 程式人生 > >mapreduce統計總數

mapreduce統計總數

現有某電商網站使用者對商品的收藏資料,記錄了使用者收藏的商品id以及收藏日期,名為buyer_favorite1

buyer_favorite1包含:買家id,商品id,收藏日期這三個欄位,資料以“\t”分割,樣本資料及格式如下:

 

買家id   商品id    收藏日期  
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  
View Code

 

 

要求編寫MapReduce程式,統計每個買家收藏商品數量。

原始碼:

 

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 class MyMapper extends Mapper<Object,Text,Text,IntWritable>{  
           private final static IntWritable one = new IntWritable(1);  
           private static String word = new String();  
           public void map(Object key, Text value, Context context) throws IOException,InterruptedException{  
                   StringTokenizer itr = new StringTokenizer(value.toString());  
                 
                   while (itr.hasMoreTokens()){  
                            
                           word=itr.nextToken();
                           System.out.println(word);
                           String id=word.substring(0,word.indexOf("   "));
                           Text word2=new Text();
                           word2.set(id);
                           context.write(word2,one);  
                   }  
                 
           }  
   }        

       public static class MyReducer extends Reducer<Text,IntWritable,Text,IntWritable>{  
           private IntWritable result = new IntWritable();  
           public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException,InterruptedException{  
                   int sum = 0;  
                   for (IntWritable val : values)  
                   {  
                           sum += val.get();  
                   }  
                   result.set(sum);  
                  
                   context.write(key,result);  
           }  
   }  
       
       public static void main(String[] args) throws Exception{  

         Job job = Job.getInstance();  
         job.setJobName("WordCount");
         job.setJarByClass(WordCount.class);  
         job.setMapperClass(MyMapper.class);  
         job.setReducerClass(MyReducer.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);  
 }  
    
}

 

統計資料:

10181    1
20001    2
20042    1
20054    6
20055    1
20056    12
20064    1
20067    1
20076    5
買家id    1