Hive学习笔记——保存select结果,Join,多重插入

本文介绍了Hive SQL中保存查询结果的多种方法,包括创建新表、插入已存在表及导出到文件等。此外还详细讲解了不同类型的JOIN操作,并通过实例展示了每种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;

 

转载于:https://www.cnblogs.com/DarrenChan/p/6786182.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值