其他分享
首页 > 其他分享> > hive多分区表创建+数据导入

hive多分区表创建+数据导入

作者:互联网

步骤:建分区表、建临时表、临时表数据写入、临时表写入数据到分区表

 

1、分区参数介绍

   

2、分区表的创建

 

(1)分区表创建

 
create table test.user_list(
user_id  string,
msisdn   string,
imsi     string)
partitioned by (city string,sdate string)
row format delimited fields terminated by ',';
 

(2)临时表创建(辅助分区表测试)

 
create table test.user_list_tmp(
user_id  string,
msisdn   string,
imsi     string
city     string,
sdate    string)
row format delimited fields terminated by ',';
 

3、数据插入方式

 

(1)静态分区插入数据:

 
insert into table user_list partition(city='HANGZHOU',sdate='20200101')   
select 
    t.user_id,
    t.msisdn,
    t.imsi
from test.user_list_tmp t
 

(2)动静混合分区插入数据

 
set hive.exec.dynamic.partition=true;  
set hive.exec.dynamic.partition.mode=strick;
--set hive.exec.dynamic.partition.mode=nonstrick;
insert into table user_list partition(city='HANGZHOU',sdate='${sdate}')   
select 
    t.user_id,
    t.msisdn,
    t.imsi,
    t.sdate
from test.user_list_tmp t
 

注意:动静分区混合时,静态分区参数必须放置在前面。

 

(3)动态分区插入数据

 
set hive.exec.dynamic.partition=true;  
set hive.exec.dynamic.partition.mode=nonstrick;
insert into table user_list partition(city='${city}',sdate='${sdate}')   
select 
    t.user_id,
    t.msisdn,
    t.imsi,
    t.city,
    t.sdate
from test.user_list_tmp t
 

4、insert into 和 insert overwrite

 

insert into示例:

 
insert into table user_list partition(city='HANGZHOU',sdate='20200101')  
select 
    t.user_id,
    t.msisdn,
    t.imsi
from test.user_list_tmp t
 

insert overwrite示例:

 
insert overwrite table user_list partition(city='HANGZHOU',sdate='20200101')   
select 
    t.user_id,
    t.msisdn,
    t.imsi
from test.user_list_tmp t
 

两者区别:insert into直接追加到表中数据的尾部,而insert overwrite会重写数据,既先进行删除,再写入。

 

 

转自:http://blog.sina.com.cn/s/blog_155cf0b760102x1ij.html

标签:导入,insert,sdate,string,partition,list,hive,分区表,user
来源: https://www.cnblogs.com/51python/p/15318945.html