Hive~庫表操作DDL

1.庫DDL

創建庫 

create database test;
create database if not exists test;
create database if not exists test location '/hive'; # 自定義存儲目錄

查詢庫

show databases;

庫詳情信息

desc database test;
desc database extended test;

切換庫

use test;

修改庫

alter database test set dbproperties('createtime'='20200503');

刪除庫

#空數據庫
drop database test;
#非空數據庫
drop database test cascade;

2.hive的數據類型

基本數據類型

Hive 數據類型

Java 數據類型

長度

例子

TINYINT

byte

1byte 有符號整數

20

SMALINT

short

2byte 有符號整數

20

INT

int

4byte 有符號整數

20

BIGINT

long

8byte 有符號整數

20

BOOLEAN

boolean

布爾類型,true 或者 false

TRUE  FALSE

FLOAT

float

單精度浮點數

3.14159

DOUBLE

double

雙精度浮點數

3.14159

STRING

string

字符系列。可以指定字符集。可以使用單引號或者雙引號。

‘now is the time’ “for all good men”

TIMESTAMP

 

時間類型

 

BINARY

 

字節數組

 

集合數據類型

Hive 有三種複雜數據類型 ARRAY、MAP 和 STRUCT。ARRAY 和 MAP 與 Java 中的 Array 和 Map 類似,而 STRUCT 與 C 語言中的 Struct 類似,它封裝了一個命名字段集合,複雜數據類型允許任意層次的嵌套。

3.HIVE表的DDL重要相關概念

內外表:默認爲內部表,EXTERNAL 關鍵字可以創建一個外部表,在建表的同時指定一個指向實際數據的路徑(LOCATION),Hive 創建內部表(管理表)時,會將數據移動到數據倉庫指向的路徑;若創建外部表,僅記錄數據所在的路徑,不對數據的位置做任何改變。在刪除表的時候,內部表的元數據和數據會被一起刪除,而外部表只刪除元數據,不刪除數據。使用場景在外部表(原始日誌表)的基礎上做大量的統計分析,用到的中間表、結果表使用內部表存儲,數據通過 SELECT+INSERT 進入內部表。

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

創建表語法

CREATE [EXTERNAL] TABLE [IF NOT EXISTS] 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]

4.實例

①.創建表SQL

create external table if not exists test_table(
name string comment '姓名',
friends array<string> comment '朋友',
properties map<string, string> comment'屬性',
father struct<name:string, age:int> comment'父親'
)
partitioned by (month string)
row format delimited fields terminated by ',' #屬性的分隔符爲’,‘
collection items terminated by '_' #MAP STRUCT 和 ARRAY 的分隔符(數據分割符號)
map keys terminated by ':' #map格式爲key:value
lines terminated by '\n'; #數據行之間的分隔符

② 導入數據

#數據
touch test.txt
vim test.txt
zhagnsan,lisi_wangwu,age:18_gender:man,zhangshanfather_39
lisi,zhangsan_wangwu,age:19_gender:woman,lisifather_40
#導入數據:
load data local inpath 'test.txt' into table test_table partition(month='202005');

③.查詢數據

select * from test_table;
select * from test_table where month='202005';
select * from test_table where month='202005' union select * from test_table where month='202004';

④.管理表與外部表的互相轉換

#修改內部表爲外部表
alter table test_table set tblproperties('EXTERNAL'='TRUE');
#修改外部表爲內部表
alter table test_table set tblproperties('EXTERNAL'='FALSE');
#注意:('EXTERNAL'='TRUE')和('EXTERNAL'='FALSE')爲固定寫法,區分大小寫!向test_table 表中插入數據,注意表設計有分區,插入的時候需要設置分區

⑤ 分區操作

#創建單個分區
alter table test_table add partition(month='202004');
#同時創建多個分區
alter table test_table add partition(month='202003') partition(month='202002');
#刪除單個分區
alter table test_table drop partition(month='202004');
#同時刪除多個分區
alter table test_table drop partition(month='202003'), partition (month='202002');
#查看分區表有多少分區
show partitions test_table;
#查看分區表結構
desc formatted test_table;
#創建二級分區表
create table test_partitions(name string) partitioned by (month string, day string)
row format delimited fields terminated by '\t';
#二級分區加載數據
touch test_partition.txt
vim test_partition.txt
zhagnsan
lisi
#加載數據
load  data local inpath 'test_partition.txt' into table test_partitions partition(month='202005', day='3');
# 二級分區查詢分區數據
select * from test_partitions where month=202005 and day=3;

⑥.重命名錶

語法:ALTER TABLE table_name RENAME TO new_table_name
alter table test_table rename to test1;

⑦.增加列

ALTER TABLE table_name ADDCOLUMNS (col_name data_type[COMMENT col_comment], ...) 
#注:ADD 是代表新增一字段,字段位置在所有列後面(partition 列前)
alter table test_table add columns(tabledesc string);
備註:ALTER TABLE  table_name  add columns( desc1 string COMMENT 'tttt') CASCADE;加分區表字段需要加上CASCADE

⑧.替換列

ALTER TABLE table_name replace [CLOUMN] col_old_name col_new_name column_type [CONMMENT col_conmment] [FIRST|AFTER column_name] [CASCADE|RESTRICT];
alter table test_table replace columns(tabledesc string, tabledesc1 string);

⑨.更新列

語法: ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name]
alter table test_table change column tabledesc tabledesc1 string;
注意:列的數據類型需要兼容

⑩. 刪除表

drop table test_table;

 

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