zoukankan      html  css  js  c++  java
  • Hive数据模型

    Hive的数据模型-数据库

    • 类似传统数据库的DataBase

    • 默认数据库"default"

    • 使用#hive命令后,不使用hive>use <数据库名>,系统默认的数据库。可以显式使用hive> use default;
    • 创建一个新库

    hive > create database test_dw;
     
     
     
    1
     
     
     
    1
    hive > create database test_dw;
     
     

    十、Hive的数据模型-表

    • Table 内部表

    • Partition 分区表

    • External Table 外部表

    • Bucket Table 桶表

    十一、Hive的数据模型-内部表

    • 与数据库中的 Table 在概念上是类似

    • 每一个 Table 在 Hive 中都有一个相应的目录存储数据。例如,一个表 test,它在 HDFS 中的路径为:/ warehouse/test。 warehouse是在 hive-site.xml 中由 ${hive.metastore.warehouse.dir} 指定的数据仓库的目录

    • 所有的 Table 数据(不包括 External Table)都保存在这个目录中。

    • 删除表时,元数据与数据都会被删除

    • 内部表的使用

      • 创建数据文件inner_table.dat
      • 创建表
      • hive>create table inner_table (key string);
         
         
         
        1
         
         
         
        1
        hive>create table inner_table (key string);
         
         
      • 加载数据

      • hive>load data local inpath '/root/inner_table.dat' into table inner_table;
         
         
         
        1
         
         
         
        1
        hive>load data local inpath '/root/inner_table.dat' into table inner_table;
         
         
      • 查看数据

      • select * from inner_table
        select count(*) from inner_table
         
         
         
        2
         
         
         
        1
        select * from inner_table
        2
        select count(*) from inner_table
         
         
      • 删除表

      • drop table inner_table
         
         
         
        1
         
         
         
        1
        drop table inner_table
         
         

    十二、Hive的数据模型-分区表

    • Partition 对应于数据库的 Partition 列的密集索引

    • 在 Hive 中,表中的一个 Partition 对应于表下的一个目录,所有的 Partition 的数据都存储在对应的目录中

    • 例如:test表中包含 date 和 city 两个 Partition,
      则对应于date=20180729, city = bj 的 HDFS 子目录为:
      /warehouse/test/date=20130201/city=bj
      对应于date=20180729, city=sh 的HDFS 子目录为;
      /warehouse/test/date=20180729/city=sh
       
       
       
      5
       
       
       
      1
      例如:test表中包含 date 和 city 两个 Partition,
      2
      则对应于date=20180729, city = bj 的 HDFS 子目录为:
      3
      /warehouse/test/date=20130201/city=bj
      4
      对应于date=20180729, city=sh 的HDFS 子目录为;
      5
      /warehouse/test/date=20180729/city=sh
       
       
    • 分区表

    • CREATE TABLE tmp_table #表名
      (
      title   string, # 字段名称 字段类型
      minimum_bid     double,
      quantity       bigint,
      have_invoice   bigint
      )COMMENT '注释:XXX' #表注释
      PARTITIONED BY(pt STRING) #分区表字段(如果你文件非常之大的话,采用分区表可以快过滤出按分区字段划分的数据)
      ROW FORMAT DELIMITED 
        FIELDS TERMINATED BY '01'   # 字段是用什么分割开的
      STORED AS SEQUENCEFILE; #用哪种方式存储数据,SEQUENCEFILE是hadoop自带的文件压缩格式
       
       
       
      x
       
       
       
       
       
      1
      CREATE TABLE tmp_table #表名
      2
      (
      3
      title   string, # 字段名称 字段类型
      4
      minimum_bid     double,
      5
      quantity       bigint,
      6
      have_invoice   bigint
      7
      )COMMENT '注释:XXX' #表注释
      8
      PARTITIONED BY(pt STRING) #分区表字段(如果你文件非常之大的话,采用分区表可以快过滤出按分区字段划分的数据)
      9
      ROW FORMAT DELIMITED 
      10
        FIELDS TERMINATED BY '01'   # 字段是用什么分割开的
      11
      STORED AS SEQUENCEFILE; #用哪种方式存储数据,SEQUENCEFILE是hadoop自带的文件压缩格式
       
       
    • 一些相关命令

    • SHOW TABLES; # 查看所有的表
      SHOW TABLES '*TMP*'; #支持模糊查询
      SHOW PARTITIONS TMP_TABLE; #查看表有哪些分区
      DESCRIBE TMP_TABLE; #查看表结构
       
       
       
      4
       
       
       
      1
      SHOW TABLES; # 查看所有的表
      2
      SHOW TABLES '*TMP*'; #支持模糊查询
      3
      SHOW PARTITIONS TMP_TABLE; #查看表有哪些分区
      4
      DESCRIBE TMP_TABLE; #查看表结构
       
       
    • 分区表的使用

      • 创建数据文件partition_table.dat
      • 创建表

      • create table partition_table(rectime string,msisdn string) partitioned by(daytime string,city string)
        row format delimited fields terminated by '	' stored as TEXTFILE;
         
         
         
        2
         
         
         
        1
        create table partition_table(rectime string,msisdn string) partitioned by(daytime string,city string)
        2
        row format delimited fields terminated by '	' stored as TEXTFILE;
         
         
      • 加载数据到分区

      • load data local inpath '/home/partition_table.dat' into table partition_table partition
        (daytime='2013-02-01',city='bj');
         
         
         
        2
         
         
         
        1
        load data local inpath '/home/partition_table.dat' into table partition_table partition
        2
        (daytime='2013-02-01',city='bj');
         
         
      • 查看数据

      • select * from partition_table
        select count(*) from partition_table
         
         
         
        2
         
         
         
        1
        select * from partition_table
        2
        select count(*) from partition_table
         
         
      • 删除表

      • drop table partition_table
         
         
         
        1
         
         
         
        1
        drop table partition_table
         
         
      • 通过load data 加载数据

      • alter table partition_table add partition (daytime='2018-07-29',city='bj');
         
         
         
        1
         
         
         
        1
        alter table partition_table add partition (daytime='2018-07-29',city='bj');
         
         
      • 元数据,数据文件删除,但目录daytime=2013-02-04还在

      • alter table partition_table drop partition (daytime='2018-07-29',city='bj')
         
         
         
        1
         
         
         
        1
        alter table partition_table drop partition (daytime='2018-07-29',city='bj')
         
         

    十三、Hive的数据模型—桶表

    • 桶表是对数据进行哈希取值,然后放到不同文件中存储。

    • 创建表

    • create table bucket_table(id string) clustered by(id) into 4 buckets;
       
       
       
      1
      1
       
       
       
       
      1
      create table bucket_table(id string) clustered by(id) into 4 buckets;
       
       
    • 加载数据

    • set hive.enforce.bucketing = true;
      insert into table bucket_table select name from stu;
      insert overwrite table bucket_table select name from stu;
       
       
       
      3
      3
       
       
       
       
      1
      set hive.enforce.bucketing = true;
      2
      insert into table bucket_table select name from stu;
      3
      insert overwrite table bucket_table select name from stu;
       
       
    • 数据加载到桶表时,会对字段取hash值,然后与桶的数量取模。把数据放到对应的文件中。

    • 抽样查询

    • select * from bucket_table tablesample(bucket 1 out of 4 on id);
       
       
       
      1
      1
       
       
       
       
       
      1
      select * from bucket_table tablesample(bucket 1 out of 4 on id);
       
       

    十四、Hive的数据模型-外部表

    • 指向已经在 HDFS 中存在的数据,可以创建 Partition

    • 它和 内部表 在元数据的组织上是相同的,而实际数据的存储则有较大的差异

    • 内部表 的创建过程和数据加载过程(这两个过程可以在同一个语句中完成),在加载数据的过程中,实际数据会被移动到数据仓库目录中;之后对数据对访问将会直接在数据仓库目录中完成。删除表时,表中的数据和元数据将会被同时删除

    • 外部表 只有一个过程,加载数据和创建表同时完成,并不会移动到数据仓库目录中,只是与外部数据建立一个链接。当删除一个 外部表 时,仅删除该链接

    • 外部表

    • CREATE EXTERNAL TABLE page_view
      ( viewTime INT, 
      userid BIGINT,
      page_url STRING, 
      referrer_url STRING, 
      ip STRING COMMENT 'IP Address of the User',
      country STRING COMMENT 'country of origination‘
      )
        COMMENT 'This is the staging page view table'
        ROW FORMAT DELIMITED FIELDS TERMINATED BY '44' LINES TERMINATED BY '12'
        STORED AS TEXTFILE
        LOCATION 'hdfs://centos:9000/user/data/staging/page_view';
       
       
       
      12
      12
       
       
       
       
      1
      CREATE EXTERNAL TABLE page_view
      2
      ( viewTime INT, 
      3
      userid BIGINT,
      4
      page_url STRING, 
      5
      referrer_url STRING, 
      6
      ip STRING COMMENT 'IP Address of the User',
      7
      country STRING COMMENT 'country of origination‘
      8
      )
      9
        COMMENT 'This is the staging page view table'
      10
        ROW FORMAT DELIMITED FIELDS TERMINATED BY '44' LINES TERMINATED BY '12'
      11
        STORED AS TEXTFILE
      12
        LOCATION 'hdfs://centos:9000/user/data/staging/page_view';
       
       
    • 外部表的使用

    • 创建数据文件external_table.dat

      • 创建表

      • hive>create external table external_table1 (key string) ROW FORMAT DELIMITED 
                              FIELDS TERMINATED BY '	' location '/home/external';
        在HDFS创建目录/home/external
        #hadoop fs -put /home/external_table.dat /home/external
         
         
         
        4
        4
         
         
         
         
        1
        hive>create external table external_table1 (key string) ROW FORMAT DELIMITED 
        2
                              FIELDS TERMINATED BY '	' location '/home/external';
        3
        在HDFS创建目录/home/external
        4
        #hadoop fs -put /home/external_table.dat /home/external
         
         
      • 加载数据

      • LOAD DATA INPATH '/home/external_table1.dat' INTO TABLE external_table1;
         
         
         
        1
        1
         
         
         
         
         
        1
        LOAD DATA INPATH '/home/external_table1.dat' INTO TABLE external_table1;
         
         
      • 查看数据

      • select * from external_table
        select count(*) from external_table
         
         
         
        2
        2
         
         
         
         
        1
        select * from external_table
        2
        select count(*) from external_table
         
         
      • 删除表

      • drop table external_table
         
         
         
        1
        1
         
         
         
         
         
        1
        drop table external_table
         
         



  • 相关阅读:
    Maven--setting详解
    OAuth 2.0 的四种方式
    C#站点检测
    SonarQube--项目工程代码质量检测神奇
    在外租房子,切记九点
    在线关系图工具
    ppt thinkcell-Thinkcell: 一款强大的专业图表制作工具
    在线数据库关系图工具
    tsql获取sqlserver某个库下所有表
    windows10 iis浏览wcf报404.3错误
  • 原文地址:https://www.cnblogs.com/TiePiHeTao/p/4822448a94c7c8191b7246b8e4680724.html
Copyright © 2011-2022 走看看