hive學習第三章:DDL數據定義

創建數據庫

  1. 創建一個數據庫,數據庫在hdfs上的默認路徑是/user/hive/warehouse/*.db
  2. 避免要創建的數據庫已經存在錯誤,增加if not exist判斷:create database if not exists db_hive
  3. 創建一個數據庫,指定數據庫在hdfs上存放的位置:create database db_hive2 location '/db_hive2.db'

說明:在hdfs下創建一個文件夾:hadoop fs -mkdir -p /database/hive3

查詢數據庫

  1. 顯示數據庫:show databases
  2. 過濾顯示查詢的數據庫:show databases like 'db_hive'
  3. 顯示數據庫信息:desc database db_hive
  4. 顯示數據庫詳細信息:desc database extended db_hive
  5. 切換當前數據庫:use db_hive
  6. 修改數據庫:alter database db_hive set dbproperties('createtime'='20170830'),說明:數據庫的其他元數據信息都是不可以更改的,包括數據庫名和數據庫所在的目錄位置
  7. 刪除數據庫:drop database db_hive2
  8. 如果刪除的數據庫不存在,最好採用if exists判斷數據庫是否存在:drop database if exists db_hive2
  9. 如果數據庫不爲空,可以採用cascade命令,強制刪除:drop database db_hive cascade

創建表

  • 建表語法
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...)]
[SORTED BY (col_name [ASC|DESC], ...)] INFO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]

字段解釋說明

  1. CREATE TABLE 創建一個指定名字的表
  2. EXTERNAL關鍵字可以讓用戶創建一個外部表,在建表的同時指定一個實際路徑(LOCATION),hive創建內部表時,會將數據移動到數據倉庫指向路徑;若創建外部表,僅記錄數據所在的路徑,不對數據的位置做任何改變。在刪除表的時候,內部白哦的元數據和數據會被遺棄刪除,而外部表只是刪除元數據,不刪除數據
  3. COMMENT爲表和列添加註解
  4. PARTITIONED BY創建分區表
  5. CLUSTERED BY創建分桶表
  6. SORTED BY不常用
  7. STORED AS指定存儲文件類型:SEQUENCEFILE(二進制序列文件)、TEXTFILE(文本)、RCFILE(列式存儲格式文件),如果文件數據是純文本,可以使用STORED AS TEXTFILE,如果數據需要壓縮,使用STORED AS SEQUENCEFILE
  8. LOCATION:指定表在hdfs上的存儲位置
  9. LIKE允許用戶複製現有的表結構,但不復制數據
  • 管理表
  1. 理論:默認創建的表都爲管理表,有時候也被稱爲內部表,hive會(或多或少)控制着數據的生命週期。hive默認情況下會將這些表的數據存儲在由配置項hive.metastore.warehouse.dir(如:/user/hive/warehouse)所定義的目錄的子目錄下,當刪除一個管理表時,hive也會刪除這個表中數據。管理表不適合和其他工具共享數據
  2. 創建表、根據查詢結果創建表
    //1.普通創建表
    create table if not exists student2(
    id int, name string
    )
    row format delimited fields terminated by '\t'
    stored as textfile
    location '/user/hive/warehouse/student2'
    
    //2.根據查詢結果創建表(查詢的結果會添加到新創建的表中)
    create table if not exists student3 as select id, name from student

     

  • 外部表
  1. 理論:刪除該表並不會刪除掉這份數據,不過描述表的元數據信息會被刪除掉
  2. 管理表和外部表的使用場景:每天將收集到的網站日誌定期流入hdfs文件,在外部表的基礎上做大量的統計分析,用到的中間表、結果表使用內部表存儲,數據通過SELECT+INSERT進入內部表
  • 管理表與外部表的互相轉換
  1. 查詢表的類型:desc formatted student2
  2. 修改內部表student2爲外部表:alter table student2 set tblproperties('EXTERNAL'='TRUE')
  3. 修改外部表student2爲內部表:alter table student2 set tblproperties('EXTERNAL'='FALSE')

分區表

分區表實際上就是對應一個hdfs文件系統上的獨立的文件夾,該文件夾下式該分區所有的數據文件。hive中的分區就是分目錄,把一個大的數據集根基業務需要分割成小的數據集。在查詢時通WHERE子句中的表達式選擇查詢所需要的指定的分區,這樣的查詢效率會提高很多

  •  分區表的基本操作
  1. 引入分區表,需要根據日期對日誌進行管理
    /user/hive/warehouse/log_partition/20200401/20200401.log
    /user/hive/warehouse/log_partition/20200402/20200402.log
    /user/hive/warehouse/log_partition/20200403/20200403.log
    

     

  2. 創建分區表語法
    create table dept_partition(
    deptno int, dname string, loc string
    )
    partitioned by (month string)
    row format delimited fields terminated by '\t';

     

  3. 加載數據到分區表中

    load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='20200401')
    load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='20200402')
    load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='20200403')

     

  4. 查詢分區表中的數據

    //1.單分區查詢
    select * from dept_partition where month='20200401'
    
    //2.多分區聯合查詢
    select * from dept_partition where month='20200401' union
    select * from dept_partition where month='20200402' union
    select * from dept_partition where month='20200403'

     

  5. 增加分區

    //1.創建單個分區
    alter table dept_partition add partition(month='20200404')
    
    //2.同時創建多個分區
    alter table dept_partition add partition(month='20200404') partition(month='20200405') 

     

  6. 刪除分區

    //1.刪除單個分區
    alter table dept_partition drop partition(month='20200401')
    
    //2.同時刪除多個分區
    alter table dept_partition drop partition(month='20200401'), partition(month='20200402')

     

  7. 查看分區表有多少分區

    show partitions dept_partition

     

  8. 查看分區表結構

    desc formatted dept_partition

     

  • 分區表注意事項
  1. 創建二級分區表
    create table dept_partition2(
    deptno int, dname string, loc string
    )
    partitioned by (month string, day string)
    row format delimited fields terminated by '\t';

     

  2. 正常的加載數據
    //1.加載數據到二級分區表中
    load data local inpath 'opt/module/datas/dept.txt' into table default.dept_partition2 partition(mobth='202004', day='13');
    
    //2.查詢分區數據
    select * from dept_partition2 where month='202004' and day='13';

     

  3. 把數據直接上傳到分區目錄上,讓分區表和數據產生關聯的三種方式
    //1.上傳數據後臺修復
    //a.上傳數據
    dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=202004/day=13;
    dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=202004/day=13;
    
    //b.查詢數據(查詢不到剛上傳的數據)
    select * from dept_partition2 where month='202004' and day='13'
    
    //c.執行修復命令
    msck repair table dept_partition2;
    
    //d.再次查詢數據
    select * from dept_partition2 where month='202004' and day='13'
    
    
    //2.上傳數據後添加分區
    //a.上傳數據
    dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=202004/day=13;
    dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=202004/day=13;
    
    //b.執行添加分區
    alter table dept_partition2 add partition(month='202004', day='13');
    
    //c.查詢數據
    select * from dept_partition2 where month='202004' and day='13';
    
    
    //3.創建文件後load數據到分區
    //a.創建目錄
    dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=202004/day=13;
    
    //b.上傳數據
    load data local inpath '/opt/module/datas/dept.txt' into table dept_partition2 partition(month='202004', day='13');
    
    //c.查詢數據
    select * from dept_partition2 where month='202004' and day='13';

     

修改表 

  • 重命名錶
    ALTER TABLE table_name RENAME TO new_table_name
    
    //如:
    alter table dept_partition2 rename to dept_partition3

     

  • 增加、修改和刪除分區:見以上分區表
  • 增加/修改/替換列信息
    //1.語法
    //a.更新列
    alter table table_name change [column] col_old_name col_new_name column_type [comment col_comment] [first|alter column_name] 
    
    //b.增加和替換列
    alter table table_name add|replace columns (col_name data_type [comment col_comment], ...)
    
    
    //2.練習
    //a.查詢表結構
    desc dept_partition
    
    //b.添加列
    alter table dept_partition add columns(deptdesc string)
    
    //c.更新列
    alter table dept_partition change column deptdesc desc int
    
    //d.替換列
    alter table dept_partition replace columns(deptno string, dname string, loc string)
    

    說明:ADD是代表新增一字段,字段位置在所有列後面(partition列前),replace則是表示替換表中的所有字段

 刪除表

drop table dept_partition

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章