美文网首页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 1.2.1 分区和分捅

    1. 借鉴 Hive学习笔记——Hive中的分桶Hive分区和分桶(0925)HIVE表索引,分区和分桶的区别 2...

  • Hive分区和分桶(0925)

    分区(Partitions) 为了提高性能,Hive可以对数据进行分区• 分区列的值将一个表划分为一个个片段(文...

  • 案例详解__HIVE中内部表、外部表、分区表和分桶表

    目录一、Hive建表语法二、内部表外部表三、分区表四、分桶表 Hive在建表时可指定内部表、外部表、分区表和分桶表...

  • Hive 分区/分桶

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

  • 好程序员大数据培训教程分享hive分区和分桶

    好程序员大数据培训教程分享hive分区和分桶,hive 分区 1.为什么要分区?? 当单个表数据量越来越大的时候,...

  • Hive 分桶

    Hive 分桶 分桶对于每一个表或者分区,Hive可以进一步组织成桶,也就是更为细粒度的数据范围划分Hive是针对...

  • HIVE分区、分桶和索引

    分区 分区是以字段的形式在表结构中存在,通过describe table命令可以查看到字段存在,但是该字段不存放实...

  • Hive 分桶详解

    1分桶 1.1什么是分桶?和分区有什么区别? 分区:Hive在查询数据的时候,一般会扫描整个表的数据,会消耗很多不...

  • 大数据框架(分区,分桶,分片)

    前言 在大数据分布式中,分区,分桶,分片是设计框架的重点。此篇就来总结各个框架。建议收藏 目录 Hive分区与分桶...

  • Hive分桶

    Hive分桶 分桶表是对列值取哈希值的方式,将不同数据放到不同文件中存储。 对于hive中每一个表、分区都可以进一...

网友评论

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

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