Hive之分区表

Hive分区表

1. 说明

  • 分区表的一个分区对应hdfs上的一个目录
  • 分区表包括静态分区表和动态分区表,根据分区会不会自动创建来区分
  • 多级分区表,即创建的时候指定 PARTITIONED BY (event_month string,loc string),根据顺序,级联创建 event_month=XXX/loc=XXX目录,其他和一级的分区表是一样

2. 静态分区

生成数据

[root@master Hive]# cat date.sh
for i in $(seq 20)
do
  echo -e  "$(date -d "$RANDOM minute ago"  +%F' '%T'.'%N)"
done|sort -nk1|awk '{print NR"\t"$0}'>test.txt
[root@master Hive]# sh date.sh
[root@master Hive]# cat test.txt
1    2018-06-13 07:47:50.981100534
2    2018-06-14 16:17:50.984444067
3    2018-06-15 00:16:50.977659799
4    2018-06-16 09:57:50.980036092
5    2018-06-16 16:28:50.983383590
6    2018-06-16 18:11:50.995934741
7    2018-06-16 20:42:50.976483956
8    2018-06-19 17:42:50.994897479
9    2018-06-20 13:21:50.993988405
10    2018-06-20 21:20:50.988528977
11    2018-06-21 09:24:50.990346343
12    2018-06-22 00:09:50.985461831
13    2018-06-26 12:18:50.982232601
14    2018-06-26 22:21:50.987616378
15    2018-06-29 23:12:50.993019664
16    2018-06-30 05:09:50.989413716
17    2018-07-01 03:18:50.992127089
18    2018-07-02 21:51:50.991226963
19    2018-07-03 08:29:50.986623743
20    2018-07-05 15:45:50.978901099

创建静态分区表结构

create table static_part (
ord_num string,
ord_date string
)
PARTITIONED BY (ord_date_month string)
row format delimited fields terminated by '\t';

往分区里面导入数据

load data local inpath '/root/Hive/test.txt' overwrite into table static_part partition (ord_date_month='2018-06');  //静态分区导入数据需要指定partition的key
select * from static_part where ord_date_month='2018-06';

3. 动态分区

创建动态分区表结构

create table dynamic_part (
ord_num string
)
PARTITIONED BY (ord_date string)      //把订单日期自动做分区
row format delimited fields terminated by '\t';

开启动态分区

set hive.exec.dynamic.partition=true; //使用动态分区
set hive.exec.dynamic.partition.mode=nonstrict; //无限制模式

把刚才静态分区的数据导入到静态分区,用ord_date自动分区

insert into table dynamic_part partition(ord_date) select ord_num,ord_date from static_part;

 

更多用法:https://www.cnblogs.com/ilvutm/p/7152499.html

posted @ 2018-07-05 17:20  雪山过客  阅读(1937)  评论(0编辑  收藏  举报