Hive 分区/分桶

2018-09-11  本文已影响0人  点点渔火

分区/桶

Hive 分区

Hive的分区方式:由于Hive实际上是数据文件在HDFS存在的目录区分
分区字段是虚拟列

静态分区

静态分区

create table bitag.f_feed_article_info
(  
source                  string,                            
domain                  string,                                     
hash_id                 string ,                                     
url                     string ,                                    
is_news                 int,                                         
title                   string,                                      
content                 string,                                      
body                    string,                                      
summary                 string,                                      
img_url                 string,                                      
article_time            bigInt,                                         
raw_content             string,                                      
first_tag               string,                                      
second_tag              string,                                      
unique_hash             string,                                      
title_hash              string,                                      
update_time             timestamp  
)  
comment 'feed文章'  
partitioned by (dt string comment '当前时间,用于分区字段')  
row format delimited  
stored as PARQUET ;

静态分区写入指定写入的分区字段partition

insert into bitag.f_feed_article_info partition(dt = '$tDate') select * from detectTags
动态分区
HIVE参数 默认值 解释
hive.exec.dynamic.partition false 是否开启动态分区功能
hive.exec.dynamic.partition.mode strict 动态分区的模式,表示必须指定至少一个分区为静态分区,nonstrict模式表示允许所有的分区字段都可以使用动态分区。
hive.exec.max.dynamic.partitions.pernode 100 在每个执行MR的节点上,最大可以创建多少个动态分区。
hive.exec.max.dynamic.partitions 1000 在所有执行MR的节点上,最大一共可以创建多少个动态分区。
hive.exec.max.created.files 100000 整个MR Job中,最大可以创建多少个HDFS文件。
hive.error.on.empty.partition false 当有空分区生成时,是否抛出异常。一般不需要设置。
set hive.exec.dynamic.partition=true;

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

set hive.exec.max.dynamic.partitions.pernode=10000;

set hive.exec.max.dynamic.partitions=1000000;

set hive.exec.max.created.files=1000000;

CREATE TABLE table (
url String
) PARTITIONED BY (month String,day String) 
stored AS textfile;

INSERT overwrite TABLE d_table PARTITION (month,day) 
SELECT url,substr(day,1,7) AS month,day 
FROM d_table;

Hive 桶

对于每一个表(table)或者分区, Hive可以进一步组织成桶,也就是说桶是更为细粒度的数据范围划分。Hive也是 针对某一列进行桶的组织。Hive采用对列值哈希,然后除以桶的个数求余的方式决定该条记录存放在哪个桶当中。

把表(或者分区)组织成桶(Bucket)有两个理由:

语法

    create table student(id INT, age INT, name STRING)

    partitioned by(stat_date STRING)

    clustered by(id) sorted by(age) into 2 buckets
    
    row format delimited fields terminated by ',';
    
 
    create table student1(id INT, age INT, name STRING)

    partitioned by(stat_date STRING)

    clustered by(id) sorted by(age) into 2 buckets

    row format delimited fields terminated by ',';

set hive.enforce.bucketing = true; # 可以自动控制上一轮reduce的数量从而适配bucket的个数

LOAD DATA local INPATH '/home/lijun/bucket.txt' OVERWRITE INTO TABLE student partition(stat_date="20120802");

from student1 
insert overwrite table student1 partition(stat_date="20120802") 
select id,age,name where stat_date="20120802" sort by age;


hadoop fs -ls /hive/warehouse/test.db/student1/stat_date=20120802;


select * from student1 tablesample(bucket 1 out of 2 on id);

注:tablesample是抽样语句,语法:TABLESAMPLE(BUCKET x OUT OF y)

y必须是table总bucket数的倍数或者因子。hive根据y的大小,决定抽样的比例。例如,table总共分了64份,当y=32时,抽取(64/32=)2个bucket的数据,当y=128时,抽取(64/128=)1/2个bucket的数据。x表示从哪个bucket开始抽取。

例如,table总bucket数为32,tablesample(bucket 3 out of 16),表示总共抽取(32/16=)2个bucket的数据,分别为第3个bucket和第(3+16=)19个bucket的数据。

参考:
https://blog.csdn.net/wisgood/article/details/17186107
https://blog.csdn.net/strongyoung88/article/details/53743937/
http://lxw1234.com/archives/2015/06/286.htm
https://www.cnblogs.com/yongjian/archive/2017/03/29/6640951.html

上一篇 下一篇

猜你喜欢

热点阅读