一、Hive分區
分區表實際上就是對應一個HDFS文件系統上的獨立的文件夾,該文件夾下是該分區所有的數據文件。Hive中的分區就是分目錄,把一個大的數據集根據業務需要分割成小的數據集。在查詢時通過WHERE子句中的表達式選擇查詢所需要的指定的分區,這樣的查詢效率會提高很多。
1、分區表基本操作
1)引入分區表(需要根據日期對日志進行管理)
/user/hive/warehouse/log_partition/20180702/20180702.log
/user/hive/warehouse/log_partition/20180703/20180703.log
/user/hive/warehouse/log_partition/20180704/20180704.log
2)創建分區表語法
hive (default)> create table dept_partition(
deptno int, dname string, loc string
)
partitioned by (month string)
row format delimited fields terminated by '\t';
3)加載數據到分區表中
hive (default)> load data local inpath '/opt/mod/dept.txt' into table default.dept_partition partition(month='201907');
hive (default)> load data local inpath '/opt/mod/dept.txt' into table default.dept_partition partition(month='201908');
hive (default)> load data local inpath '/opt/mod/dept.txt' into table default.dept_partition partition(month='201909');
4)查詢分區表中數據
單分區查詢
hive (default)> select * from dept_partition where month='201907';
多分區聯合查詢
hive (default)> select * from dept_partition where month='201908'
union
select * from dept_partition where month='201907'
union
select * from dept_partition where month='201909';
_u3.deptno _u3.dname _u3.loc _u3.month
10 ACCOUNTING NEW YORK 201807
10 ACCOUNTING NEW YORK 201808
10 ACCOUNTING NEW YORK 201809
20 RESEARCH DALLAS 201807
20 RESEARCH DALLAS 201808
20 RESEARCH DALLAS 201809
30 SALES CHICAGO 201807
30 SALES CHICAGO 201808
30 SALES CHICAGO 201809
40 OPERATIONS BOSTON 201807
40 OPERATIONS BOSTON 201808
40 OPERATIONS BOSTON 201809
5)增加分區
創建單個分區
hive (default)> alter table dept_partition add partition(month='201806') ;
同時創建多個分區
hive (default)> alter table dept_partition add partition(month='201805') partition(month='201804');
注:增加多個分區之間用空格" "隔開,刪除多個分區用","隔開
6)刪除分區
刪除單個分區
hive (default)> alter table dept_partition drop partition (month='201804');
同時刪除多個分區
hive (default)> alter table dept_partition drop partition (month='201805'), partition (month='201806');
7)查看分區表有多少分區
hive>show partitions dept_partition;
8)查看分區表結構
hive>desc formatted dept_partition;
# Partition Information
# col_name data_type comment
month string
2 、分區表注意事項
1)創建二級分區表
hive (default)> create table dept_partition2(
deptno int, dname string, loc string
)
partitioned by (month string, day string)
row format delimited fields terminated by '\t';
2)正常的加載數據
(1)加載數據到二級分區表中
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition2 partition(month='201809', day='13');
(2)查詢分區數據
hive (default)> select * from dept_partition2 where month='201809' and day='13';
3)把數據直接上傳到分區目錄上,讓分區表和數據產生關聯的兩種方式
(1)方式一:上傳數據后修復
上傳數據
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201809/day=12;
hive (default)> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201809/day=12;
查詢數據(查詢不到剛上傳的數據)
hive (default)> select * from dept_partition2 where month='201809' and day='12';
執行修復命令
hive>msck repair table dept_partition2;
再次查詢數據
hive (default)> select * from dept_partition2 where month='201809' and day='12';
(2)方式二:上傳數據后添加分區
上傳數據
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201809/day=11;
hive (default)> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201809/day=11;
執行添加分區
hive (default)> alter table dept_partition2 add partition(month='201809', day='11');
查詢數據
hive (default)> select * from dept_partition2 where month='201809' and day='11';
(3)方式三:上傳數據后load數據到分區
創建目錄
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201809/day=10;
上傳數據
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table dept_partition2 partition(month='201809',day='10');
查詢數據
hive (default)> select * from dept_partition2 where month='201809' and day='10';
五、DML數據操作
續上一篇,多以是五~
5.1 數據導入
5.1.1 向表中裝載數據(Load)
1)語法
hive>load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student [partition (partcol1=val1,…)];
(1)load data:表示加載數據
(2)local:表示從本地加載數據到hive表;否則從HDFS加載數據到hive表
(3)inpath:表示加載數據的路徑
(4)overwrite:表示覆蓋表中已有數據,否則表示追加
(5)into table:表示加載到哪張表
(6)student:表示具體的表名
(7)partition:表示上傳到指定分區
2)實操案例
(0)創建一張表
hive (default)> create table student(id string, name string) row format delimited fields terminated by '\t';
(1)加載本地文件到hive
hive (default)> load data local inpath '/opt/module/datas/student.txt' into table default.student;
(2)加載HDFS文件到hive中
上傳文件到HDFS
hive (default)> dfs -put /opt/module/datas/student.txt /user/AncientMing/hive;
加載HDFS上數據
hive (default)>load data inpath '/user/AncientMing/hive/student.txt' into table default.student;
(3)加載數據覆蓋表中已有的數據
上傳文件到HDFS
hive (default)> dfs -put /opt/module/datas/student.txt /user/AncientMing/hive;
加載數據覆蓋表中已有的數據
hive (default)>load data inpath '/user/AncientMing/hive/student.txt' overwrite into table default.student;
注:load hdfs的數據相當于mv文件到另一個目錄中,原目錄文件消失
5.1.2 通過查詢語句向表中插入數據(Insert)
1)創建一張分區表
hive (default)> create table student(id int, name string) partitioned by (month string) row format delimited fields terminated by '\t';
2)基本插入數據
hive (default)> insert into table student partition(month='201907') values(1,'wangwu');
3)基本模式插入(根據單張表查詢結果)
hive (default)> insert overwrite table student partition(month='201908')
select id, name from student where month='201907';
4)多插入模式(根據多張表查詢結果)
hive (default)> from student
insert overwrite table student partition(month='201909')
select id, name where month='201907'
insert overwrite table student partition(month='201910')
select id, name where month='201908';
5.1.3 查詢語句中創建表并加載數據(As Select)
詳見4.5.1章創建表。
根據查詢結果創建表(查詢的結果會添加到新創建的表中)
create table if not exists student3
as select id, name from student;
這種方式不能創建外部表。
external
CREATE-TABLE-AS-SELECT cannot create external table
5.1.4 創建表時通過Location指定加載數據路徑
1)創建表,并指定在hdfs上的位置
hive (default)> create table if not exists student5(
id int, name string
)
row format delimited fields terminated by '\t'
location '/user/hive/warehouse/student5';
2)上傳數據到hdfs上
hive (default)> dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student5;
3)查詢數據
hive (default)> select * from student5;
5.1.5 Import數據到指定Hive表中
注意:先用export導出后(導出的數據目錄里面附帶有元數據),再import數據導入。同在HDFS上是Copy級操作
hive (default)> export table default.student to '/user/hive/warehouse/export/student';
hive (default)>create table student5(
> id int, name string
> )
> partitioned by (month string)
> row format delimited fields terminated by '\t';
hive (default)> import table student5 from '/user/hive/warehouse/export/student';
5.2 數據導出
5.2.1 Insert導出
1)將查詢的結果導出到本地,數據之間無間隔
hive (default)> insert overwrite local directory '/opt/module/datas/export/student'
select * from student;
2)將查詢的結果格式化導出到本地,數據之間"\t"間隔
hive (default)> insert overwrite local directory '/root/student2'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student;
3)將查詢的結果導出到HDFS上(沒有local)
hive (default)> insert overwrite directory '/user/AncientMing/student2'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student;
注:雖然同是HDFS,但不是copy操作
5.2.2 Hadoop命令導出到本地
hive (default)> dfs -get /user/hive/warehouse/student/month=201809/000000_0 /opt/module/datas/export/student3.txt;
5.2.3 Hive Shell 命令導出
基本語法:(hive -f/-e 執行語句或者腳本 > file(自己創建))
[AncientMing@bigdata111hive]$ bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student4.txt;
5.2.4 Export導出到HDFS上
hive (default)> export table default.student to '/user/hive/warehouse/export/student';
5.3 清除表中數據(Truncate)
注意:Truncate只能刪除管理表,不能刪除外部表中數據
hive (default)> truncate table student;