Hive分区表

1、为什么设立分区表

在大量数据需要查询时,虽然可以通过where进行筛选,但也是检索整个数据表后得到的结果。

而将一个大的数据集根据实际需要分割成各个小型数据集,再通过where选择需要查询的分区,

故而效率大大提高。

2、分区表实质

Hive中的分区是将一个文件分割成各个目录(文件)。

3、分区表语法

3.1建立分区表

3.1.1建立一级分区表

create table 分区表名称(

字段名称1 数据类型,

字段名称2 数据类型,

。。。

字段名称n 数据类型

partitioned by(分区字段名称1 数据类型)

row format delimited

fields terminated by '分割符';

collection items terminated by '分割符'

map keys terminated by '分割符'

lines terminated by '分割符'

create table song1(
id int,
name string,
num int
)
partitioned by (month string)
row format delimited fields terminated by '\t';

3.1.2建立二级分区表

create table 分区表名称(

字段名称1 数据类型,

字段名称2 数据类型,

。。。

字段名称n 数据类型

partitioned by(分区字段名称1 数据类型,分区字段名称2 数据类型)

row format delimited

fields terminated by '分割符';

create table singpartition(
id int, name string, num int
)
partitioned by (month string, day string)
row format delimited fields terminated by '\t';

3.2加载数据到分区表

3.2.1加载数据到一级分区表

load data local inpath '本地路径文件' into table 分区表名 partition(分区字段名称1=字段值)

hive> load data local inpath '/opt/songs.txt' into table song1 partition(month=20220108);
hive> load data local inpath '/opt/songs2.txt' into table song1 partition(month=20220109);
hive> load data local inpath '/opt/songs3.txt' into table song1 partition(month=20220110);

 3.2.1加载数据到二级分区表

load data local inpath '本地路径文件' into table 分区表名 partition(分区字段名称1=字段值,分区字段名称2=字段值)

load data local inpath '/opt/songs.txt' into table
 singpartition partition(month='20220111',day=13);

3.3分区的增删改查

3.3.1 单分区查询

select 需查询字段 from 表名 where 分区字段名称=字段值

hive> select * from song1 where month=20220110;
OK
1       never3  3       20220110
2       over3   2       20220110

hive> select name from song1 where month=20220109;
OK
never2
over2

3.3.2 多分区查询

①:通过union

select 需查询字段 from 表名 where 分区字段名称=字段值1

union

select 需查询字段 from 表名 where 分区字段名称=字段值2 ;

hive> select * from song1 where month='20220109'
    > union
    > select * from song1 where month='20220110';
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
Query ID = root_20220110232623_3d981c7c-6cee-460d-a564-038775507413
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks not specified. Estimated from input data size: 1
In order to change the average load for a reducer (in bytes):
  set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
  set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
  set mapreduce.job.reduces=<number>
Starting Job = job_1641870198202_0002, Tracking URL = http://master:8088/proxy/application_1641870198202_0002/
Kill Command = /opt/software/hadoop-2.7.1/bin/hadoop job  -kill job_1641870198202_0002
Hadoop job information for Stage-1: number of mappers: 2; number of reducers: 1
2022-01-10 23:26:52,299 Stage-1 map = 0%,  reduce = 0%
2022-01-10 23:27:15,111 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 5.97 sec
2022-01-10 23:27:24,753 Stage-1 map = 100%,  reduce = 100%, Cumulative CPU 8.3 sec
MapReduce Total cumulative CPU time: 8 seconds 300 msec
Ended Job = job_1641870198202_0002
MapReduce Jobs Launched:
Stage-Stage-1: Map: 2  Reduce: 1   Cumulative CPU: 8.3 sec   HDFS Read: 16922 HDFS Write: 213 SUCCESS
Total MapReduce CPU Time Spent: 8 seconds 300 msec
OK
1       never2  3       20220109
1       never3  3       20220110
2       over2   2       20220109
2       over3   2       20220110

②通过 or

select 需查询字段 from 表名 where 分区字段名称=字段值1 or 分区字段名称=字段值2

hive> select * from song1 where month=20220110 or month=20220108;
OK
1       never   3       20220108
2       over    2       20220108
1       never3  3       20220110
2       over3   2       20220110

3.3.3查询二级分区数据

语法:select 查询字段 from 表名 where 分区字段1=分区字段1的值 

           and 分区字段2=分区字段2的值;

select * from singpartition where month='20220111' and day=13;

3.3.4增加分区

①增加单个分区

语法:alter table 表名 add partition (分区字段='分区字段值');

hive> alter table song1 add partition(month='20220112');

②同时创建多个分区 add partition (分区字段='分区字段值1') partition (分区字段='分区字段值2');

语法:alter table 表名 

hive> alter table song1 add partition(month='20220111') partition(month='20220106');

 3.3.5删除分区

①删除单个分区

语法:alter table 表名 drop partition(分区字段='分区字段值');

hive> alter table song1 drop partition(month='20220112');
Dropped the partition month=20220112
OK
Time taken: 0.777 seconds

①删除多个分区

语法:alter table 表名 drop partition(分区字段='分区字段值')partition(分区字段='分区字段值2');

alter table song1 drop partition(month='20220111'),partition(month='20220106');
Dropped the partition month=20220106
Dropped the partition month=20220111
OK
Time taken: 0.407 seconds

 3.3.6查询表中分区

语法:show partitions 表名

hive> show partitions song1;
OK
month=20220108
month=20220109
month=20220110

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值