1. 程式人生 > >Hive(5)-DDL資料定義

Hive(5)-DDL資料定義

一. 建立資料庫

CREATE DATABASE [IF NOT EXISTS] database_name
[COMMENT database_comment]
[LOCATION hdfs_path]
[WITH DBPROPERTIES (property_name=property_value, ...)];

欄位解釋:

1). IF NOT EXISTS: 避免要建立的資料庫已經存在

2). COMMENT : 給資料庫新增一個備註

3). LOCATION: 如果不指定路徑,預設的儲存路徑是HDFS的/user/hive/warehouse/*.db

4). WITH DBPROPERTIES

: 給資料庫新增一些自定的<key,value>

create database if not exists hive_db
comment 'my fisrt database'
location '/first_database'
with dbproperties ('createtime' = '20181218');

Hive預設不支援中文,但是可以改

1). 修改hive_site.xml中的引數

<property>
    <name>javax.jdo.option.ConnectionURL</name>     
    <
value>jdbc:mysql://hadoop102:3306/metastore?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=UTF-8</value> <description>JDBC connect string for a JDBC metastore</description> </property>

2). 進入資料庫的metastore中執行以下SQL

-- 修改表字段註解和表註解:
alter table
COLUMNS_V2 modify column COMMENT varchar(256) character set utf8; alter table TABLE_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8; -- 修改分割槽欄位註解: alter table PARTITION_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8; alter table PARTITION_KEYS modify column PKEY_COMMENT varchar(4000) character set utf8;

 

二. 查詢資料庫

1.顯示資料庫

-- 顯示資料庫
show databases;

-- 過濾顯示資料庫
show databases like 'hive*';

2.檢視資料庫詳情

-- 顯示資料庫資訊
desc database hive_db;

-- 顯示資料庫詳細資訊
desc database extended hive_db;

3. 切換資料庫

use hive_db2;

 

三. 修改資料庫

使用者可以使用ALTER DATABASE命令為某個資料庫的DBPROPERTIES設定鍵-值對屬性值,來描述這個資料庫的屬性資訊。資料庫的其他元資料資訊都是不可更改的,包括資料庫名和資料庫所在的目錄位置.

alter database hive_db set dbproperties('createtime'='20181218');

 

四. 刪除資料庫

-- 刪除資料庫
drop database hive_db;

-- 如果刪除的資料庫不存在,最好用if exists判斷
drop database if exists hive_db;

-- 如果資料庫裡有表,可以用cascade強制刪除
drop database hive_db cascade;

 

五. 建立表

1.建表語法

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]
[TBLPROPERTIES (property_name=property_value, ...)]
[AS select_statement]
[LIKE table_name]

可選欄位解釋

1). EXTERNAL  表示建立的表是一個外部表,一般在建外部表的時候會和LOCATION一起用,指向實際資料的路徑. 在刪除表的時候,內部表的元資料和資料會被一起刪除,而外部表只刪除元資料,不刪除資料.

2). IF NOT EXISTS   判斷要建立的表是否已經存在,如果存在的話,不報異常

3). COMMENT  為表的欄位或者表新增備註

4). PARTITIONED BY  建立分割槽表

5). CLUSTERED BY  建立分桶表

6). SORTED BY  對桶中的一列或多列另外排序

7).ROW FORMAT  

DELIMITED

[FIELDS TERMINATED BY char]     指定欄位分隔符

[COLLECTION ITEMS TERMINATED BY char]     指定Struct和Array資料分隔符

[MAP KEYS TERMINATED BY char]     指定Map資料的key value 分隔符

[LINES TERMINATED BY char]     指定行分隔符

| SERDE serde_name [WITH SERDEPROPERTIES (property_key=property_value,...)]

使用者在建表的時候可以自定義SerDe或者使用自帶的SerDe。如果沒有指定ROW FORMAT 或者ROW FORMAT DELIMITED,將會使用自帶的SerDe。在建表的時候,使用者還需要為表指定列,使用者在指定表的列的同時也會指定自定義的SerDe,Hive通過SerDe確定表的具體的列的資料.

SerDe是Serialize/Deserilize的簡稱, hive使用Serde進行行物件的序列與反序列化

8).STORED AS  指定儲存檔案型別.常用的儲存檔案型別:SEQUENCEFILE(二進位制序列檔案)、TEXTFILE(文字)、RCFILE(列式儲存格式檔案).如果檔案資料是純文字,可以使用STORED AS TEXTFILE。如果資料需要壓縮,使用 STORED AS SEQUENCEFILE

9). LOCATION  指定表在HDFS上的儲存位置

10). TBLPROPERTIES  給表新增一些自定的k,v值

11). AS  後面跟查詢語句,根據查詢結果建立表

12). LIKE  允許使用者複製現有的表結構,但是不復制資料

2. 管理表(MANAGED_TABLE)

預設建立的表都是所謂的管理表,有時也被稱為內部表。因為這種表,Hive會(或多或少地)控制著資料的生命週期。Hive預設情況下會將這些表的資料儲存在由配置項hive.metastore.warehouse.dir(例如,/user/hive/warehouse)所定義的目錄的子目錄下。    當我們刪除一個管理表時,Hive也會刪除這個表中資料。管理表不適合和其他工具共享資料。

-- 普通建立表
create table if not exists namelist(
id int, name string
)
row format delimited 
fields terminated by '\t'
stored as textfile
location '/user/hive/warehouse/namelist';

-- 根據查詢結果建立表(查詢的結果會新增到新建立的表中)
create table if not exists namelist2 as select id, name from namelist;

-- 根據已經存在的表結構建立表
create table if not exists namelist3 like namelist;

-- 查詢表的型別
desc formatted namelist;

3. 外部表

因為表是外部表,所以Hive並非認為其完全擁有這份資料。刪除該表並不會刪除掉這份資料,不過描述表的元資料資訊會被刪除掉。

管理表和外部表的使用場景:

每天將收集到的網站日誌定期流入HDFS文字檔案。在外部表(原始日誌表)的基礎上做大量的統計分析,用到的中間表、結果表使用內部表儲存,資料通過SELECT+INSERT進入內部表。

準備一份student和dept檔案,上傳到hdfs上

-- 建立外部表
create external table stu_external(
id int, 
name string) 
row format delimited fields terminated by '\t' 
location '/student';

-- 檢視建立的表
select * from stu_external;

-- 查看錶格式化資料
desc formatted dept;

--刪除外部表
drop table stu_external;

外部表刪除後,hdfs中的資料還在,但是metadata中stu_external的元資料已被刪除

4.管理表和外部表的互相轉換

-- 查詢表的型別
desc formatted student2;

-- 修改內部表student2為外部表
alter table student2 set tblproperties('EXTERNAL'='TRUE');

-- 查詢表的型別
desc formatted student2;

-- 修改外部表student2為內部表
alter table student2 set tblproperties('EXTERNAL'='FALSE');

-- 查詢表的型別
hive (default)> desc formatted student2;

('EXTERNAL'='TRUE')和('EXTERNAL'='FALSE')為固定寫法,區分大小寫!

 

六. 分割槽表

分割槽表實際上就是對應一個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/module/datas/dept.txt' into table default.dept_partition partition(month='201809');

load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201910');

load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201811’);

分割槽表載入資料時,必須指定分割槽

3.查詢表中資料

-- 單分割槽查詢
select * from dept_partition where month='201809';

-- 多分割槽聯合查詢
select * from dept_partition where month='201909'
union
select * from dept_partition where month='201808'
union
select * from dept_partition where month='201807';

4. 增加分割槽

-- 建立單個分割槽
alter table dept_partition add partition(month='201706') ;

-- 同時建立多個分割槽
alter table dept_partition add partition(month='201705') partition(month='201704');

5. 刪除分割槽

-- 刪除單個分割槽
alter table dept_partition drop partition (month='201804');

-- 同時刪除多個分割槽
alter table dept_partition drop partition (month='201705'), partition (month='201706');

建立多個分割槽時,partition之間沒有 逗號,刪除多個分割槽時,partition之間有 逗號.....嚴重懷疑不是同一個人寫的

6.檢視分割槽表有多少個分割槽

show partitions dept_partition;

7.檢視分割槽表結構

desc formatted dept_partition;

8. 二級分割槽表

-- 建立二級分割槽表
create table dept_partition2(
deptno int, dname string, loc string
)
partitioned by (month string, day string)
row format delimited fields terminated by '\t';

-- 載入資料到二級分割槽表
load data local inpath '/opt/module/datas/dept.txt' into table
default.dept_partition2 partition(month='201709', day='13');

--查詢二級分割槽表
select * from dept_partition2 where month='201709' and day='13';

9. 把資料直接上傳到分割槽目錄上,讓分割槽表和資料產生關聯的三種方式

1).上傳資料後修復

上傳資料

dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=12;

dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201709/day=12;
-- 查詢資料(查詢不到剛上傳的資料)
select * from dept_partition2 where month='201709' and day='12';

-- 執行修復命令
msck repair table dept_partition2;

-- 再次查詢資料(有資料)
select * from dept_partition2 where month='201709' and day='12';

2).上傳資料後新增分割槽

上傳方法同上

-- 執行新增分割槽
alter table dept_partition2 add partition(month='201709',day='11');

-- 查詢資料
select * from dept_partition2 where month='201709' and day='11';

3).建立資料夾後load資料到分割槽

建立目錄

dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=10;
-- 上傳資料
load data local inpath '/opt/module/datas/dept.txt' into table dept_partition2 partition(month='201709',day='10');

-- 查詢資料
select * from dept_partition2 where month='201709' and day='10';

 

七. 修改表

1.重命名錶

語法

ALTER TABLE table_name RENAME TO new_table_name
alter table dept_partition2 rename to dept_partition3;

2. 增加/修改/替換列資訊

語法

更新列
ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name]

增加和替換列
ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...) 

ADD是代表新增一欄位,欄位位置在所有列後面(partition列前),REPLACE則是表示替換表中所有欄位。

-- 新增列
alter table dept_partition add columns(deptdesc string);

-- 更新列
alter table dept_partition change column deptdesc desc int;

-- 替換列
alter table dept_partition replace columns(deptno string, dnamestring, loc string);

 

八. 刪除表

drop table dept_partition;