創建表
CREATE TABLE if not exists student ( student_id int, sex int, address String, email String )
這里需要注意的是,雖然hiveSL類似sql語言,但是他們有很多需要注意的地方,不能直接使用符號,比如“'”,“;”這些,分號,hdfs認為是結束的符號。要使用這些符號的話,需要寫成他們ask碼的形式
還有在創建表時,字段的數據類型,不是使用的sql中的類型,類似java中的類型
create table:創建一個指定名字的表
external 關鍵字:創建一個外部表,在創建一個外部表時需要給他指定一個指向真實數據的路徑(local),在創建一個內部表時,數據會隨着你指向的路徑兒移動,而創建外部表僅僅記錄數據的路徑;當刪除內部表時:內部表的數據和元數據會隨着一起被刪除,而刪除外部表,就只刪除元數據。
like:格式修飾的create table允許復制一個已經存在的表,但是只是復制表的結構,而不復制表的數據
添加表分區
create table student_1( student_id int, sex int comment 'sex:0 gg,1 man', address String comment 'address is student Faily address', email String ) partitioned by(ds String,country String)//指定分區,按照地市和時間進行分區,此並沒有真正的存儲列,也就是此列不存在你的數據中 row format delimited //設置創建表在加載數據的列分割符 fields terminated by '\001'//分割列 stored as sequencefile//需要壓縮數據
row format delimited:用戶在建表的時候可以自定義 SerDe 或者使用自帶的 SerDe。如果沒有指定
ROW FORMAT 或者
ROW FORMAT DELIMITED,將會使用自帶的 SerDe。在建表的時候,用戶還需要為表指定列,用戶在指定表的列的同時也會指定自定義的 SerDe,Hive 通過 SerDe 確定表的具體的列的數據。
添加聚類存儲
create table student_2( student_id int, sex int comment 'sex:0 gg,1 man', address String comment 'address is student Faily address', email String ) comment 'student_2 table view' partitioned by(ds String,country String) clustered by(student_id) sorted by (student_id) into 32 buckets//根據sutdent_id進行分區並存儲,並對student_id進行排序並放到32個桶中 row format delimited fields terminated by '\001' stored as sequencefile
這樣組織結構允許用戶通過student_id高效的對集群中的列進行采樣
修改表的語句 alert(對於已經存在的表)
alert table student_2 to student_3
指定存儲路徑
create table student_2( student_id int, sex int comment 'sex:0 gg,1 man', address String comment 'address is student Faily address', email String ) comment 'student_2 table view' partitioned by(ds String,country String) clustered by(student_id) sorted by (student_id) into 32 buckets//根據sutdent_id進行分區並存儲,並對student_id進行排序並放到32個桶中 row format delimited fields terminated by '\001' stored as sequencefile location '<hdfs路徑>' //表數據存在hdfs上目錄,也可以是本地目錄
注意:修改表的結構只會對元數據做改變,不會改變數據,用戶應該確保元數據的定義和數據結構的一致性
增加和更新列
alert table tableName add|replace colunms(col_name col_type col_moent,.......)
add columns,允許用戶在當前列的末尾,未分區之前增加新的列。replace columns刪除當前列,增加新的列。只有在
使用native的serDE在能這么做