MapReduce模板2

MapReduce模板,方便以后直接引用

  1. package jtlyuan.csdn.template;    
  2. import java.io.IOException;  
  3.   
  4. import org.apache.Hadoop.conf.Configuration;  
  5. import org.apache.hadoop.conf.Configured;  
  6. import org.apache.hadoop.fs.Path;  
  7. import org.apache.hadoop.io.LongWritable;  
  8. import org.apache.hadoop.io.Text;  
  9. import org.apache.hadoop.mapreduce.Job;  
  10. import org.apache.hadoop.mapreduce.Mapper;  
  11. import org.apache.hadoop.mapreduce.Reducer;  
  12. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  13. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  16. import org.apache.hadoop.util.Tool;  
  17. import org.apache.hadoop.util.ToolRunner;  
  18.   
  19.   
  20. public class MyJob extends Configured implements Tool{  
  21.     /*MapClass静态类*/  
  22.     public static class MapClass extends Mapper<LongWritable,Text,Text,Text>{  
  23.   
  24.   
  25.         public void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException{  
  26.             String[] citation = value.toString().split(",");  
  27.             context.write(new Text(citation[1]),new Text(citation[0]));  
  28.         }  
  29.     }  
  30.       
  31.     /*Reduce静态类*/  
  32.     public static class Reduce extends Reducer<Text,Text,Text,Text>{  
  33.         public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{  
  34.             String csv = "";  
  35.             for(Text val:values){  
  36.                 if(csv.length()>0){  
  37.                     csv += ",";  
  38.                 }  
  39.                 csv += val.toString();  
  40.             }  
  41.             context.write(key, new Text(csv));  
  42.         }  
  43.     }  
  44.       
  45.     /*driver驱动方法*/  
  46.     public int run(String[] args) throws Exception {  
  47.         Configuration conf = getConf();  
  48.         Job job = new Job(conf,"MyJobName");  
  49.         job.setJarByClass(MyJob.class);  
  50.           
  51.         FileInputFormat.setInputPaths(job, new Path(args[0]));  
  52.         FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  53.           
  54.         job.setMapperClass(MapClass.class);  
  55.         job.setCombinerClass(Reduce.class);  
  56.         job.setReducerClass(Reduce.class);  
  57.           
  58.         job.setInputFormatClass(TextInputFormat.class);  
  59.         job.setOutputFormatClass(TextOutputFormat.class);  
  60.         job.setOutputKeyClass(Text.class);  
  61.         job.setOutputValueClass(Text.class);  
  62.           
  63.         System.exit(job.waitForCompletion(true)?0:1);  
  64.   
  65.         return 0;  
  66.     }  
  67.       
  68.     /*主函数入口*/  
  69.     public static void main(String[] args) throws Exception {  
  70.         int res = ToolRunner.run(new Configuration(),new MyJob(),args);  
  71.           
  72.         System.exit(res);  
  73.     }  
  74.       
  75. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值