前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hadoop 处理不同的输入文件,文件关联

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

作者头像
星哥玩云
发布2022-06-30 21:05:35
6910
发布2022-06-30 21:05:35
举报
文章被收录于专栏:开源部署

类型一: 一一对应

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 ,输出。

程序:

package smiple;

import java.io.IOException; import java.util.StringTokenizer;

import org.apache.Hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.InputSplit; 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.input.FileSplit; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser;

public class FileJoin {

 public static class MyMap extends Mapper<LongWritable , Text, Text, Text> {

  public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException { //   String line = value.toString();    String line=new String(value.getBytes(),0,value.getLength(),"GBK");    StringTokenizer tokenizer = new StringTokenizer(line);    String keystr = tokenizer.nextToken();    String valuestr = tokenizer.nextToken();    //获取文件名    InputSplit inputSplit = context.getInputSplit();    String fileName = ((FileSplit) inputSplit).getPath().getName();    if("file1".equals(fileName)){//加标记     context.write(new Text(valuestr),new Text("file1_"+keystr));    }else if("file2".equals(fileName)){     context.write(new Text(keystr), new Text("file2_"+valuestr));    }   }  }

 public static class MyReduce extends Reducer<Text, Text, Text, Text> {

  public void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException {    Text resultKey = new Text("key0");    Text resultValue = new Text("value0");    for (Text val : values) {     if("file1_".equals(val.toString().substring(0, 6))){      resultKey = new Text(val.toString().substring(6));     }else if("file2_".equals(val.toString().substring(0, 6))){      resultValue = new Text(val.toString().substring(6));     }    }    System.out.println(resultKey.toString()+"  " + resultValue.toString());    context.write(resultKey, resultValue);   }  }

 public static void main(String[] args) throws Exception {   Configuration conf = new Configuration();   String[] ioArgs = new String[] { "hdfs://ip:port/mr/join/in","hdfs://ip:port/mr/join/out" };   String[] otherArgs = new GenericOptionsParser(conf, ioArgs).getRemainingArgs();

  if (otherArgs.length != 2) {    System.err.println("Usage: Data Sort <in> <out>");    System.exit(2);   }   Job job = new Job(conf, "file join ");

  job.setJarByClass(Sort.class);

  // 设置Map和Reduce处理类   job.setMapperClass(MyMap.class);   job.setReducerClass(MyReduce.class);

  // 设置输出类型   job.setOutputKeyClass(Text.class);   job.setOutputValueClass(Text.class);

  // 设置输入和输出目录   FileInputFormat.addInputPath(job, new Path(otherArgs[0]));   FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

  System.exit(job.waitForCompletion(true) ? 0 : 1);

 }

}

结果:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 结果:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档