Hive學習筆記——保存select結果,Join,多重插入


1. 保存select查詢結果的幾種方式:

1、將查詢結果保存到一張新的hive表中

create table t_tmp
as
select * from t_p;

2、將查詢結果保存到一張已經存在的hive表中(用load的時候,可以是into或者overwrite into,這里是into或者overwrite)

insert into/overwrite table t_tmp
select * from t_p;

3、將查詢結果保存到指定的文件目錄(可以是本地,也可以是hdfs)

insert overwrite local directory '/home/hadoop/test'
select * from t_p;
insert overwrite directory '/aaa/test'
select * from t_p;

2. 關於hive中的各種join

准備數據
1,a
2,b
3,c
4,d
7,y
8,u

2,bb
3,cc
7,yy
9,pp

建表:

create table a(id int,name string)
row format delimited fields terminated by ',';

create table b(id int,name string)
row format delimited fields terminated by ',';

導入數據:

load data local inpath '/home/hadoop/a.txt' into table a;
load data local inpath '/home/hadoop/b.txt' into table b;

實驗:
** inner join

select * from a join b on a.id=b.id;

+-------+---------+-------+---------+--+
| a.id | a.name | b.id | b.name |
+-------+---------+-------+---------+--+
| 2 | b | 2 | bb |
| 3 | c | 3 | cc |
| 7 | y | 7 | yy |
+-------+---------+-------+---------+--+

**left join

select * from a left outer join b on a.id=b.id;

+-------+---------+-------+---------+--+
| a.id | a.name | b.id | b.name |
+-------+---------+-------+---------+--+
| 1 | a | NULL | NULL |
| 2 | b | 2 | bb |
| 3 | c | 3 | cc |
| 4 | d | NULL | NULL |
| 7 | y | 7 | yy |
| 8 | u | NULL | NULL |
+-------+---------+-------+---------+--+

**right join

select * from a right outer join b on a.id=b.id;

同上效果,只不過這次b的全部顯示,a的會有NULL。

**full join

select * from a full outer join b on a.id=b.id;

+-------+---------+-------+---------+--+
| a.id | a.name | b.id | b.name |
+-------+---------+-------+---------+--+
| 1 | a | NULL | NULL |
| 2 | b | 2 | bb |
| 3 | c | 3 | cc |
| 4 | d | NULL | NULL |
| 7 | y | 7 | yy |
| 8 | u | NULL | NULL |
| NULL | NULL | 9 | pp |
+-------+---------+-------+---------+--+

**left semi join

select * from a left semi join b on a.id = b.id;

+-------+---------+--+
| a.id | a.name |
+-------+---------+--+
| 2 | b |
| 3 | c |
| 7 | y |
+-------+---------+--+

3. 多重插入

from student
insert into table student_p partition(part='a')
select * where id<95011;
insert into table student_p partition(part='a')
select * where id<95011;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM