DDL
1.庫
建庫:
> create database if not exists 庫名;
還有一個方式:
> create database if not exists 庫名 location 路徑;
指定hdfs路徑
查看數(shù)據(jù)庫:
> show databases;
看數(shù)據(jù)庫信息:
> desc databases 庫名;
想多看點(diǎn):
> desc databases extended 庫名;
改庫:(數(shù)據(jù)庫名和數(shù)據(jù)庫目錄位置無法修改)
> 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]
語句特別長逃糟,中括號里面的不是必要的笆焰,但是大多數(shù)情況下的建表都是分區(qū)表。
1.create table 就是建表基礎(chǔ)氢妈。
2.external:關(guān)鍵字是可以創(chuàng)建于一個外部表粹污,建表同時指定一個指向?qū)嶋H數(shù)據(jù)的路徑(location)。
hive創(chuàng)建內(nèi)部表時首量,會將數(shù)據(jù)移動到數(shù)據(jù)倉庫指向的路徑壮吩;
如果創(chuàng)建外部表,僅記錄數(shù)據(jù)所在的路徑加缘,不對數(shù)據(jù)的位置做任何改變粥航。
區(qū)別就在于刪除表的時候。內(nèi)部表元數(shù)據(jù)和數(shù)據(jù)都會被刪除生百,外部表只刪除元數(shù)據(jù)递雀,不刪除數(shù)據(jù)。
3.comment:添加注釋(對表蚀浆、對列)
4.partitioned by:創(chuàng)建分區(qū)表
5.clustered by:創(chuàng)建分桶表
6.sorted by : 排序缀程,基本不用
7.row format:數(shù)據(jù)切分形式
8.stored as :指定文件存儲類型
9.location:指定表在hdfs的位置
10.like :允許用戶復(fù)制現(xiàn)有變結(jié)構(gòu),但不復(fù)制數(shù)據(jù)
我們使用create table的時候市俊,基本上hive幫我們補(bǔ)充了其他參數(shù)的默認(rèn)值杨凑。
管理表
默認(rèn)創(chuàng)建的表都是管理表,有時候也稱為內(nèi)部表摆昧。這種表撩满,hive會控制數(shù)據(jù)的生命周期,我們刪除表的時候,hive也會刪除表中的數(shù)據(jù)伺帘。
外部表更安全一些昭躺,畢竟數(shù)據(jù)是分開的。刪表之后數(shù)據(jù)會保留伪嫁。
查詢表的類型
> desc formatted 表名
能發(fā)現(xiàn)Table_type领炫,是表的類型≌趴龋可以修改
內(nèi)部表改為外部表
> alert table 表名 set tblproperties ('EXTERNAL'='TRUE')
外部表改為內(nèi)部表
> alert table 表名 set tblproperties ('EXTERNAL'='FALSE')
MANAGED_TABLE是內(nèi)部表帝洪,EXTERNAL_TABLE是外部表。
注意=呕4邢俊!('EXTERNAL'='TRUE')和 ('EXTERNAL'='FALSE')這個要區(qū)分大小寫AW逦帧!
3.分區(qū)
分區(qū)的意義:分區(qū)表實(shí)際上來說泌参,就是對應(yīng)HDFS文件系統(tǒng)上獨(dú)立的文件夾脆淹,該文件夾下是該分區(qū)所有數(shù)據(jù)文件。Hive中的分區(qū)就是分目錄沽一,把一個大的數(shù)據(jù)集根據(jù)業(yè)務(wù)需要盖溺,分割成小的數(shù)據(jù)集。在查詢的時候铣缠,通過where子句中的表達(dá)式烘嘱,選擇查詢所需要的指定分區(qū)。提高查詢效率(謂詞下推)蝗蛙。
3.1 引入分區(qū)表(根據(jù)日期對日志進(jìn)行管理)
/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 創(chuàng)建分區(qū)表
> create table dept_partition(deptno int,dname string,loc string)
partitioned by (month string)
row format delimited fields terminated by '\t';
3.3 加載數(shù)據(jù)到分區(qū)表中
> 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的時候發(fā)現(xiàn),查詢的是兩個文件和并的數(shù)據(jù)捡硅。并且?guī)臀覀冏詣犹砑恿薽onth哮内,并且它可以作為查詢條件。這個條件區(qū)分了文件夾壮韭,查詢的時候會被先訪問北发。
我們?nèi)ysql看元數(shù)據(jù)。PARTITION表喷屋,增加了兩條數(shù)據(jù)琳拨。這個記錄了我們的分區(qū)。
3.4 查詢分區(qū)表中數(shù)據(jù)
單分區(qū)查詢:
> select * from dept_partition where month = '2019-06';
多分區(qū)聯(lián)合查詢:
> 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 增加分區(qū)
創(chuàng)建單個分區(qū):
> alert table dept_partition add partition(month=''2019-09);
多個分區(qū):
> alert table dept_partition add partition(month=''2019-10) partition(month=''2019-11);
3.6 刪除分區(qū)
> alter table dept_partition drop partition (month='2019-10');
刪多個:
> alter table dept_partition drop partition (month='2019-10'),partition (month='2019-11');
3.7 查看分區(qū)表有多少個分區(qū)
> show partitions dept_partition;
3.8 分區(qū)表結(jié)構(gòu)查看
> desc formatted dept_partition;
多了一個分區(qū)字段信息屯曹。把分區(qū)字段當(dāng)普通字段使用就可以了狱庇。
4.分區(qū)表注意事項(xiàng)
4.1 二級分區(qū)
> 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 把數(shù)據(jù)直接上傳到分區(qū)目錄上颜启,讓分區(qū)表和數(shù)據(jù)產(chǎn)生關(guān)聯(lián)的三種方式。
(1)上傳數(shù)據(jù)之后修復(fù)
> msck repair table dept_partition;
(2)上傳數(shù)據(jù)之后添加分區(qū)
> alter table dept_partition add partition(month'2019-11');
(3)創(chuàng)建文件夾之后load數(shù)據(jù)到分區(qū)批什。
load添加了分區(qū)信息农曲,上傳了數(shù)據(jù)驻债,問題直接就解決了形葬。
五.修改表
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
一.數(shù)據(jù)導(dǎo)入
1.1.1 向表中裝載數(shù)據(jù)(Load)
語法:
> load data [local] inpath '/opt/soft/文件.txt' overwrite|into
table 表名 [partition(partcol1=val1,...)];
load data:加載數(shù)據(jù)
local:表示從本地加載到hive表猖腕;否則從HDFS加載到Hive
inpath:加載數(shù)據(jù)路徑
overwrite:表示覆蓋表中已有數(shù)據(jù),否則表示追加倘感。
into table:表示加載到哪張表
partition:上傳到指定分區(qū)
1.1.2 通過查詢語句插入數(shù)據(jù)(insert)
> insert into table 表名 partition(month='2019-06') value (1,'zahngsan');
into也可以變成overwrite,基本模式插入老玛。
還有多插入模式之類的淤年。。蜡豹。不過不常用。
1.1.3 查詢語句中創(chuàng)建并加載數(shù)據(jù)(As Select)
1.創(chuàng)建一張分區(qū)表
例如:
> create table if not exist 表名(id int,name string) partitioned by
(month string) row format delimited fields terminated by '\t';
2.基本插入數(shù)據(jù)
例如:
> insert into table student partition(month='2019-07') values(1,'wangwu');
3.基本模式插入(覆蓋)
例如:
> insert overwrite table student partition(month='2019-07') values(1,'wangwu');
二.數(shù)據(jù)導(dǎo)出
2.1 Insert導(dǎo)出
1.結(jié)果導(dǎo)出到本地
> insert overwrite local directory '/opt/module/datas/export/student'
select * from student;
哎镜廉,由于我虛擬機(jī)分配內(nèi)存比較小,系統(tǒng)慢的要死威根!讓我一度以為集群搭建的有問題!B宀蟆佑淀!
2.將結(jié)果格式化導(dǎo)出到本地
>insert overwrite local directory '/opt/module/datas/export/student'
row format delimited fields terminated by '\t'
select * from student;
3.導(dǎo)出到hdfs
>insert overwrite directory '/dept'
row format delimited fields terminated by '\t'
select * from student;
2.2 Hadoop導(dǎo)出
2.3 Export導(dǎo)出到HDFS
2.4 SQOOP導(dǎo)出
MySQL 與 Hive(HDFS) 之間導(dǎo)來導(dǎo)去留美。。逢倍。