hadoop中的表连接

von4xj4u  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(428)

我是hadoop新手,正在编写我的第一个程序来连接mapreduce中的以下两个表。
第一张table:

  1. 11111 John
  2. 22222 Robert
  3. 33333 Stephan
  4. 44444 Peter
  5. 55555 Andersen

第二张table:

  1. 11111 Washington EEE 2011
  2. 22222 Jacksonville EIE 2010
  3. 33333 Minneapolis ECE 2012
  4. 44444 Cheyenne CSE 2013
  5. 55555 Detroit IT 2014

我已经上传了上述两个文本文件到hdfs使用色调。每列之间都有一个制表位。
运行代码后,我得到一个意外的输出,如下所示:

  1. 11111 John Washington EEE 2011
  2. 22222 Jacksonville EIE 2010 Robert
  3. 33333 Stephan Minneapolis ECE 2012
  4. 44444 Cheyenne CSE 2013 Peter
  5. 55555 Andersen Detroit IT 2014

我不知道我的代码出了什么问题。以下是我的java代码:
驱动器类:

  1. public class DriverClass extends Configured{
  2. public static void main (String args[]) throws IOException, ClassNotFoundException, InterruptedException{
  3. Job job = new Job();
  4. job.setJarByClass(DriverClass.class);
  5. MultipleInputs.addInputPath(job, new Path(args[0]), TextInputFormat.class, MapperClassOne.class);
  6. MultipleInputs.addInputPath(job, new Path(args[1]), TextInputFormat.class, MapperClassTwo.class);
  7. FileOutputFormat.setOutputPath(job, new Path(args[2]));
  8. job.setReducerClass(ReducerClass.class);
  9. job.setMapOutputKeyClass(Text.class);
  10. job.setMapOutputValueClass(Text.class);
  11. job.setOutputKeyClass(Text.class);
  12. job.setOutputValueClass(Text.class);
  13. System.exit(job.waitForCompletion(true)? 0 : -1);
  14. }
  15. }

我的第一个数据集(第一个表)的mapperclass-mapperclassone:

  1. public class MapperClassOne extends Mapper<LongWritable, Text, Text, Text>{
  2. public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
  3. String[] line = value.toString().split("\t");
  4. context.write(new Text(line[0]), new Text(line[1]));
  5. }
  6. }

我的第二个数据集(第二个表)的mapperclass-MapPerClass2:

  1. public class MapperClassTwo extends Mapper<LongWritable, Text, Text, Text>{
  2. public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
  3. String[] line = value.toString().split("\t");
  4. String temp = "";
  5. for(int i=1; i<line.length; i++){
  6. temp += line[i] + "\t";
  7. }
  8. context.write(new Text(line[0]), new Text(temp));
  9. }
  10. }

减速器等级:

  1. public class ReducerClass extends Reducer<Text, Text, Text, Text>{
  2. public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException{
  3. Iterator<Text> iter = values.iterator();
  4. String temp = "";
  5. while(iter.hasNext()){
  6. temp += iter.next().toString() + "\t";
  7. }
  8. context.write(key, new Text(temp));
  9. }
  10. }

请帮助我,如果有更好的方法来执行表联接,也请建议我。

rslzwgfq

rslzwgfq1#

在reducer中,除非实现二次排序,否则不会对键的值进行排序。在当前的实现中,键的值可能以任意顺序出现。您需要向Map器值添加标识符,以标识reducer中键的值源。
请参阅:http://kickstarthadoop.blogspot.com/2011/09/joins-with-plain-map-reduce.htmlhttp://www.lichun.cc/blog/2012/05/hadoop-genericwritable-sample-usage/

相关问题