hive 动态分区与混合分区写入

hive分区测试:

hive分区建立,静态、动态、混合分区,在混合中静态分区要在动态分区之前。

删除时间段分区

在partition()里面逻辑符用逗号, 比如时间段分区

-- 批量删除分区数据

alter table tmp_test.tmptable drop partition(dt>='20191101',dt<='20191130')

分区数据写入

创建一个分区测试表

use tmp_test;
drop table if exists tmp_test.cs_partitionTable_20190813;
CREATE EXTERNAL TABLE IF NOT EXISTS tmp_test.cs_partitionTable_20190813
(id int
,name string
)
partitioned by (pid1 int,pid2 int)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\u001'
STORED AS INPUTFORMAT
'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/data/test/tmp/cs_partitionTable_20190813';

待写入数据表

use tmp_test;
drop table if exists tmp_test.cs_partitiondTable_insertdata_20190813;
CREATE EXTERNAL TABLE IF NOT EXISTS tmp_test.cs_partitiondTable_insertdata_20190813
(id int
,name string
,pid1 int
,pid2 int
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\u001'
STORED AS INPUTFORMAT
'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/data/test/tmp/cs_partitiondTable_insertdata_20190813';

 写入数据:

insert overwrite table tmp_test.cs_partitiondTable_insertdata_20190813
select 1 as id,'香港' as name,1 as pid1,1 as pid2
union all 
select 2 as id,'台湾' as name,2 as pid1,2 as pid2
union all 
select 3 as id,'澳门' as name,3 as pid1,3 as pid2
union all 
select 4 as id,'大陆' as name,2 as pid1,4 as pid2
  • 静态分区

插入语句:
from tmp_test.cs_partitiondTable_insertdata_20190813
insert overwrite table tmp_test.cs_partitionTable_20190813 
    partition (pid1 = 1,pid2 = 1)
select id,name where pid1 = 1 and pid2 = 1 

insert overwrite table tmp_test.cs_partitionTable_20190813 
    partition (pid1 = 2,pid2 = 2)
select id,name where pid1 = 2 and pid2 = 2 ;
  • 动态分区

动态分区功能默认情况下没有开启。需要配置参数设定,默认严格模式执行情况下需要至少一列分区字段是静态的。

 

插入语句:
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nostrict;
set hive.exec.max.dynamic.partitions.pernode=1000;

insert overwrite table tmp_test.cs_partitionTable_20190813 
    partition (pid1,pid2)
select id,name,pid1,pid2 
from tmp_test.cs_partitiondTable_insertdata_20190813
结果:
show partitions tmp_test.cs_partitionTable_20190813

  • 混合分区

在动态分区的基础上前置一个静态分区,插入语句:

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nostrict;
set hive.exec.max.dynamic.partitions.pernode=1000;

insert overwrite table tmp_ihotel.cs_partitionTable_20190813 
    partition (pid1=1,pid2)
select id,name,pid1,pid2 
from tmp_ihotel.cs_partitiondTable_insertdata_20190813
where pid1 = 1

 关于分区,静态分区,动态分区,混合分区基本写法一直,主要是需要设置dynamic参数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值