内部表
内部表(Table)
-與資料庫的Table在概念上類似
-每一個Table在Hive中都有一個相應的目錄(HDFS上的目錄)存儲資料
-所有的Table資料(不包括External Table)都儲存在這個目錄(HDFS目錄)中
-表的中繼資料資訊,存儲在中繼資料資料庫中(mysql)
-删除表後,中繼資料和資料都會被删除
建立表--案例:
create table t1
(t1 int, tname string, age int);
在hive中建立一張表,如果不指定表所儲存的位置,那麼這張表會建立在HDFS檔案系統中的/user/hive/warehouse目錄下
create table t2
(tid int, tname string, age int)
location '/mytable/hive/t2';
指定表的位置為HDFS中的/mytable/hive/t2
create table t3
(tid int, tname string, age int)
row format delimited fields terminated by ',';
表示以csv檔案格式存儲,因為csv存儲的分隔符為逗号
//row format 指定表示行的格式
加入資料--案例:
create table t4
as
select * from sample_data;
//采用sample_data查詢的集合來建立t4表
//檢視HDFS中的檔案發現,t4表中資料與資料之間沒有分隔符
這裡我們同樣可以指定分隔符:
create table t4
row format delimited fields terminated by ','
as
select * from sample_data;
//采用sample_data查詢的集合來建立t5表,并以','為分隔符
在一張表上加入新的列---案例:
alter table t1 add columns(english int);
删除一張表--案例:
drop table t1;
//當删除一張表時,它會把對應的檔案放入HDFS的資源回收筒中,是以删除之後
//我們可以利用一定的方式恢複表中的資料
分區表
分區表(Partition):
(可以提高查詢的效率)
-Partition對應于資料庫Partiton列的密集索引
-在Hive中,表中的一個Partition對應于表下的一個目錄,所有的Partition的資料都存儲在對應的目錄中
建立表--案例
create table partition_table
(sid int, sname string)
partitioned by (gender string)
row format delimited fields terminated by ',';
//建立一張以','分隔,以性别進行分區的分區表partition_table
insert into table partition_table partition(gender = 'M') select sid,sname from sample_data where gender = 'M';
//将sample_data表中,gender為'M'的行資料,插入到paetition_table表中gender為'M'的分區中
insert into table partition_table partition(gender = 'F') select sid,sname from sample_data where gender = 'F';
//将sample_data表中,gender為'F'的行資料,插入到paetition_table表中gender為'F'的分區中
外部表
外部表(External Table) -指向已經在HDFS中存在的資料,可以建立Partition -它和内部表在中繼資料的組織上時相同的,而實際存儲則有極大的差異 -外部表隻有一個過程,加載資料和建立表同時完成,并不會移動到資料倉庫目錄中,隻會與外部資料建立一個連結,當删除該表時,僅删除該連結而不删除實際的資料
外部表建立--案例
create external table external_student
(sid int, sname string, age int)
row format delimited fields terminate
location '/input';
//建立一個以','為分隔符的外部表,這個外部表與HDFS中/input目錄下的檔案相關聯
桶表
桶表(Bucket Table)
桶表是對資料進行哈希取值,然後放到不同檔案存儲。也就是說,桶表中的資料,是通過哈希運算後,将其打散,再存入檔案當中,這樣做會避免造成熱塊,進而提高查詢速度。
桶表建立--案例
create table bucket_table
(sid int, sname string, age int)
clustered by (sname) into 5 buckets;
//建立一個桶表,這個桶表是以sname作為哈希運算,運算後的結果放到5個桶中
後記:網課筆記