美文网首页Hive
Hive分区和分桶(0925)

Hive分区和分桶(0925)

作者: hipeer | 来源:发表于2018-09-26 17:33 被阅读0次

    分区(Partitions)

    • 为了提高性能,Hive可以对数据进行分区
      • 分区列的值将一个表划分为一个个片段(文件夹)
      • 可以在查询时忽略整个分区
      • 类似于关系数据库
    • 分区必须由用户正确创建。插入数据时必须指定分区
    • “分区”列和常规列的模式没有区别
    • 在查询时,Hive会自动过滤分区以获得更好的性能

    注:原始数据表customer

    1. 静态分区

    • 创建分区表
    create external table customer_partition(
        name string,
        city string
    )
      partitioned by (country string)
      row format delimited
      fields terminted by ',';
    
    • 查看指定表的分区
    show partitions customer_partition;
    
    • 添加分区
    alter table customer add partition (country='USA') 
                             partition (country='Canada') 
                             partition (country='Mexico');
    
    • 往分区表中加载数据
     1. 使用load加载,比如customer.csv文件放在本地机器的/root/data下,把他加载到HDFS上的分区表中
    
        load data local inpath '/root/data/customer.csv' [overwrite] into table customer_partition partition(countrt='USA');
    
    2. 已查询的方式往分区表中插入数据
    
        insert into table customer_partition partition(country='USA') select name, city from customer where country='USA';
    
    
    • 删除分区
    alter table customer_partition drop partition(country='USA');
    alter table customer_partition drop if exists partition(country="USA");
    

    2. 动态分区
    静态分区添加分区时需要手动用alter添加分区,load数据时每一个分区都要添加。为了方便,可以根据查询得到的数据动态分配到分区里。动态分区与静态分区区别就是不指定分区目录。

    • 创建动态分区表
    create external table customer_auto_partition(
        name string,
        city string
    )
    partitioned by (country string) 
    row format delimited
    fields terminated by ','
    stored as textfile;
    
    • 设置非严格型动态分区模式
    set hive.exec.dynamic.partition.mode=nonstrict;
    
    • 往分区表中插入数据
    insert into table customer_auto_partition partition(country) select c.name, c.city, c.country from customer c;
    或者
    insert overwrite table customer_auto_partition partition(country) select c.name, c.city, c.country from customer c;
    

    注意:
    1)如果hive没有设置动态分区功能,需要设置set hive.exec.dynamic.partition=true
    2)动态分区严禁型模式下,要求至少有一个静态分区列(动态分区不能在静态分区前
    面,在select中按位置顺序出现在最后)
    3)load命令不支持动态分区插入,以下用法错误

    load data local inpath '/root/customer.csv' into customer_auto_partition partition(country);
    

    4)Hive会限制动态分区可以创建的最大分区数,用来避免由于创建了太多分区导致超过了文件系统的处理能力以及其他问题,所以如果分区太多会导致执行失败


    分区特点:
    1)创建分区,实则是在hdfs上创建表目录的子目录,可以创建多级分区
    2)分区可以建在内部表或外部表,在建表时定义。利用alter添加分区
    3)删除内部表分区,对应分区的数据也删除。外部表分区删除,数据不会删除
    注意:不要试图在数据文件找分区列的数据,并不在数据文件中


    使用场合:
    数据需要连续性输入时用静态分区
    进行数据清洗、数据转换时用动态分区


    分桶(Buckets)

    • 桶对应与HDFS中的文件片段
    • 基于“bucket列”的哈希函数将数据分解为一组bucket
    • Hive不会自动设置分桶, 用户需要通过设置reducer的数量或设置桶数来指定分桶数量
      SET mapred.reduce.tasks = 256;
      SET hive.enforce.bucketing = true;
    • 为了定义桶的数量,我们应该避免在每个桶中有太多或太少的数据。一个更好的选择在两个数据块附近。使用2N作为桶数.
    • 创建分桶表
      使用'clustered by '语句定义bucket
    create external table customer_bucket(
        name string,
        city string,
        country string
    )
    clustered by (city) sotred by (country) into 6 buckets
    row format delimited 
    fields terminated by ','
    stored as textfile;
    
    

    注:sorted by 是对桶中的数据排序需要把hive.enforce.sorting设为true

    • 往表中插入数据
      为了将数据填充到桶中,必须使用insert语句而不是load语句,因为它不根据元数据定义验证数据(只将文件复制到文件夹)
    必须使用insert
    insert overwrite table custmoer_bucket select name, city, country from customer;
    

    分桶的优点:
    1)由于桶的数量是固定的,所以没有数据波动
    2)非常适合数据抽样
    3)有利于执行高效的map-side JOIN

    相关文章

      网友评论

        本文标题:Hive分区和分桶(0925)

        本文链接:https://www.haomeiwen.com/subject/dtwxoftx.html