hive分区partition(动态和静态分区混合使用; partition的简介)

分区是hive存放数据的一种方式。将列值作为目录来存放数据,就是一个分区。这样where中给出列值时,只需根据列值直接扫描对应目录下的数据,不扫面其他不关心的分区,快速定位,查询节省大量时间。分动态和静态分区两种

动态分区

1)不显示的给出分区名,根据列的取值自动建立对应分区(多少种取值,多少种分区),所以需要限制最大分区数:
    SET hive.exec.dynamic.partition=true;
    SET hive.exec.max.dynamic.partitions.pernode=1000;
    SET hive.exec.max.dynamic.partitions=3000; 
2)分区全部使用动态分区,还要设置为nonstrict模式,否则无法执行
    set hive.exec.dynamic.partition.mode=nonstrict;

3)动态分区按位置来对应,跟名称无关。所以查询select中必须在最后字段按动态分区顺序给出对应字段
    insert overwrite table tb_pmp_raw_log_analysis_count partition (day='2016-05-17', media,type)
        select advertiser_id,ad_plan_id,crt_id,hour,ad_place_id,city_bidrequest,device,network,os,category,channel,ad_type,rtb_type,price,count(1) as cnt,media, 1 as type
        from (
          select 
            split(all,'\\|~\\|')[41] as advertiser_id,
            split(all,'\\|~\\|')[10] as ad_plan_id,
            split(all,'\\|~\\|')[11] as crt_id,
            substr(split(all,'\\|~\\|')[0],12,2) as hour,
            case 
              when split(split(all,'\\|~\\|')[5],'/')[1] = 'wax' then split(all,'\\|~\\|')[17]
              else split(all,'\\|~\\|')[7]
            end as city_bidrequest,
            NULL as device,
            NULL as network,
            NULL as os,
            NULL as category,
            NULL as channel,
            NULL as ad_type,
            NULL as rtb_type,
            split(all,'\|~\|')[13] as price,
            split(split(all,'\\|~\\|')[5],'/')[1] as media
          from tb_pmp_raw_log_bid_tmp tb
        ) a 
        group by advertiser_id,ad_plan_id,crt_id,hour,ad_place_id,city_bidrequest,device,network,os,category,channel,ad_type,rtb_type,price,media
        distribute by media;
注意:动态分区名称无关,顺序必须一致

4)distribute by 和动态分区原理一样,一般不一起用,如代码

5)动态分区与静态分区混用:
    a) 只要有一个静态分区,就可以在默认的strict状态执行。如上代码,动静分区混用时,不用设置nonstrict状态。
    b) 动态分区不能在静态分区前面,在select中按位置顺序出现在最后(因为静态分区提前产生,动态分区运行时产生,如果动态分区作为父路径,则子静态分区无法提前生成。会报错为动态分区不能为静态的父路径)。代码中,type设为静态,但media是动态,会报错,所以才在内部给定type的值。如果分区顺序改为day、type、media,则可以将day和type作为静态,media做动态。

Hive partition 导出时无名称:

存在分区partition的hive表,用select *返回包括分区字段值。
但select * >localdata.txt导入本地文件,无分区字段名称,但值记录存在。也就是说,文件中第一行的属性字段中无分区字段属性,而分区属性的取值在记录中存在。
如果要导入数据到mysql里,或用kettle读入时,记得要手动加上分区列对应的属性列名称

Hive partition 简介

在HiveSelect查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作。有时候只需要扫描表中关心的一部分数据,因此引入了partition概念。建表时指定partition空间。一个Partition对应于表下的一个目录,所有的Partition的数据都存储在自己的目录。
总的说来partition就是辅助查询,缩小查询范围,加快数据的检索速度和对数据按照一定的规格和条件进行管理。

Partition的使用:

查看分区:show partitions hive_tb;
1、分区以字段形式在表结构中存在,通过describetable命令可以查看分区字段,但该字段不存放实际的数据内容,仅仅是分区的表示。
2、可有多个分区,每个分区以文件夹的形式单独存在表文件夹的目录下。
3、分区建表分为2种,一种是单分区,在表文件夹目录下只有一级文件夹目录。另外一种是多分区,表文件夹下多文件夹嵌套模
a、单分区建表语句:createtable day_table (id int, content string) partitioned by (dt string);单分区表,按天分区,在表结构中存在id,content,dt三列。
b、双分区建表语句:createtable day_hour_table (id int, content string) partitioned by (dt string, hourstring);双分区表,按天和小时分区,在表结构中新增加了dt和hour两列。

