Hive 的DDL和DML

本文是跟着這個學習的 https://www.bilibili.com/video/av65556024?p=1

DDL

1.庫

建庫:

> create database if not exists 庫名; 

還有一個方式:

> create database if not exists 庫名 location 路徑; 

指定hdfs路徑

查看數據庫:

> show databases;

看數據庫信息:

> desc databases 庫名;

想多看點:

> desc databases extended 庫名;

改庫:(數據庫名和數據庫目錄位置無法修改)

> alter database 庫名 set ...

刪庫:(想跑路?)
空庫:

> drop database if exists 庫名;

非空庫:

> drop database 庫名 cascade;

2.表

建表:

> create [external] table [if not exist] table_name [(col_name data_type [comment col_comment],...)]
[comment table_comment]
[partitioned by (col_name data_type[comment col_comment],...)
[clustered by (col_name,col_name...)
[sorted by (col_name [asc|desc],...)] into num_buckets buckets]
[row format row_format]
[stored as file_format]
[Location hdfs_path]

語句特別長,中括號裏面的不是必要的,但是大多數情況下的建表都是分區表。
1.create table 就是建表基礎。
2.external:關鍵字是可以創建於一個外部表,建表同時指定一個指向實際數據的路徑(location)。
hive創建內部表時,會將數據移動到數據倉庫指向的路徑;
如果創建外部表,僅記錄數據所在的路徑,不對數據的位置做任何改變。
區別就在於刪除表的時候。內部表元數據和數據都會被刪除,外部表只刪除元數據,不刪除數據。
3.comment:添加註釋(對錶、對列)
4.partitioned by:創建分區表
5.clustered by:創建分桶表
6.sorted by : 排序,基本不用
7.row format:數據切分形式
8.stored as :指定文件存儲類型
9.location:指定表在hdfs的位置
10.like :允許用戶複製現有變結構,但不復制數據

我們使用create table的時候,基本上hive幫我們補充了其他參數的默認值。

管理表

默認創建的表都是管理表,有時候也稱爲內部表。這種表,hive會控制數據的生命週期,我們刪除表的時候,hive也會刪除表中的數據。
外部表更安全一些,畢竟數據是分開的。刪表之後數據會保留。

查詢表的類型

> desc formatted 表名

能發現Table_type,是表的類型。可以修改

內部表改爲外部表

> alert table 表名 set tblproperties ('EXTERNAL'='TRUE')

外部表改爲內部表

> alert table 表名 set tblproperties ('EXTERNAL'='FALSE')

MANAGED_TABLE是內部表,EXTERNAL_TABLE是外部表。
注意!!!(‘EXTERNAL’=‘TRUE’)和 (‘EXTERNAL’=‘FALSE’)這個要區分大小寫!!!

3.分區

分區的意義:分區表實際上來說,就是對應HDFS文件系統上獨立的文件夾,該文件夾下是該分區所有數據文件。Hive中的分區就是分目錄,把一個大的數據集根據業務需要,分割成小的數據集。在查詢的時候,通過where子句中的表達式,選擇查詢所需要的指定分區。提高查詢效率(謂詞下推)。

3.1 引入分區表(根據日期對日誌進行管理)

/user/hive/warehouse/log_partition/20191028/20191028.log
/user/hive/warehouse/log_partition/20191029//20191029.log
/user/hive/warehouse/log_partition/20191030//20191030.log

3.2 創建分區表

> create table dept_partition(deptno int,dname string,loc string) 
partitioned by (month string)
row format delimited fields terminated by '\t';

3.3 加載數據到分區表中

> load data local inpath '/mnt/hgfs/shareOS/dept.txt' into table dept_partition partition(month='2019-06');
> load data local inpath '/mnt/hgfs/shareOS/dept.txt' into table dept_partition partition(month='2019-07');

看到生成了兩個文件,select的時候發現,查詢的是兩個文件和並的數據。並且幫我們自動添加了month,並且它可以作爲查詢條件。這個條件區分了文件夾,查詢的時候會被先訪問。
我們去mysql看元數據。PARTITION表,增加了兩條數據。這個記錄了我們的分區。

3.4 查詢分區表中數據

單分區查詢:

> select * from dept_partition where month = '2019-06';

多分區聯合查詢:

> select * from dept_partition where month = '2019-06' 
    union
        select * from dept_partition where month = '2019-07' 
            union
                select * from dept_partition where month = '2019-08' ;

3.5 增加分區

創建單個分區:

> alert table dept_partition add partition(month=''2019-09);

多個分區:

> alert table dept_partition add partition(month=''2019-10) partition(month=''2019-11);

3.6 刪除分區

> alter table dept_partition drop partition (month='2019-10');

刪多個:

> alter table dept_partition drop partition (month='2019-10'),partition (month='2019-11');

3.7 查看分區表有多少個分區

> show partitions dept_partition;

3.8 分區表結構查看

> desc formatted dept_partition;

多了一個分區字段信息。把分區字段當普通字段使用就可以了。

4.分區表注意事項

4.1 二級分區

> create table dept_partition2(deptno int,dname string,loc string) 
partitioned by (month string,day string)
row format delimited fields terminated by '\t';

4.2 添加的時候,兩個也都要填。

> load data local inpath '/mnt/hgfs/shareOS/dept.txt' into table dept_partition partition(month='2019-06',day='10');

4.3 把數據直接上傳到分區目錄上,讓分區表和數據產生關聯的三種方式。

(1)上傳數據之後修復

> msck repair table dept_partition;

(2)上傳數據之後添加分區

> alter table dept_partition add partition(month'2019-11');

(3)創建文件夾之後load數據到分區。
load添加了分區信息,上傳了數據,問題直接就解決了。

五.修改表

5.1 改表名

> alter table 表名 RENAME TO new_table_name;

5.3 增加、修改、替換列信息

更新列:

> alter table 表名 change [COLUMN] col_old_name col_new_name 
column_type [COMMENT col_comment] [FIRST|AFTER column_name]

增加和替換列:

> alter table 表名 add|replace COLUMNS 
(col_name data_type [COMMENT col_comment],...)

ADD是增加一個字段,REPLACE是替換表中所有字段。

接下來是部分DML

一.數據導入

###1.1.1 向表中裝載數據(Load)
語法:

> load data [local] inpath '/opt/soft/文件.txt' overwrite|into 
table 表名 [partition(partcol1=val1,...)];

load data:加載數據
local:表示從本地加載到hive表;否則從HDFS加載到Hive
inpath:加載數據路徑
overwrite:表示覆蓋表中已有數據,否則表示追加。
into table:表示加載到哪張表
partition:上傳到指定分區

1.1.2 通過查詢語句插入數據(insert)

> insert into table 表名 partition(month='2019-06') value (1,'zahngsan');

into也可以變成overwrite,基本模式插入。
還有多插入模式之類的。。。不過不常用。

1.1.3 查詢語句中創建並加載數據(As Select)

1.創建一張分區表

例如:

> create table if not exist 表名(id int,name string) partitioned by 
(month string) row format delimited fields terminated by '\t';

2.基本插入數據

例如:

> insert into table student partition(month='2019-07') values(1,'wangwu');

3.基本模式插入(覆蓋)
例如:

> insert overwrite table student partition(month='2019-07') values(1,'wangwu');

二.數據導出

2.1 Insert導出

1.結果導出到本地

> insert overwrite local directory '/opt/module/datas/export/student'
select * from student;

哎,由於我虛擬機分配內存比較小,系統慢的要死!讓我一度以爲集羣搭建的有問題!!!

2.將結果格式化導出到本地

>insert overwrite local directory '/opt/module/datas/export/student'  
row format delimited fields terminated by '\t'
select * from student;

3.導出到hdfs

>insert overwrite directory '/dept'  
row format delimited fields terminated by '\t'
select * from student;

2.2 Hadoop導出
2.3 Export導出到HDFS
2.4 SQOOP導出
MySQL 與 Hive(HDFS) 之間導來導去。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章