多表关联

多表关联和单表关联类似,它也是通过对原始数据进行一定的处理,从其中挖掘出关心的信息。下面进入这个实例。

1 实例描述

输入是两个文件,一个代表工厂表,包含工厂名列和地址编号列;另一个代表地址表,包含地址名列和地址编号列。要求从输入数据中找出工厂名地址名对应关系,输出"工厂名——地址名"表。

样例输入如下所示。

1)factory:

factoryname                    addressed

Beijing Red Star                    1

Shenzhen Thunder                3

Guangzhou Honda                2

Beijing Rising                       1

Guangzhou Development Bank      2

Tencent                        3

Back of Beijing                     1

2)address:

addressID    addressname

1            Beijing

2            Guangzhou

3            Shenzhen

4            Xian

样例输出如下所示。

factoryname                        addressname

Back of Beijing                          Beijing

Beijing Red Star                        Beijing

Beijing Rising                          Beijing

Guangzhou Development Bank          Guangzhou

Guangzhou Honda                    Guangzhou

Shenzhen Thunder                    Shenzhen

Tencent                            Shenzhen

2 设计思路

多表关联和单表关联相似,都类似于数据库中的自然连接。相比单表关联,多表关联的左右表和连接列更加清楚。所以可以采用和单表关联的相同处理方式,map识别出输入的行属于哪个表之后,对其进行分割,将连接的列值保存在key中,另一列和左右表标识保存在value中,然后输出。reduce拿到连接结果之后,解析value内容,根据标志将左右表内容分开存放,然后求笛卡尔积,最后直接输出。

这个实例的具体分析参考单表关联实例。下面给出代码。

 import java.io.IOException;