Hive partition基本语法:

1.创建分区表语法:
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_namedata_type [COMMENT col_comment], ...)] [COMMENT table_comment] [PARTITIONED BY(col_name data_type [COMMENT col_comment], ...)] [CLUSTERED BY (col_name,col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS][ROW FORMAT row_format] [STORED AS file_format] [LOCATION hdfs_path]
双分区表,按天和小时分区,在表结构中新增加了dt和hour两列。
create table day_hour_table (id int, content string)partitioned by (dt string, hour string);
2.添加分区表语法(表已创建,在此基础上添加分区):
ALTERTABLE table_name ADD partition_spec [ LOCATION 'location1' ] partition_spec [LOCATION 'location2' ] ... partition_spec: : PARTITION (partition_col =partition_col_value, partition_col = partiton_col_value, ...)
向一个表中增加分区(ALTER TABLE ADDPARTITION)。当分区名是字符串时加引号。例:
ALTERTABLE day_table ADD PARTITION (dt='2008-08-08', hour='08') location'/path/pv1.txt' PARTITION (dt='2008-08-08', hour='09') location'/path/pv2.txt';
3.删除分区(ALTER TABLE table_name DROP partition_spec,partition_spec,…)分区的元数据和数据将被一并删除,例:
ALTERTABLE day_hour_table DROP PARTITION (dt='2008-08-08', hour='09');
4.分区表加载数据语法:
LOADDATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION(partcol1=val1, partcol2=val2 ...)]

例:

LOADDATA INPATH '/user/pv.txt' INTO TABLE day_hour_table PARTITION(dt='2008-08-08', hour='08'); LOAD DATA local INPATH '/user/hua/*' INTO TABLE day_hourpartition(dt='2010-07- 07');
数据加载至表中,不会对数据进行任何转换。Load操作只是将数据复制至Hive表对应的位置。数据加载时自动在表下创建一个目录,文件存放在该分区下。
5.基于分区的查询的语句:
SELECTday_table.* FROM day_table WHERE day_table.dt>= '2008-08-08';
6. 查看分区:
show partitions 表名;  
show partitions hive_tb;

hive中partition的实际操作:

hive>create table mp (a string) partitioned by (b string, c string);
hive> alter table mp add partition (b='1', c='1');
hive> alter table mp add partition (b='1', c='2');
hive> alter table mp add partition (b='2', c='2');
hive> show partitions mp ;

b=1/c=1
b=1/c=2
b=2/c=2

hive>explain extended alter table mp drop partition (b='1');

ABSTRACTSYNTAX TREE:
(TOK_ALTERTABLE_DROPPARTS mp (TOK_PARTSPEC (TOK_PARTVAL b '1')))
STAGEDEPENDENCIES:
Stage-0 is a root stage
STAGEPLANS:
Stage: Stage-0
Drop Table Operator:
Drop Table
table: mp

hive> alter table mp drop partition (b='1');
FAILED: Error in metadata: table is partitioned but partition spec is notspecified or tab: {b=1}
FAILED: Execution Error, return code 1 fromorg.apache.hadoop.hive.ql.exec.DDLTask
hive> show partitions mp ;

b=1/c=1
b=1/c=2
b=2/c=2

hive>alter table mp add partition ( b='1', c = '3') partition ( b='1' , c='4');
hive> show partitions mp ;
b=1/c=1
b=1/c=2
b=1/c=3
b=1/c=4
b=2/c=2
b=2/c=3

hive>insertoverwrite table mp partition (b='1', c='1') select cnt from tmp_et3 ;
hive>altertable mp add columns (newcol string);
location指定目录结构:
alter table wizad_mdm_raw_hdfs add partition (day='2014-11-07',adn='3') location '/user/wizad/data/wizad/raw/2014-11-07/3_1/';
hive>alter table alter2 add partition (insertdate='2008-01-01') location'2008/01/01';
hive>alter table alter2 add partition (insertdate='2008-01-02') location'2008/01/02';
原文地址:https://www.cnblogs.com/cl1024cl/p/6205344.html