hadoop 处理不同的输入文件,文件关联

类型一: 一一对应

file1:

a  1
b  2
c  3


file2:

1 !
2 @
3 #

file1和file2进行关联,想要的结果:

a  !

b  @

3  #


思路:

1、标记不同输入文件

2、将file1的key、value颠倒 ;file1和file2的key相同,file1的value做key,file2的value做value ,输出。

程序:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package smiple;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.StringTokenizer;  
  5.   
  6. import org.apache.hadoop.conf.Configuration;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.LongWritable;  
  9. import org.apache.hadoop.io.Text;  
  10. import org.apache.hadoop.mapreduce.InputSplit;  
  11. import org.apache.hadoop.mapreduce.Job;  
  12. import org.apache.hadoop.mapreduce.Mapper;  
  13. import org.apache.hadoop.mapreduce.Reducer;  
  14. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  15. import org.apache.hadoop.mapreduce.lib.input.FileSplit;  
  16. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  17. import org.apache.hadoop.util.GenericOptionsParser;  
  18.   
  19. public class FileJoin {  
  20.   
  21.     public static class MyMap extends Mapper<LongWritable , Text, Text, Text> {  
  22.   
  23.         public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {  
  24. //          String line = value.toString();  
  25.             String line=new String(value.getBytes(),0,value.getLength(),"GBK");  
  26.             StringTokenizer tokenizer = new StringTokenizer(line);  
  27.             String keystr = tokenizer.nextToken();  
  28.             String valuestr = tokenizer.nextToken();  
  29.               
  30.             //获取文件名  
  31.             InputSplit inputSplit = context.getInputSplit();  
  32.             String fileName = ((FileSplit) inputSplit).getPath().getName();  
  33.               
  34.               
  35.             if("file1".equals(fileName)){//加标记  
  36.                 context.write(new Text(valuestr),new Text("file1_"+keystr));  
  37.             }else if("file2".equals(fileName)){  
  38.                 context.write(new Text(keystr), new Text("file2_"+valuestr));  
  39.             }  
  40.               
  41.         }  
  42.     }  
  43.   
  44.     public static class MyReduce extends Reducer<Text, Text, Text, Text> {  
  45.   
  46.         public void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException {  
  47.             Text resultKey = new Text("key0");  
  48.             Text resultValue = new Text("value0");  
  49.             for (Text val : values) {  
  50.                 if("file1_".equals(val.toString().substring(06))){  
  51.                     resultKey = new Text(val.toString().substring(6));  
  52.                 }else if("file2_".equals(val.toString().substring(06))){  
  53.                     resultValue = new Text(val.toString().substring(6));  
  54.                 }  
  55.             }  
  56.             System.out.println(resultKey.toString()+"   " + resultValue.toString());  
  57.             context.write(resultKey, resultValue);  
  58.         }  
  59.     }  
  60.   
  61.     public static void main(String[] args) throws Exception {  
  62.         Configuration conf = new Configuration();  
  63.         String[] ioArgs = new String[] { "hdfs://ip:port/mr/join/in","hdfs://ip:port/mr/join/out" };  
  64.         String[] otherArgs = new GenericOptionsParser(conf, ioArgs).getRemainingArgs();  
  65.   
  66.         if (otherArgs.length != 2) {  
  67.             System.err.println("Usage: Data Sort <in> <out>");  
  68.             System.exit(2);  
  69.         }  
  70.         Job job = new Job(conf, "file join ");  
  71.   
  72.         job.setJarByClass(Sort.class);  
  73.   
  74.         // 设置Map和Reduce处理类  
  75.         job.setMapperClass(MyMap.class);  
  76.         job.setReducerClass(MyReduce.class);  
  77.   
  78.         // 设置输出类型  
  79.         job.setOutputKeyClass(Text.class);  
  80.         job.setOutputValueClass(Text.class);  
  81.   
  82.         // 设置输入和输出目录  
  83.         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  84.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  85.   
  86.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  87.   
  88.     }  
  89.   
  90. }  

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值