【外连接】是在【内连接】的基础上稍微修改即可。具体HQL语句详见Hive查询Join

package join.map;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List; 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.io.VLongWritable;
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; public class JoinOn { public static void main(String[] args) throws Exception { //临时配置windows的环境变量
System.setProperty("hadoop.home.dir", "D:\\workspace\\hadoop-2.2.0"); Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(JoinOn.class); job.setMapperClass(JOMapper.class);
job.setReducerClass(JOReducer.class); job.setMapOutputKeyClass(VLongWritable.class);
job.setMapOutputValueClass(Text.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); } public static class JOMapper extends Mapper<LongWritable, Text, VLongWritable, Text>{
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException { //获取当前分片所对应的文件名
String name = ((FileSplit)context.getInputSplit()).getPath().getName(); String[] splited = value.toString().split("\t"); if(name.endsWith("sales")){ //sales表
//<key,value> --> <id, things+':'+name+'\t'+id>
context.write(new VLongWritable(Long.parseLong(splited[1])), new Text(name+":"+value.toString()));
}else if(name.endsWith("things")) {

//<key,value> --> <id, sales+':'+id+'\t'+name>
context.write(new VLongWritable(Long.parseLong(splited[0])), new Text(name+":"+value.toString()));
}
}
} public static class JOReducer extends Reducer<VLongWritable, Text, Text, Text>{
@Override
protected void reduce(VLongWritable key, Iterable<Text> v2s, Context context)
throws IOException, InterruptedException { //分别存储sales和things两表的name
List<String> sales=new ArrayList<String>();
List<String> things=new ArrayList<String>(); for(Text text : v2s){
String[] splited = text.toString().split(":"); //sales表中的数据
if(splited[0].endsWith("sales")){ //加入集合
sales.add(splited[1]);
}
//things表中数据
else if(splited[0].endsWith("things")){
things.add(splited[1]);
}
}
//笛卡尔积
/**
* 左外连接:只要求左表中有数据即可
*/
if(sales.size()!=0 /*&& things.size()!=0*/){
for(String sale : sales){


//如果右表中没有数据,则使用 NULL 代替
if(things.size()==0){
context.write(new Text(sale), new Text("NULL"+"\t"+"NILL"));


}else {//如果右表中有数据,则直接输出
for(String thing : things){
context.write(new Text(sale), new Text(thing));
}
}
}
}
}
}
}

总结:

 1).左外连接:左表全部显示,右表不匹配的部分以NULL替代。

 2).代码实现即要求左表不为空即可,右表为空则以NULL输出,右表不为空则直接输出。

MR案例:外连接代码实现的更多相关文章

  1. 数据算法 --hadoop/spark数据处理技巧 --(3.左外连接 4.反转排序)

    三. 左外连接 考虑一家公司,比如亚马逊,它拥有超过2亿的用户,每天要完成数亿次交易.假设我们有两类数据,用户和交易: users(user_id,location_id) transactions( ...

  2. mysql自连接和外连接知识点及相关案例

    #三.自连接 #查询员工的名字.上级的名字 SELECT e.last_name, m.last_name FROM employees e JOIN employees m ON e.manager ...

  3. MR案例:Reduce-Join

    问题描述:两种类型输入文件:address(地址)和company(公司)进行一对多的关联查询,得到地址名(例如:Beijing)与公司名(例如:Beijing JD.Beijing Red Star ...

  4. SubSonic3.0使用外连接查询时查询不出数据的问题修改

    今天在开发时,要使用到外连接查询,如图 老是查不出数据,所以就追踪了一下代码,发现查询后生成的SQL语句变成了内连接了,真是晕 然后继续Debug,发现原来SqlQuery类在调用LeftInnerJ ...

  5. [原创]java WEB学习笔记91:Hibernate学习之路-- -HQL 迫切左外连接,左外连接,迫切内连接,内连接,关联级别运行时的检索策略 比较。理论,在于理解

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. 【SQL】SQL中笛卡尔积、内连接、外连接的数据演示

    SQL的查询语句中,常使用到内连接.外连接,以及连接的基础--笛卡尔积运算. 在简单的SQL中,也许我们还分辨清楚数据如何连接,一旦查询复杂了,脑子也犯浆糊了,迷迷糊糊的. 本文,简单以数据形式记录连 ...

  7. SQL连接方式(内连接,外连接,交叉连接)

    1.内连接.左连接.右连接.全连接介绍 內连接仅选出两张表中互相匹配的记录.因此,这会导致有时我们需要的记录没有包含进来.内部连接是两个表中都必须有连接字段的对应值的记录,数据才能检索出来.   左连 ...

  8. 使用Criteria 实现两表的左外连接,返回根对象

    (转) 引用 两个实体 Parent(P) 和 Child(C)之间是1:N的关系,现要求符合指定条件的P及所包 含的C 采用hibernate中的Criteria来实现此功能的代码如下: Java代 ...

  9. 数据库外连接及MySQL实现

    MySQL查询分为内连接查询和外连接查询,他们的区别在于:内连接查询的两个表示对等关系,根据条件进行匹配:外连接是以某一个表为主,两一个表根据条件进行关联.外连接分为左外连接.右外连接和全外连接.本文 ...

随机推荐

  1. python--生成测试数据

    1.方法 import datetime params_dict_data = { "system_id":"systemId001", "order ...

  2. Spring在Web应用中使用的原理

    那Spring如何在web应用中使用 ①加入Spring web应用的特定jar包spring-web-4.0.0.RELEASE.jar.spring-webmvc-4.0.0.RELEASE.ja ...

  3. ios开发 更改状态栏

    设置statusBar 简单来说,就是设置显示电池电量.时间.网络部分标示的颜色, 这里只能设置两种颜色: 默认的黑色(UIStatusBarStyleDefault) 白色(UIStatusBarS ...

  4. Spark源码分析 -- PairRDD

    和一般RDD最大的不同就是有两个泛型参数, [K, V]表示pair的概念 关键的function是, combineByKey, 所有pair相关操作的抽象 combine是这样的操作, Turns ...

  5. python的os模块命令

    https://www.cnblogs.com/weiyiming007/p/8493913.html

  6. 【chainer框架】【pytorch框架】

    教程: https://bennix.github.io/ https://bennix.github.io/blog/2017/12/14/chain_basic/ https://bennix.g ...

  7. scrollend,滚动结束执行一次

    var timer;window.onscroll = function () { clearTimeout(timer); timer = setTimeout(function () { aler ...

  8. 解决MySQL数据库同步1236错误

    1.报错如下: Got fatal error from master when reading data from binary log: 'The slave is connecting usin ...

  9. LayoutInflater的动态增加控件

    在实际开发中LayoutInflater这个类是非常有用的,它的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件. 而findView ...

  10. Oracle 学习笔记 12 -- 序列、索引、同义词

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/Topyuluo/article/details/24232449 数据库的对象包含:表.视图.序列. ...