• HIVE-分区表详解以及实例


    HIVE中的分区表是什么,我们先看操作,然后再来体会。 

    创建一个分区表,分区的单位时dt和国家名
    hive> create table logs(ts bigint,line string)
        > partitioned by (dt String,country string);

    接下来我们创建要给分区

    hive> load data local inpath '/root/hive/partitions/file1' into table logs
        > partition (dt='2001-01-01',country='GB');

    上面语句的效果是在hdfs系统上建立了一个层级目录

      -logs

        -dt=2001-01-01

        -country=GB

    我们继续执行下面语句,先看一下什么效果
    hive>  load data local inpath '/root/hive/partitions/file2' into table logs
        > partition (dt='2001-01-01',country='GB');
    Loading data to table default.logs partition (dt=2001-01-01, country=GB)
    OK
    Time taken: 1.379 seconds
    hive>  load data local inpath '/root/hive/partitions/file3' into table logs
        > partition (dt='2001-01-01',country='US');
    Loading data to table default.logs partition (dt=2001-01-01, country=US)
    OK
    Time taken: 1.307 seconds
    hive>  load data local inpath '/root/hive/partitions/file4' into table logs
        > partition (dt='2001-01-02',country='GB');
    Loading data to table default.logs partition (dt=2001-01-02, country=GB)
    OK
    Time taken: 1.253 seconds
    hive>  load data local inpath '/root/hive/partitions/file5' into table logs
        > partition (dt='2001-01-02',country='US');
    Loading data to table default.logs partition (dt=2001-01-02, country=US)
    OK
    Time taken: 1.07 seconds
    hive>  load data local inpath '/root/hive/partitions/file6' into table logs
        > partition (dt='2001-01-02',country='US');
    Loading data to table default.logs partition (dt=2001-01-02, country=US)
    OK
    Time taken: 1.227 seconds

    我们到HDFS上查看,发现建立了下面层级目录

    /user/hive/warehouse/logs
    ├── dt=2001-01-01/
    │ ├── country=GB/
    │ │ ├── file1
    │ │ └── file2
    │ └── country=US/
    │ └── file3
    └── dt=2001-01-02/
    ├── country=GB/
    │ └── file4
    └── country=US/
    ├── file5
    └── file6 

    是加上所有files的内容基本上一样,蓝色的^A是系统默认分隔符。八进制是‘01’.随后参考我的另一个文章。比较详细解释了分隔符。

    总结:分区表的意思,其实想明白了就很简单。就是在系统上建立文件夹,把分类数据放在不同文件夹下面,加快查询速度。

    关键点1:partitioned by (dt String,country string); 创建表格时,指明了这是一个分区表。将建立双层目录,第一次目录的名字和第二层目录名字规则

    PARTITIONED BY子句中定义列,是表中正式的列,成为分区列。但是数据文件中并没有这些值,仅代表目录。

    关键点2: partition (dt='2001-01-01',country='GB'); 上传数据时,把数据分别上传到不同分区中。也就是分别放在不同的子目录下。

    理解分区就是文件夹分而治之,查询的时候可以当作列名来显示查询的范围。

    查看分区结构
    hive> show partitions logs;
    OK
    dt=2001-01-01/country=GB
    dt=2001-01-01/country=US
    dt=2001-01-02/country=GB
    dt=2001-01-02/country=US

     

    条件限定了country='GB'目录所以只有file1,2,4的内容输出
    hive> select ts,dt,line 
        > from logs
        > where country='GB';
    OK
    1    2001-01-01    Log line 1
    2    2001-01-01    Log line 2
    4    2001-01-02    Log line 4

    现在只查看dt=2001-01-02目录下country=US的文件夹下的数据。

    hive> select ts,dt,line
    > from logs
    > where dt='2001-01-02'
    > and country='US';
    OK
    5 2001-01-02 Log line 5
    6 2001-01-02 Log line 6

     
  • 相关阅读:
    android焦点
    URI和URL的区别比较与理解
    Android Bundle类
    repo命令
    ubuntu adb找不到设备
    【python】-网络编程
    【python】-反射
    【python】-类的特殊成员方法
    【python】-7-面向对象的进阶
    【python】-多态
  • 原文地址:https://www.cnblogs.com/kouryoushine/p/7801924.html
Copyright © 2020-2023  润新知