注意:Hive中Join的关联键必须在on中指定,不能在Where中指定,否则就会先做笛卡尔积,再过滤。
一、测试数据
测试表
create table test_1223_1
(
mobile string
,score1 string
,score2 string
)
row format delimited fields terminated by ',' ;
数据
测试表
create table test_1223_2
(
mobile string
,score3 string
,score4 string
)
row format delimited fields terminated by ',' ;
数据
二、实验
内关联join
select *
from test_1223_1 a
join
test_1223_2 b
on a.mobile=b.mobile;
左关联left join
以LEFT JOIN关键字前面的表作为主表,和其他表进行关联,返回记录和主表的记录数一致,关联不上的字段置为NULL。
hive中,left join与left outer join等价
select *
from test_1223_1 a
left join
test_1223_2 b
on a.mobile=b.mobile;
右关联right join
和左外关联相反,以right join关键词后面的表作为主表,和前面的表做关联,返回记录数和主表一致,关联不上的字段为NULL。
select *
from test_1223_1 a
right join
test_1223_2 b
on a.mobile=b.mobile;
全外关联full [outer] join
以两个表的记录为基准,返回两个表的记录去重之和,关联不上的字段为NULL。
是否指定OUTER关键字,貌似对查询结果无影响。
注意:FULL JOIN时候,Hive不会使用MapJoin来优化。
select *
from test_1223_1 a
full outer join
test_1223_2 b
on a.mobile=b.mobile;
left semi join
以left semi join关键字前面的表为主表,返回主表的KEY也在副表中的记录。
select *
from test_1223_1 a
left semi join
test_1223_2 b
on a.mobile=b.mobile;
笛卡尔积关联(cross join)
返回两个表的笛卡尔积结果,不需要指定关联键。
select *
from test_1223_1 a
cross join
test_1223_2 b
;
如果指定on
select *
from test_1223_1 a
cross join
test_1223_2 b
on a.mobile=b.mobile;