Hive学习:Hive连接JOIN用例详解

1 准备数据:

1.1 t_1

01  张三
02  李四
03  王五
04  马六
05  小七
06  二狗

1.2 t_2

01  11
03  33
04  44
06  66
07  77
08  88

1.3 t_3

01  男
02  男
03  女
04  男
05  女
06  女
07  男
08  X

2 创建表:t_1,t_2,t_3

create table if not exists t_1(id string,name string)row format delimited fields terminated by '\t';

create table if not exists t_2(id string,score string)row format delimited fields terminated by '\t';

create table if not exists t_3(id string,sex string)row format delimited fields terminated by '\t';

3 加载数据

load data local inpath '/root/tmp/t_1' into table t_1;
load data local inpath '/root/tmp/t_2' into table t_2;
load data local inpath '/root/tmp/t_3' into table t_3;

4 笛卡尔积:Join

select * from t_1 join t_2;
等价于:
select * from t_1,t_2;

1518378-20190104223248702-1687446924.png

5 等值连接:Join ... on(查交集)

select * from t_1 t1 join t_2 t2 on t1.id=t2.id;

1518378-20190104223306364-1905902447.png

图解原理:
1518378-20190104223323040-711609811.png

5 左连接: left join ... on ...

左连接是显示左边的表的所有数据,如果有右边表的数据与之对应,则显示;否则显示null

select * from t_1 t1 left join t_2 t2 on t1.id=t2.id;

1518378-20190104223333494-599403611.png

图解原理:
1518378-20190104223341870-1547850379.png

6 右连接: right join ... on ...

与左连接类似,右连接是显示右边的表的所有数据,如果有左边表的数据与之对应,则显示;否则显示null

select * from t_1 t1 right join t_2 t2 on t1.id=t2.id;

1518378-20190104223350149-2022917881.png

图解原理:
1518378-20190104223400316-306073091.png

7 全连接:full outer join ... on

相当于t_1和t_2的数据都显示,如果没有对应的数据,则显示Null.

select * from t_1 t1 full outer join t_2 t2 on t1.id=t2.id;

1518378-20190104223409224-465743177.png

图解原理:

1518378-20190104223418346-217552613.png

8 左半连接:semi join

semi join仅会显示t_1的数据,即左边表的数据。效率比左连接快,因为它会先拿到t_1的数据,然后在t_2中查找,只要查找到结果立马就返回t_1的数据。

select * from t_1 t1 left semi join t_2 t2 on t1.id=t2.id;

1518378-20190104223427940-1316432692.png

图解原理:

1518378-20190104223440126-1690697038.png

9 用单次MapReduce实现连接:

如果在连接中使用了公共键,Hive还支持通过一次MapReduce来连接多个表。

select t1.*,t3.sex,t2.score from t_1 t1 join t_3 t3 on t1.id=t3.id join t_2 t2 on t2.id=t1.id;

1518378-20190104223449121-1495137448.png

转载于:https://www.cnblogs.com/lingchen-liang/p/10222889.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值