【Hive】(五)Hive 中动态分区与静态分区详解


Hive中的分区有两种:动态分区和静态分区

一、静态分区

静态分区
1、创建分区表

hive> create table stu2(
    > id int,
    > name string,
    > likes array<string>,
    > adress map<string,string>
    > )
    > partitioned by (age int,sex string)
    > row format delimited
    > fields terminated by ','
    > collection items terminated by '-'
    > map keys terminated by ':'
    > lines terminated by '\n';

2、加载数据到分区表

hive> load data local inpath '/opt/soft/stu.txt' into table stu2
    > partition (age=20,sex="male");
alter table stu2 add partition(age=10,sex='female'); 
// 

show partitions stu2;   // 查看分区

alter table stu2 drop partition(age=10,sex='female');

3、这种手动指定分区加载数据,就是常说的静态分区的使用。但是在日常工作中用的比较多的是动态分区。
静态分区是在创建表的时候就指定分区或者将表已经创建之后再指定分区(使用alter关键字)

二、动态分区

1、创建目标表

hive> create table stuo1(
    > id int,
    > name string,
    > age int,
    > gender string,
    > likes array<string>,
    > address map<string,string>
    > )
    > row format delimited
    > fields terminated by ','
    > collection items terminated by '-'
    > map keys terminated by ':'
    > lines terminated by '\n';
hive> create table stuo2(
    > id int,
    > name string,
    > likes array<string>,
    > address map<string,string>
    > )
    > partitioned by (age int,gender string)
    > row format delimited
    > fields terminated by ','
    > collection items terminated by '-'
    > map keys terminated by ':'
    > lines terminated by '\n';

2、采用动态方式加载数据到目标表
加载之前先设置一下下面的参数

hive> set hive.exec.dynamic.partition=true;
hive> set hive.exec.dynamic.partition.mode=nonstrict;

开始加载

hive> insert into table stuo2 partition(age,gender)
    > select id,name,likes,address,age,gender from stuo1;

上面加载数据方式并没有指定具体的分区,只是指出了分区字段。在select最后一个字段必须跟你的分区字段,这样就会自行根据(age,gender)的value来分区。
3、验证一下
在这里插入图片描述
在这里插入图片描述
创建动态分区表:

首先是要创建静态分区表;然后将表设置为非严格模式;再次创建动态分区表,并加载数据

加载数据的时候,是按照静态分区的模式,将数据加载到动态分区中去

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值