Spark 之 经典案例

转自:http://blog.csdn.net/kwu_ganymede/article/details/50474763


Hadoop经典案例Spark实现(二)——数据去重问题


1、原始数据
1)file1:

[plain]  view plain  copy
  1. 2012-3-1 a  
  2. 2012-3-2 b  
  3. 2012-3-3 c  
  4. 2012-3-4 d  
  5. 2012-3-5 a  
  6. 2012-3-6 b  
  7. 2012-3-7 c  
  8. 2012-3-3 c   

2)file2:
[plain]  view plain  copy
  1. 2012-3-1 b  
  2. 2012-3-2 a  
  3. 2012-3-3 b  
  4. 2012-3-4 d  
  5. 2012-3-5 a  
  6. 2012-3-6 c  
  7. 2012-3-7 d  
  8. 2012-3-3 c   


数据输出:
[plain]  view plain  copy
  1. 2012-3-1 a  
  2. 2012-3-1 b  
  3. 2012-3-2 a  
  4. 2012-3-2 b  
  5. 2012-3-3 b  
  6. 2012-3-3 c  
  7. 2012-3-4 d  
  8. 2012-3-5 a  
  9. 2012-3-6 b  
  10. 2012-3-6 c  
  11. 2012-3-7 c  
  12. 2012-3-7 d  


3)、说明
数据去重的最终目标是让原始数据中出现次数超过一次的数据在输出文件中只出现一次。我们自然而然会想到将同一个数据的所有记录都交给一台reduce机器,
无论这个数据出现多少次,只要在最终结果中输出一次就可以了。具体就是reduce的输入应该以数据作为key,

而对value-list则没有要求。当reduce接收到一个<key,value-list>时就直接将key复制到输出的key中,并将value设置成空值。


2、MapReduce实现

代码编写

[java]  view plain  copy
  1. import java.io.IOException;  
  2.   
  3. import org.apache.hadoop.conf.Configuration;  
  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. import org.apache.hadoop.util.GenericOptionsParser;  
  13.    
  14.   
  15. public class Dedup {  
  16.   
  17.     //map将输入中的value复制到输出数据的key上,并直接输出  
  18.   
  19.     public static class Map extends Mapper<Object,Text,Text,Text>{  
  20.   
  21.     private static Text line=new Text();//每行数据  
  22.   
  23.     //实现map函数  
  24.   
  25.     public void map(Object key,Text value,Context context)  
  26.   
  27.         throws IOException,InterruptedException{  
  28.   
  29.         line=value;  
  30.   
  31.         context.write(line, new Text(""));  
  32.   
  33.     }  
  34.    
  35.   
  36.     }  
  37.   
  38.      
  39.   
  40.     //reduce将输入中的key复制到输出数据的key上,并直接输出  
  41.   
  42.     public static class Reduce extends Reducer<Text,Text,Text,Text>{  
  43.   
  44.     //实现reduce函数  
  45.   
  46.     public void reduce(Text key,Iterable<Text> values,Context context)  
  47.   
  48.         throws IOException,InterruptedException{  
  49.   
  50.         context.write(key, new Text(""));  
  51.   
  52.     }  
  53.         
  54.     }  
  55.   
  56.      
  57.   
  58.     public static void main(String[] args) throws Exception{  
  59.   
  60.     Configuration conf = new Configuration();  
  61.   
  62.     //这句话很关键  
  63.   
  64.     conf.set("mapred.job.tracker""192.168.1.2:9001");  
  65.   
  66.          
  67.   
  68.     String[] ioArgs=new String[]{"dedup_in","dedup_out"};  
  69.   
  70.         String[] otherArgs = new GenericOptionsParser(conf, ioArgs).getRemainingArgs();  
  71.   
  72.         if (otherArgs.length != 2) {  
  73.   
  74.           System.err.println("Usage: Data Deduplication <in> <out>");  
  75.   
  76.           System.exit(2);  
  77.   
  78.         }  
  79.   
  80.        
  81.   
  82.      Job job = new Job(conf, "Data Deduplication");  
  83.   
  84.      job.setJarByClass(Dedup.class);  
  85.   
  86.        
  87.   
  88.      //设置Map、Combine和Reduce处理类  
  89.   
  90.      job.setMapperClass(Map.class);  
  91.   
  92.      job.setCombinerClass(Reduce.class);  
  93.   
  94.      job.setReducerClass(Reduce.class);  
  95.   
  96.        
  97.   
  98.      //设置输出类型  
  99.   
  100.      job.setOutputKeyClass(Text.class);  
  101.   
  102.      job.setOutputValueClass(Text.class);  
  103.   
  104.        
  105.   
  106.      //设置输入和输出目录  
  107.   
  108.      FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  109.   
  110.      FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  111.   
  112.      System.exit(job.waitForCompletion(true) ? 0 : 1);  
  113.   
  114.      }  
  115.   
  116. }   


3、Spark实现Scala版本

[java]  view plain  copy
  1. val two = sc.textFile("/tmp/spark/two")  
  2.   
  3. two.filter(_.trim().length>0).map(line=>(line.trim,"")).groupByKey().sortByKey().keys.collect.foreach(println _)  

上面通过groupByKey来去重,并sortByKey排序,因为hadoop的结果也是排序过的,验证结果是一样的,代码精简不少



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值