import java.lang.String;
import java.util.Iterator;
import java.util.StringTokenizer; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
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.output.FileOutputFormat; public class MTJoin {
public static int time = 0; public static class Map extends Mapper<Object, Text, Text, Text> { @Override
protected void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String relationType = new String();
if (line.contains("factoryname") == true
|| line.contains("addressID") == true) {
return;
} StringTokenizer itr = new StringTokenizer(line);
String mapkey = new String();
String mapvalue = new String(); String[] split = line.split(" "); if (split.length == 2 && split[1].charAt(0) >= '0'
&& split[1].charAt(0) <= '9') {
mapkey = split[1];
mapvalue = split[0];
relationType = "1";
}
if (split.length == 2 && split[0].charAt(0) >= '0'
&& split[0].charAt(0) <= '9') {
mapkey = split[0];
mapvalue = split[1];
relationType = "2";
} context.write(new Text(mapkey), new Text(relationType + "+"
+ mapvalue)); }
} public static class Reduce extends Reducer<Text, Text, Text, Text> { @Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
if (0 == time) {
context.write(new Text("factoryname"), new Text("addressname"));
time++;
} int factorynum = 0;
String[] factory = new String[10];
int addressnum = 0;
String[] address = new String[10]; for(Text value:values ){
if (0 == value.toString().length()) {
continue;
} char relationType = value.toString().charAt(0); // left
if ('1' == relationType) {
factory[factorynum] = value.toString().substring(2);
factorynum++;
}
// right
if ('2' == relationType) {
address[addressnum] = value.toString().substring(2);
addressnum++;
}
} if (0 != factorynum && 0 != addressnum) {
for (int m = 0; m < factorynum; m++) {
for (int n = 0; n < addressnum; n++) {
context.write(new Text(factory[m]),
new Text(address[n]));
}
}
}
} } public static void main(String[] args) throws Exception {
Job job = new Job();
job.setJobName("MTJoin");
job.setJarByClass(MTJoin.class); job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

MapReduce多表连接的更多相关文章

  1. MapReduce 多表连接

    题目描述: 现在有两个文件,1为存放公司名字和城市ID,2为存放城市ID和城市名 表一: factoryname,addressed Beijing Red Star,1 Shenzhen Thund ...

  2. Hadoop阅读笔记(三)——深入MapReduce排序和单表连接

    继上篇了解了使用MapReduce计算平均数以及去重后,我们再来一探MapReduce在排序以及单表关联上的处理方法.在MapReduce系列的第一篇就有说过,MapReduce不仅是一种分布式的计算 ...

  3. Hadoop-Map/Reduce之单表连接的实现

    MapReduce程序就是根据其特性对数据进行一个简单的逻辑处理,其中最为重要的一个特性就是根据key值将value值进行合并,其次就是在shuffle阶段有排序. 遇到一个MR程序就是要巧妙利用合并 ...

  4. SQL多表连接查询(详细实例)

    转载博客:joeleo博客(http://www.xker.com/page/e2012/0708/117368.html) 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:stud ...

  5. 关于Oracle表连接

    表连接注意left join on与where的区别: select * from dept; select * from emp; select * from emp a right outer j ...

  6. SQL多表连接查询

    SQL多表连接查询 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:student  截图如下: 表2:course  截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际 ...

  7. oracle(sql)基础篇系列(二)——多表连接查询、子查询、视图

        多表连接查询 内连接(inner join) 目的:将多张表中能通过链接谓词或者链接运算符连接起来的数据查询出来. 等值连接(join...on(...=...)) --选出雇员的名字和雇员所 ...

  8. Access数据库多表连接查询

    第一次在Access中写多表查询,就按照MS数据库中的写法,结果报语法错,原来Access的多表连接查询是不一样的 表A.B.C,A关联B,B关联C,均用ID键关联 一般写法:select * fro ...

  9. PostgreSQL-join多表连接查询和子查询

    一.多表连接查询 1.连接方式概览 [inner] join 内连接:表A和表B以元组为单位做一个笛卡尔积,记为表C,然后在C中挑选出满足符合on 语句后边的限制条件的内容. left [outer] ...

随机推荐

  1. 使用TortoiseGit对Git版本进行分支操作

    版本克隆分支问题 TortoiseGit在克隆分支的时候,默认克隆master分支,克隆后本地工作目录为中心器的Master分支. 克隆后本地分支 中心库分支 Push分支到中心服务器(Pushing ...

  2. Java HashMap 如何正确遍历并删除元素

    (一)HashMap的遍历 HashMap的遍历主要有两种方式: 第一种采用的是foreach模式,适用于不需要修改HashMap内元素的遍历,只需要获取元素的键/值的情况. HashMap<K ...

  3. 【转】使用BBB的device tree和cape(重新整理版)

    只要你想用BBB做哪怕一丁点涉及到硬件的东西,你就不可避免地要用到cape和device tree的知识.所以尽管它们看起来很陌生而且有点复杂,但还是得学.其实用起来不难的.下面我只讲使用时必须会的内 ...

  4. 分享一个字数限制和统计的UITextView分类方法

    - (NSUInteger)letterCountWithLimits:(NSInteger)limits { NSString *toBeString = self.text; NSUInteger ...

  5. mapping 详解5(dynamic mapping)

    概述 在使用 ES 的时,我们不需要事先定义好映射设置就可以直接向索引中导入文档.ES 可以自动实现每个字段的类型检测,并进行 mapping 设置,这个过程就叫动态映射(dynamic mappin ...

  6. 基于ubuntu和windows连接

    对于ubuntu和centos安装软件是不一样的 对于ubuntu是  apt-get install  +软件名字 但是对于centos是 yum install +软件名字 所以ubunu远程连接 ...

  7. .NET学习笔记(3) — VisualStudio使用总结

    目录 一:VS是什么? 二:VS可以创建什么类型的工程? 三:VS的常用功能? 四:VS都有哪些使用技巧? 五:注意事项 六:资源汇总   一:VS是什么? Microsoft Visual Stud ...

  8. dede-搜索

    <form name="formsearch" action="{dede:global.cfg_cmsurl/}/plus/search.php"> ...

  9. JAXB - Validate Document before It is Unmarshalled

    Validation A considerable part of the XML Schema language deals with facets, enabling the programmer ...

  10. JAXB - Annotations, The Annotation XmlElement

    The basic annotation for a field that's intended to be an element is XmlElement. It permits you to d ...