Hive_数据模型_分区

目录

0 参考列表

1 概念

2 语法

2.1 DDL

2.1.1 创建分区表

2.1.2 添加分区

2.1.3 删除

2.2 DML

2.2.1 插入数据

2.2.2 修改

2.2.3 查询

3 参数


0 参考列表

阿里云:分区和列操作https://help.aliyun.com/document_detail/73771.html

1 概念

        分区表是指拥有分区空间的表,即在创建表时指定表内的一个或者某几个字段作为分区列.分区表实际就是对应分布式文件系统上的独立的文件夹,一个分区对应一个文件夹,文件夹下是对应分区所有的数据文件.

2 语法

2.1 DDL

2.1.1 创建分区表

create table table_name (
    column_name data_type comment '注释'
    ...
)
partitioned by (partition_key data_type comment '注释', ...)
;

2.1.2 添加分区

alter table table_name add [if not exists] 
partition (partition_col1 = partition_col_value1, ...) 
partition ...
;

2.1.3 删除

(1) 清空分区

-- 1 清空指定分区的数据
truncate table table_name partition(...),...;

-- 2 根据条件清空多个分区的数据
truncate table table_name partition(partition_filtercondition);

(2) 删除分区

-- 1 删除分区
alter table table_name drop [if exists] 
partition(partition_col1 = partition_col_value1, ...)
,partition...;

-- 2 根据条件删除分区
-- 支持多个分区过滤表达式,表达式之间是or的关系
alter table table_name drop [if exists] partition(分区条件,...),partition(分区条件);

2.2 DML

2.2.1 插入数据

2.2.1.1 静态分区

(1) insert into(向分区中追加数条数据,无论分区是否存在)

-- 1 插入数据,可指定列
insert into table <table_name> 
partition (<pt_spec>) [(col_name ,col_name...)]
values (value1,value2,...valueN)
,(...)
;

-- 2 查询插入数据
insert into table <table_name> 
partition (<pt_spec>) [(col_name,col_name...)]
select col_name... from 旧表名;

(2) insert overwrite(覆写数据)

insert overwrite table <table_name> 
partition (<pt_spec>)
select col_name... from 旧表名;

2.2.1.2 动态分区

insert {into|overwrite} table table_name
partition (<pt_spec>)
select 
     col_name
     ... 
    ,分区列
from 旧表名
;

2.2.2 修改

(1) 修改分区值

alter table table_name partition (partition_col1 = partition_col_value1, ...) 
rename to partition (partition_col1 = partition_col_value2, ...);

2.2.3 查询

(1) 查询分区

show partitions table_name;

(2) 查看分区信息

describe extended table_name partition(分区名 = 分区值);

(3) 条件查询

select
		col_name...
from table_name
where 分区列的条件 
		and [条件]
;

3 参数

        ①hive.exec.dynamic.partition = true:开启动态分区;

        ②hive.exec.dynamic.partition.mode = nonstrict:默认strict表示必须指定至少一个分区为静态分区,nonstrict表示允许所有的分区字段都可以使用动态分区;

        ③hive.exec.max.dynamic.partitions = 5000:设置最大分区数.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值