Hive的Join的文档说明地址:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual%2BJoins
以下为两个测试数据表建表语句:
use test; DROP TABLE IF EXISTS table1; create table table1( student_no bigint comment '学号', student_name string comment '姓名' ) COMMENT 'test 学生信息' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' STORED AS TEXTFILE; DROP TABLE IF EXISTS table2; create table table2( student_no bigint comment '学号', class_no bigint comment '课程号' ) COMMENT 'test 学生选课信息' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' STORED AS TEXTFILE; load data local inpath 'data_table1.txt' overwrite into table table1; load data local inpath 'data_table2.txt' overwrite into table table2;
测试数据为:
hive left join测试数据
语句:
select * from table1 left outer join table2 on(table1.student_no=table2.student_no);
结果:
FAILED: Parse Error: line 1:22 cannot recognize input near ‘left’ ‘join’ ‘table2′ in join type specifier
我用的HIVE版本是0.8,不支持直接的left join写法;
语句:
select * from table1 left outer join table2 on(table1.student_no=table2.student_no);
结果:
1 name1 1 11
1 name1 1 12
1 name1 1 13
2 name2 2 11
2 name2 2 14
3 name3 3 15
3 name3 3 12
4 name4 4 13
4 name4 4 12
5 name5 5 14
5 name5 5 16
6 name6 NULL NULL
可以看到left outer join左边表的数据都列出来了,如果右边表没有对应的列,则写成了NULL值。
同时注意到,如果左边的主键在右边找到了N条,那么结果也是会叉乘得到N条的,比如这里主键为1的显示了右边的3条。
语句:
select * from table1 left semi join table2 on(table1.student_no=table2.student_no);
结果:
1 name1
2 name2
3 name3
4 name4
5 name5
可以看到,只打印出了左边的表中的列,规律是如果主键在右边表中存在,则打印,否则过滤掉了。
结论:
本文地址:http://www.crazyant.net/1470.html
您可能也喜欢: | ||||
![]() Hive使用TRANSFORM运行Python脚本总结 |
![]() Hive开发中使用变量的两种方法 |
![]() HIVE的几个使用技巧 |
![]() Hive开发经验问答式总结 |
![]() hive从查询中获取数据插入到表或动态分区 |
无觅 |