MR实现多表连接的原理和单表连接时一样的,甚至比单表连接还要简单。

在map阶段只需要根据文件的名称区分左表还是右表。使用关联的字段作为key2。

在reduce中对values中的值分别存储到一个左表list和右表list中。对左表list和右表list进行一个笛卡尔积完事。

 import java.io.*;
import java.util.*; import org.apache.hadoop.io.*;
import org.apache.hadoop.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.util.Tool;
public class MTjoin extends Configured implements Tool {
/*
* 多表链接,与单表链接思路类似。将关联列作为map的key值,用数字区分左表和右表。在Reduce阶段对两个表进行笛卡尔积
* */
public static class Map extends Mapper<LongWritable,Text,Text,Text>{
public void map(LongWritable key,Text value,Context context)throws IOException,InterruptedException{
String line=value.toString();
int linelen=line.length();
//去除文件首行
if(line.indexOf("factoryname")==-1&&line.indexOf("addressID")==-1)
{
//处理factory数据
if(line.charAt(linelen-2)==' ')
{
String facstr="1"+line.substring(0, linelen-2);
String addrestr=String.valueOf(line.charAt(linelen-1));
context.write(new Text(addrestr), new Text(facstr));
}else{
String addreidstr=String.valueOf(line.charAt(0));
String addrenastr="2"+line.substring(1);
context.write(new Text(addreidstr), new Text(addrenastr));
} }
} } public static class Reduce extends Reducer<Text,Text,Text,Text>{
public void reduce(Text key,Iterable<Text> values,Context context)throws IOException, InterruptedException{
ArrayList<String> facarr=new ArrayList<String>();
ArrayList<String> addarr=new ArrayList<String>();
for(Text var:values){
if(var.toString().charAt(0)=='1')
{
facarr.add(var.toString().substring(1));
}else if(var.toString().charAt(0)=='2')
{
addarr.add(var.toString().substring(1));
} }
if(facarr.size()!=0&&addarr.size()!=0)
{
for(int i=0;i<facarr.size();i++)
{
context.write(new Text(facarr.get(i)), new Text(addarr.get(0)));
} }
}
}
@Override
public int run(String[] args) throws Exception {
// TODO Auto-generated method stub
Configuration conf=new Configuration();
Job job=new Job(conf,"MTjoin");
job.setJarByClass(MTjoin.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class); job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); boolean success=job.waitForCompletion(true);
return success?0:1;
}
public static void main(String[] args)throws Exception{
int ret=ToolRunner.run(new MTjoin(), args);
System.exit(ret);
} }

Hadoop--Map/Reduce实现多表链接的更多相关文章

  1. Hadoop Map/Reduce教程

    原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/mapred_tutorial.html 目的 先决条件 概述 输入与输出 例子:WordCount v1.0 ...

  2. 一步一步跟我学习hadoop(5)----hadoop Map/Reduce教程(2)

    Map/Reduce用户界面 本节为用户採用框架要面对的各个环节提供了具体的描写叙述,旨在与帮助用户对实现.配置和调优进行具体的设置.然而,开发时候还是要相应着API进行相关操作. 首先我们须要了解M ...

  3. Hadoop Map/Reduce

    Hadoop Map/Reduce是一个使用简易的软件框架,基于它写出来的应用程序能够运行在由上千个商用机器组成的大型集群上,并以一种可靠容错的方式并行处理上T级别的数据集.一个Map/Reduce ...

  4. Hadoop Map/Reduce的工作流

    问题描述 我们的数据分析平台是单一的Map/Reduce过程,由于半年来不断地增加需求,导致了问题已经不是那么地简单,特别是在Reduce阶段,一些大对象会常驻内存.因此越来越顶不住压力了,当前内存问 ...

  5. Hadoop Map/Reduce 示例程序WordCount

    #进入hadoop安装目录 cd /usr/local/hadoop #创建示例文件:input #在里面输入以下内容: #Hello world, Bye world! vim input #在hd ...

  6. (转载)Hadoop map reduce 过程获取环境变量

    来源:http://www.linuxidc.com/Linux/2012-07/66337.htm   作者: lmc_wy Hadoop任务执行过程中,在每一个map节点或者reduce节点能获取 ...

  7. Hadoop map reduce 任务数量优化

    mapred.tasktracker.map.tasks.maximum 官方解释:The maximum number of map tasks that will be run  simultan ...

  8. hadoop2.2编程:自定义hadoop map/reduce输入文件切割InputFormat

    hadoop会对原始输入文件进行文件切割,然后把每个split传入mapper程序中进行处理,FileInputFormat是所有以文件作为数据源的InputFormat实现的基类,FileInput ...

  9. hadoop map reduce 实例wordcount的使用

    hadoop jar hadoop-mapreduce-examples-2.7.3.jar wordcount /wordcount.txt /wc/output3

随机推荐

  1. 深入了解relative

    1.relative是自身定位,距原本位置的偏移 2.无侵入布局: 挪动位置,原本位置还在占据,并不会影响其他元素的布局   应用: 实现鼠标拖拽,比自身api好用 3.top/bottom 和 le ...

  2. [JS]Cookie精通之路

    [JS]Cookie精通之路 转http://blog.163.com/neu_pdh1983/blog/static/572407020077310528915/ 发布:Cary 媒体:www.Ju ...

  3. .NET异步操作学习之一:Async/Await中异常的处理

    以前的异常处理,习惯了过程式的把出现的异常全部捕捉一遍,然后再进行处理.Async/Await关键字出来之后的确简化了异步编程,但也带来了一些问题.接下来自己将对这对关键字进行学习.然后把研究结果放在 ...

  4. 关于angular 自定义directive

    关于angular 自定义directive的小结 首先我们创建一个名为"expander"的自定义directive指令: angular.module("myApp& ...

  5. Checbox的操作含已选、未选及判断代码

    Checbox的操作包括已选.未选.判断等等,下面有个不错的示例,使用jquery完成,感兴趣的朋友可以参考下 $("#chk1").attr("checked" ...

  6. 转载的在DOS下操作mysql

    转载原文地址:http://www.server110.com/mysql/201309/1070.html 一.连接MYSQL. 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1 ...

  7. insert遭遇阻塞

    insert的阻塞确实不常见,今天碰到了一个,看书又了解一个,整理下.1.多个会话同时向unique字段插入相同的值session1:首先建测试表test,并在字段id上创建一个主键索引(唯一键也可以 ...

  8. iOS 改变UILabel部分颜色

    //协议 UILabel *xieLabel = [[UILabel alloc] init]; xieLabel.textColor = TextGrayColor; xieLabel.font = ...

  9. matplotlib 显示中文

    # --*-- coding: utf-8 --*-- from matplotlib.font_manager import FontProperties import matplotlib.pyp ...

  10. bzoj 2852: 强大的区间 辗转相除

    2852: 强大的区间 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 45  Solved: 12[Submit][Status][Discuss] D ...