hive 創建和刪除庫、表

前言

本文主要介紹hive 中操作庫和表的語句。

hive 庫操作

  1. 查看數據庫:show databases
  2. 創建數據庫:create database if not exists hive_testdb
  3. 使用某個數據庫:use hive_testdb
  4. 查詢數據庫:show databases like ‘hive*’
  5. 顯示數據庫的信息:describe database hive_testdb
  6. 添加數據庫備註:create database teacherdb comment
  7. 刪除空數據庫:drop database hive_testdb
  8. 強制刪除數據庫:drop database hive_testdb casecade

hive表操作

  1. 查看錶的列表:show tables
  2. 創建表:create table if not exists hive_db.table_test(id string, name string, age int, …)
  3. 刪除表:drop table if exists table_test
  4. 重命名錶:alter table table_test rename to table1
  5. 修改表中列信息:alter table table_test change columns id tb_id int
  6. 增加列:alter table table_test add columns(class string commet “班級”)
  7. 刪除或者替換列:alter table table_test replace columns(id string commet “備註”,name string commet “備註”);//這裏是將所用列全部刪除後再新建id和name列
  8. 複製表及數據:create table table_test1 as select * from table_test
  9. 導入表數據:load data local inpath ‘xxx(數據目錄)/data/xxx.xxx’ overwrite into table hive_db.table_test
  10. 查詢表:show tables in hive_db like “table*”
  11. 查看錶信息:desc table_test
  12. 內部表與外部表的相互轉換:
    alter table table_test set tblproperties(“EXTERNAL”=“TRUE”)  //內部錶轉換爲外部表
    alter table table_test set tblproperties(“EXTERNAL”=“FALSE”)  //外部錶轉換爲內部表
  13. 分區表(物理分區,邏輯上還是整表)
    創建分區表(按照月份來分區):create table table_partition(id string,name string) partitioned by (month string) row format delimited fields terminated by ‘\t’
    上傳數據到分區表:load data local inpath ‘xxx(數據目錄)/data/xxx.xxx’ into table table_partition partition(month=“201909”)
    查找分區表:select * from table_partition //查找分區表中的所有記錄
    select * from table_partition where month=“201909”  //查找分區表中分區名201909中的所有記錄
    查看分區:show partitions table_partition
    增加分區:alter table table_partition add partition (month=“201910”)
    alter table table_partition add partition (month=“201911”), partition (month=“201912”)
    刪除分區:alter table table_partition drop partition(month=“201909”)
    ps:二級分區指的是2個分區字段,按照字段的順序來設置分區順序,比如:partition(month=“201909”,day=“01”)就是一個二級分區,目錄結構中day是month文件夾的子文件夾。

參考博客

hive的數據定義之創建數據庫和表 - hdc520 - 博客園

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