Hive操作指南


一. 建外部表

create external  table test.test_external(
                id int comment '測試id' , 
                name string comment '測試名稱'  )                           
comment '測試'
row format delimited fields terminated by '\t'
stored as textfile
location '/user/hive/warehouse/external';


二. 建表

create table test.test(
                id int comment '測試id' , 
                name string comment '測試名稱'  )                           
comment '測試'
row format delimited fields terminated by '\t'
stored as textfile;

備註: hive常用存儲文件類型有textfile、parquet、ocr等


三. 建分區表

create table test.test(
                id int comment '測試id' , 
                name string comment '測試名稱'  )                           
comment '測試'
partitioned by (dt string comment '分區日期')
row format delimited fields terminated by '\t'
stored as textfile;


四. 建分區分桶表

create table test.test(
    id int comment '測試id' , 
    name string comment '測試名稱'  )                           
comment '測試'
partitioned by (dt string comment '分區日期')
clustered by (id) into 5 buckets
row format delimited fields terminated by '\t'
stored as ocr;

五. 更改表名

ALTER TABLE test RENAME TO new_name;

六. 添加列

ALTER TABLE test ADD COLUMNS (col_spec[, col_spec ...])

備註:
 ALTER TABLE test ADD COLUMNS (col_spec[, col_spec ...]) CASCADE;// 加分區表字段需要加上CASCADE ,否則歷史分區數據無法被重新寫入數據,即便指定該新增列。


七. 更改列名相關

ALTER TABLE test CHANGE column_name new_name new_type;


八. 刪除指定列age(原表共有id,name,age列)

ALTER TABLE test REPLACE COLUMNS (id bigint ,name string)

備註:
ALTER TABLE name DROP [COLUMN] column_name     //  DROP column_name爲無效語法

九. 複製表結構
① 非分區表:

create table new_table as select * from exists_table where 1=0;

② 分區表:

create table text2 like test;

十. 複製表結構和數據
① 非分區表: 

create table new_table as select * from exists_table;

② 分區表:
方法一:

1.  create table test2 like test;

 注意: 如果使用as在分區表基礎上,創建新表,那麼得到的爲非分區表,得到原表完整結構要用like。
2. hive cmd模式(將HDFS的數據文件複製一份到新表目錄):   

 dfs -cp -f /user/hive/warehouse/test.db/test/* /user/hive/warehouse/test.db/test2/

3. hive cmd模式(修復元數據信息):   

MSCK REPAIR TABLE test2;

方法二:

1. create table test2 like test;

動態分區:

2. insert overwrite table test2 partition (dt) select id,name,dt from test;

十一. 刪除表

drop table dbname.tname;

十二. 清空表數據
① 內部表(保留表結構):

truncate table dbname.tname;

② 外部表(保留表結構):
1. 進入hue hdfs指定分區刪除
2. 用shell腳本刪除

#!/bin/bash
temp=$(date +%Y-%m-%d)
temp2=$(date -d "-1 day" +%Y-%m-%d)
hdfs dfs -rm -r /user/hive/test_table/partition_date=${temp}

 

Hive創建內外部表    Hive 複製分區表和數據
Hive修改表1    Hive修改表2    Hive表刪除&清空數據

 

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