码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-04-29 22:15:10      阅读:751      评论:0      收藏:0      [点我收藏+]

标签:term   oop   color   table   int   where   direct   pat   rmi   

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;

 

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

标签:term   oop   color   table   int   where   direct   pat   rmi   

原文地址:http://www.cnblogs.com/DarrenChan/p/6786182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!