摘要:本文主要介绍了大数据开发入门到精通之大数据Hive:hive中的join函数,通过具体的内容向大家展现,希望对大家大数据Hive的学习有所帮助。
本文主要介绍了大数据开发入门到精通之大数据Hive:hive中的join函数,通过具体的内容向大家展现,希望对大家大数据Hive的学习有所帮助。
1.hive中join与mysql中join的异同
mysql中没有left semi join,full join ,但是hive中有;
其他join,left join right join 两者都有;
2.下面重点讲解full join,left semi join
2.1 full join 与 union比较
full join 使用on条件时,select * 相当于把两个表(左表有m列p行和右表有n列q行)的所有列拼接成了一个有m+n列的结果表。
select * from table1 full join table2 on(table1.student_no=table2.student_no);
2)union 相当于把两个查询结果(左查询结果表有m列p行和右查询结果表有n列q行)的所有行进行了拼接,形成具有p+q行的查询结果。
select student_no tb1_student_no,student_name from table1 union select student_no as tb2_student_no,class_no from table2;
备注:union要求两个表的列数相同(要查取的列数)且列的数据类型相同;
3)union和union all的区别
Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序。
Union All:对两个结果集进行并集操作,包括重复行,不进行排序。
2.2 left semi join
测试子查询:在Hive 2.1.1版本中,是支持where子句中的子查询 in 和 not in;
SELECT table1.student_no, table1.student_name FROM table1 WHERE table1.student_no in (SELECT table2.student_no FROM table2);
SELECT table1.student_no, table1.student_name FROM table1 LEFT SEMI JOIN table2 on ( table1.student_no =table2.student_no);
上面两个语句查询结果相同;
此外,需要注意以下几项:
1、left semi join 的限制是, JOIN 子句中右边的表只能在 ON 子句中设置过滤条件,在 WHERE 子句、SELECT 子句或其他地方过滤都不行。
对右表的过滤条件只能写在on子句中:
hive> SELECT * FROM table1 LEFT SEMI JOIN table2 on ( table1.student_no =table2.student_no and table2.student_no>3);
3.(Inner) join,left (outer) join,right (outer) join
3.1:测试内连接Inner join等价于join
select * from table1 join table2 on table1.student_no=table2.student_no;
select * from table1 join table2 on table1.student_no=table2.student_no where table2.student_no is not null ;
3.2:left (outer) join ,在两张表进行连接查询时,会返回左表所有的行,即使在右表中没有匹配的记录。
select * from table1 left join table2 on(table1.student_no=table2.student_no);
3.3:测试左表独有
select * from table1 left outer join table2 on table1.student_no=table2.student_no where table2.student_no is null;
3.4:测试right (outer) join 在两张表进行连接查询时,会返回右表所有的行,即使在左表中没有匹配的记录
select * from table1 right join table2 on(table1.student_no=table2.student_no);
3.5:测试右表独有
select * from table1 right join table2 on(table1.student_no=table2.student_no) where table1.student_no is null;
3.6:full join,在两张表进行连接查询时,返回左表和右表中所有没有匹配的行。
select * from table1 full join table2 on(table1.student_no=table2.student_no);
3.7:并集去交集
select * from table1 full join table2 on table1.student_no=table2.student_no where table2.student_no is null or table1.student_no is null ;
我是小职,记得找我
✅ 解锁高薪工作
✅ 免费获取基础课程·答疑解惑·职业测评
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号