HiveQL:數據定義

Hive中的數據庫:本質上是表的一個目錄或命名空間,數據庫所在的目錄位於屬性hive.metastore.warehouse.dir所指定的頂層目錄之後,數據庫的文件目錄名是以.db結尾的。

hive在查詢的時候,如果表中的數據以及分區個數都非常大的話,執行這樣一個包含所有分區的查詢可能會觸發一個巨大的MapReduce任務。一個高度建議的安全措施就是將Hive設置爲"strict(嚴格)"模式,這樣如果對分區表進行查詢而where字句沒有加分區過濾的話,將會禁止提交這個任務。
set hive.mapred.mode=strict;
set hive.mapred.mode=nonstrict;

查看錶的詳細結構:
describe extended mydb.emp ;

查看錶的所有分區:
show partitions emp ;

查看錶的指定分區:
show partitions emp partition(state="US");

添加一個分區:
alter table emp add partition(year=2014,month=05,day=02) location 'hdfs://master.server/data/emp/2014/05/02';

自定義表的存儲格式:
記錄的解析是序列化器/反序列化器(或者簡寫爲SerDe)來控制的。對於TextFile,Hive所使用的SerDe是org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe的Java類。
Hive使用一個InputFormat對象將輸入流分隔成記錄,然後在使用一個OutputFormat對象來將記錄格式化爲輸出流,在使用一個SerDe在讀取數據時將記錄解析成列,在寫入數據時將列編碼成記錄。

用戶也可以使用指定的第三方的輸入和輸出格式以及SerDe:
create table kst
partitioned by (ds string)
row format serde 'com.linkedin.haivvreo.AvroSerDe'
with serdeproperties('schema.url'='http://schema_provider/kst.avsc')
stored as
inputformat 'com.linkedin.haivvreo.AvroContainerInputFormat'
outputformat 'com.linkedin.haivvreo.AvroContainerOutputFormat'; 

增加、修改及刪除表分區:
alter table emp add if not exists
partition (year=2014,month=05,day=01) location 'hdfs://master.server/data/emp/2014/05/01'
partition (year=2014,month=05,day=02) location 'hdfs://master.server/data/emp/2014/05/02'
partition (year=2014,month=05,day=03) location 'hdfs://master.server/data/emp/2014/05/03';

alter table emp partition(year=2014,month=05,day=01)
set location 'hdfs://master.server/data/mod/2014/05/01';

alter table emp drop if exists partition(year=2014,month=05,day=01);

增加、修改、刪除或者替換列:
添加新字段:
alter table emp add columns (
app_name string comment ''
sess_id long comment ''
);

修改hmn字段並將字段位置移動到serverity字段之後:
alter table emp change column
hmn hours_minites_seconds int comment '' 
after serverity ; 

移除表的所有字段並重新定義新的字段:
alter table emp replace columns (
hours_mins_secs int comment ''
serverity string comment ''
mess string comment ''
);

發佈了43 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章