大資料(二十):hive分割槽表、修改表語句與資料的匯入匯出
阿新 • • 發佈:2018-11-10
一、分割槽表
分割槽表實際上就是對應一個HDFS檔案系統上的一個獨立的資料夾,該資料夾下是該分割槽所有的資料檔案,hive中的分割槽就是分目錄,把一個大的資料集更具業務需求分割成小的資料集。在查詢時通過where子句中的表示式選擇查詢所需要的指定分割槽,這樣查詢效率會提高很多。
1.建立分割槽表
create table dept_partition( deptno int, dname string, loc string ) partitioned by (month string) row format delimited fields terminated by '\t';
2.匯入資料
load data local inpath '/opt/datas/dept.txt' into table dept_partition partition (month ='201809')
3.選擇分割槽查詢
select * from dept_partition where month='201809';
4.多分割槽聯合查詢
select * from dept_partition where month='201809' union select * from dept_partition where month='201808';
5.新增分割槽
alter table dept_partition add partition(month='201810');
6同時增加多個分割槽(兩個之間只有空格,沒有任何連線符)
alter table dept_partition add partition(month='201811') partition(month='201812');
7.刪除分割槽
alter table dept_partition drop partition(month='201812');
8.同時刪除多個分割槽(中間有逗號分隔,與增加不同)
alter table dept partition drop partition(month='201811'),partition(month='201810');
9.檢視有多少分割槽
show partition dept_partition;
10.檢視分割槽表結構
desc formatted dept_partition;
11.建立二級分割槽
create table dept_partition2(
deptno int,
dname string,
loc string
)
partitioned by(month string,day string)
row format delimited fields terminated by '\t';
12.二級分割槽匯入資料
load data local inpath '/opt/datas/dept.txt' into table dept_partition partition (month ='201809',day='123');
二、分割槽資料關聯的三種方式
1.正常的載入資料
load data local inpath '/opt/datas/dept.txt' into table dept_partition partition(month='201809',day='123');
2.上傳資料後修復
建立目錄和上傳資料(hive客戶端)
dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201810/day=12;
dfs -put /user/hive/warehouse/dept_partition2/month=201810/day=12;
執行修復命令
msck repair table dept_partition2;
3.上傳資料後新增分割槽
dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201810/day=12;
dfs -put /user/hive/warehouse/dept_partition2/month=201810/day=12;
新增分割槽
alter table dept_partition2 add partition(month='201810',day='12');
三、修改表
1.修改表名
ALTER TABLE table_name RENAME TO new_table_name
2.更新列
ALTER TABLE table_name CHANGE [COLUMN] col_old_name column_type [COMMENT col_comment][FIRST|AFTER colummn_name]
3.增加和替換列
ALTER TABLE table_name ADD|REPLACE COLUMNS(col_name data_type[COMMENT col_comment],...)
注:ADD是代表增加一欄位,欄位位置在所有列後面(partition列前),REPLACE則是表示替換表中所有欄位。
四、資料匯入
1.向表中裝載資料
load data [local] inpath '/opt/datas/student.txt' [overwrite] into table student [partition(partcol1=val1,...)];
- load data:表示載入資料
- local:表示從本地載入資料到hive表;否則從HDFS載入資料到hive
- inpath:表示載入資料的路徑
- overwrite:表示覆蓋表中已有資料,否則表示追加
- into table:表示載入到哪張表
- student:具體的表名
- partition:表示上傳的指定分割槽
2.通過查詢語句插入資料
基本插入資料
insert into table student partition(month='201809') values('1001','wangwu');
基本模式插入(根據單張表查詢結果插入)
insert overwrite table student partition(month='201809')select id,name from student where month='201808';
3.Import匯入資料到指定Hive表中(必須是用export匯出的資料)
import table student2 partition (month='201809') from '/opt/datas/export/student'
五、資料匯出
1.將查詢結果匯出到本地
insert overwrite local directory '/opt/export/student' select * from student;
2.將查詢的結果格式化匯出到本地
insert overwrite local directory '/opt/export/student1' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY '\n' select * from student;
3.將查詢的結果匯出到HDFS上(去掉local就行了)
insert overwrite directory '/opt/export/student1' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY '\n' select * from student;
4.通過hadoop命令匯出到本地
dfs -get /suer/hive/warehouse/student/month=201809/000000_0 /opt/datas/export/student.txt
5.Export匯出到HDFS上
export table default student to '/opt/datas/export/student';
六、清除表中資料
truncate table student;
注意:只能刪除管理表中的資料,無法刪除外部表中的資料