數(shù)據(jù)倉庫-Hive基礎(chǔ)(五)Hive 的基本操作

1. 數(shù)據(jù)庫操作

1.1 創(chuàng)建數(shù)據(jù)庫

create database if not exists myhive; 

use myhive;

說明:hive的表存放位置模式是由hive-site.xml當(dāng)中的一個屬性指定的

<name>hive.metastore.warehouse.dir</name> 
<value>/user/hive/warehouse</value>

1.2 創(chuàng)建數(shù)據(jù)庫并指定位置

create database myhive2 location '/myhive2';

1.3 設(shè)置數(shù)據(jù)庫鍵值對信息

數(shù)據(jù)庫可以有一些描述性的鍵值對信息,在創(chuàng)建時添加:

create database foo with dbproperties ('owner'='itcast', 'date'='20190120');

查看數(shù)據(jù)庫的鍵值對信息:

describe database extended foo;

修改數(shù)據(jù)庫的鍵值對信息:

alter database foo set dbproperties ('owner'='itheima');

1.4 查看數(shù)據(jù)庫更多詳細(xì)信息

desc database extended myhive2;

1.5 刪除數(shù)據(jù)庫

刪除一個空數(shù)據(jù)庫,如果數(shù)據(jù)庫下面有數(shù)據(jù)表斗躏,那么就會報錯

drop database myhive2;

強(qiáng)制刪除數(shù)據(jù)庫惦积,包含數(shù)據(jù)庫下面的表一起刪除

drop database myhive cascade;

2.數(shù)據(jù)庫表操作

create [external] table [if not exists] table_name ( 
col_name data_type [comment '字段描述信息'] 
col_name data_type [comment '字段描述信息']) 
[comment '表的描述信息'] 
[partitioned by (col_name data_type,...)] 
[clustered by (col_name,col_name,...)] 
[sorted by (col_name [asc|desc],...) into num_buckets buckets] 
[row format row_format] 
[storted as ....] 
[location '指定表的路徑']

說明:

create table

創(chuàng)建一個指定名字的表割粮。如果相同名字的表已經(jīng)存在并鸵,則拋出異常苇倡;用戶可以用 IF NOT EXISTS 選項來忽略這個異常拳球。

external

可以讓用戶創(chuàng)建一個外部表审姓,在建表的同時指定一個指向?qū)嶋H數(shù)據(jù)的路徑 (LOCATION),Hive 創(chuàng)建內(nèi)部表時祝峻,會將數(shù)據(jù)移動到數(shù)據(jù)倉庫指向的路徑魔吐;若創(chuàng)建外部 表,僅記錄數(shù)據(jù)所在的路徑莱找,不對數(shù)據(jù)的位置做任何改變酬姆。在刪除表的時候,內(nèi)部表的 元數(shù)據(jù)和數(shù)據(jù)會被一起刪除奥溺,而外部表只刪除元數(shù)據(jù)辞色,不刪除數(shù)據(jù)。

comment

表示注釋,默認(rèn)不能使用中文

partitioned by

表示使用表分區(qū),一個表可以擁有一個或者多個分區(qū)浮定,每一個分區(qū)單獨存在一個目錄下 .

clustered by

對于每一個表分文件淫僻, Hive可以進(jìn)一步組織成桶,也就是說桶是更為細(xì)粒 度的數(shù)據(jù)范圍劃分壶唤。Hive也是 針對某一列進(jìn)行桶的組織雳灵。

sorted by

指定排序字段和排序規(guī)則

row format

指定表文件字段分隔符

storted as

指定表文件的存儲格式, 常用格式:SEQUENCEFILE, TEXTFILE, RCFILE,如果文件 數(shù)據(jù)是純文本,可以使用 STORED AS TEXTFILE闸盔。如果數(shù)據(jù)需要壓縮悯辙,使用 storted as SEQUENCEFILE。

location

指定表文件的存儲路徑

3.內(nèi)部表的操作

創(chuàng)建表時,如果沒有使用external關(guān)鍵字,則該表是內(nèi)部表(managed table)

Hive建表字段類型

分 類 類型 描述 字面量示例
原始類型 BOOLEAN true/false TRUE
TINYINT 1字節(jié)的有符號整數(shù), -128~127 1Y
SMALLINT 2個字節(jié)的有符號整 數(shù),-32768~32767 1S
INT 4個字節(jié)的帶符號整數(shù) 1
BIGINT 8字節(jié)帶符號整數(shù) 1L
FLOAT 4字節(jié)單精度浮點數(shù) 1.0
DOUBLE 8字節(jié)雙精度浮點數(shù) 1.0
DEICIMAL 任意精度的帶符號小數(shù) 1.0
STRING 字符串躲撰,變長 “a”,’b’
VARCHAR 變長字符串 “a”,’b’
CHAR 固定長度字符串 “a”,’b’
BINARY 字節(jié)數(shù)組 無法表示
TIMESTAMP 時間戳针贬,毫秒值精度 122327493795
DATE 日期 ‘2016-03-29’
INTERVAL 時間頻率間隔
復(fù)雜類型 ARRAY 有序的的同類型的集合 array(1,2)
MAP key-value,key必須為原始類型,value可以任意類型 map(‘a(chǎn)’,1,’b’,2)
STRUCT 字段集合,類型可以不同 struct(‘1’,1,1.0), named_stract(‘col1’,’1’,’col2’,1,’clo3’,1.0)
UNION 在有限取值范圍內(nèi)的一個值 create_union(1,’a’,63)

建表入門:

# 選中創(chuàng)建的數(shù)據(jù)庫
use myhive; 

# 創(chuàng)建學(xué)生表
create table stu(id int,name string); 

#插入一條數(shù)據(jù)(insert命令走mapreduce所以效率很低)
insert into stu values (1,"zhangsan"); 

# 查詢表內(nèi)元素
select * from stu;

創(chuàng)建表并指定字段之間的分隔符

create table if not exists stu2(id int ,name string) row format delimited fields terminated by '\t';

創(chuàng)建表并指定表文件的存放路徑

create table if not exists stu2(id int ,name string) row format delimited fields terminated by '\t' location '/user/stu2';

根據(jù)查詢結(jié)果創(chuàng)建表

create table stu3 as select * from stu2; # 通過復(fù)制表結(jié)構(gòu)和表內(nèi)容創(chuàng)建新表

根據(jù)已經(jīng)存在的表結(jié)構(gòu)創(chuàng)建表

create table stu4 like stu;

查詢表的詳細(xì)信息

desc formatted stu2;

刪除表

drop table stu4;

4. 外部表的操作

外部表說明

外部表因為是指定其他的hdfs路徑的數(shù)據(jù)加載到表當(dāng)中來拢蛋,所以hive表會認(rèn)為自己不完全獨占 這份數(shù)據(jù)桦他,所以刪除hive表的時候,數(shù)據(jù)仍然存放在hdfs當(dāng)中谆棱,不會刪掉.

內(nèi)部表和外部表的使用場景

每天將收集到的網(wǎng)站日志定期流入HDFS文本文件快压。在外部表(原始日志表)的基礎(chǔ)上做大量 的統(tǒng)計分析,用到的中間表垃瞧、結(jié)果表使用內(nèi)部表存儲蔫劣,數(shù)據(jù)通過SELECT+INSERT進(jìn)入內(nèi)部表。

操作案例

分別創(chuàng)建老師與學(xué)生表外部表个从,并向表中加載數(shù)據(jù)

創(chuàng)建老師表

create external table teacher (t_id string,t_name string) row format delimited fields terminated by '\t';

創(chuàng)建學(xué)生表

create external table student (s_id string,s_name string,s_birth string , s_sex string ) row format delimited fields terminated by '\t';

加載數(shù)據(jù)

load data local inpath '/export/servers/hivedatas/student.csv' into table student;

加載數(shù)據(jù)并覆蓋已有數(shù)據(jù)

load data local inpath '/export/servers/hivedatas/student.csv' overwrite into table student;

從hdfs文件系統(tǒng)向表中加載數(shù)據(jù)(需要提前將數(shù)據(jù)上傳到hdfs文件系統(tǒng))

# 進(jìn)入文件目錄
cd /export/servers/hivedatas 

# 用hdfs上傳文件到指定文件夾
hdfs dfs -mkdir -p /hivedatas hdfs dfs -put techer.csv /hivedatas/ 

# 讀取數(shù)據(jù)到teable中
load data inpath '/hivedatas/techer.csv' into table teacher;

5. 分區(qū)表的操作

在大數(shù)據(jù)中脉幢,最常用的一種思想就是分治,我們可以把大的文件切割劃分成一個個的小的文 件嗦锐,這樣每次操作一個小的文件就會很容易了嫌松,同樣的道理,在hive當(dāng)中也是支持這種思想 的奕污,就是我們可以把大的數(shù)據(jù)豆瘫,按照每月,或者天進(jìn)行切分成一個個的小的文件,存放在不同 的文件夾中.

創(chuàng)建分區(qū)表語法

create table score(s_id string,c_id string, s_score int) partitioned by (month string) row format delimited fields terminated by '\t';

創(chuàng)建一個表帶多個分區(qū)

create table score2 (s_id string,c_id string, s_score int) partitioned by (year string,month string,day string) row format delimited fields terminated by '\t';

加載數(shù)據(jù)到分區(qū)表中

load data local inpath '/export/servers/hivedatas/score.csv' into table score partition (month='201806');

加載數(shù)據(jù)到多分區(qū)表中

load data local inpath '/export/servers/hivedatas/score.csv' into table score2 partition(year='2018',month='06',day='01');

多分區(qū)表聯(lián)合查詢(使用union all)

select * from score where month = '201806' union all select * from score where month = '201806';

查看分區(qū)

show partitions score;

添加一個分區(qū)

alter table score add partition(month='201805');

刪除分區(qū)

alter table score drop partition(month = '201806');

6. 分區(qū)表綜合練習(xí)

現(xiàn)在有一個文件score.csv文件菊值,存放在集群的這個目錄下/scoredatas/month=201806外驱,這個文 件每天都會生成,存放到對應(yīng)的日期文件夾下面去腻窒,文件別人也需要公用昵宇,不能移動。需 求儿子,創(chuàng)建hive對應(yīng)的表瓦哎,并將數(shù)據(jù)加載到表中,進(jìn)行數(shù)據(jù)統(tǒng)計分析柔逼,且刪除表之后蒋譬,數(shù)據(jù)不能 刪除

數(shù)據(jù)準(zhǔn)備:

hdfs dfs -mkdir -p /scoredatas/month=201806 
hdfs dfs -put score.csv /scoredatas/month=201806/

創(chuàng)建外部分區(qū)表,并指定文件數(shù)據(jù)存放目錄

create external table score4(s_id string, c_id string,s_score int) partitioned by (month string) row format delimited fields terminated by '\t' location '/scoredatas';

進(jìn)行表的修復(fù)(建立表與數(shù)據(jù)文件之間的一個關(guān)系映射)

msck repair table score4;

7. 分桶表操作

分桶愉适,就是將數(shù)據(jù)按照指定的字段進(jìn)行劃分到多個文件當(dāng)中去,分桶就是MapReduce中的分區(qū).

開啟 Hive 的分桶功能

set hive.enforce.bucketing=true;

設(shè)置 Reduce 個數(shù)

set mapreduce.job.reduces=3;

創(chuàng)建分桶表

create table course (c_id string,c_name string,t_id string) clustered by(c_id) into 3 buckets row format delimited fields terminated by '\t';

桶表的數(shù)據(jù)加載犯助,由于通標(biāo)的數(shù)據(jù)加載通過hdfs dfs -put文件或者通過load data均不好使,只能通過insert overwrite

創(chuàng)建普通表维咸,并通過insert overwriter的方式將普通表的數(shù)據(jù)通過查詢的方式加載到桶表當(dāng)中去

創(chuàng)建普通表

create table course_common (c_id string,c_name string,t_id string) row format delimited fields terminated by '\t';

普通表中加載數(shù)據(jù)

load data local inpath '/export/servers/hivedatas/course.csv' into table course_common;

通過insert overwrite給桶表中加載數(shù)據(jù)

insert overwrite table course select * from course_common cluster by(c_id);

修改表結(jié)構(gòu)

重命名:

alter table old_table_name rename to new_table_name;

把表score4修改成score5

alter table score4 rename to score5;

增加/修改列信息:

查詢表結(jié)構(gòu)

desc score5;

添加列

alter table score5 add columns (mycol string, mysco int);

更新列

alter table score5 change column mysco mysconew int;

刪除表

drop table score5;

增加/修改列信息:

查詢表結(jié)構(gòu)

desc score5;

添加列

alter table score5 add columns (mycol string, mysco int);

更新列

alter table score5 change column mysco mysconew int;

刪除表

drop table score5;

hive表中加載數(shù)據(jù)

直接向分區(qū)表中插入數(shù)據(jù)

create table score3 like score;

insert into table score3 partition(month ='201807') values ('001','002','100');

通過load方式加載數(shù)據(jù)

load data local inpath '/export/servers/hivedatas/score.csv' overwrite into table score partition(month='201806');

通過查詢方式加載數(shù)據(jù)

create table score4 like score; 

insert overwrite table score4 partition(month = '201806') select s_id,c_id,s_score from score;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末剂买,一起剝皮案震驚了整個濱河市惠爽,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌瞬哼,老刑警劉巖婚肆,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異坐慰,居然都是意外死亡较性,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進(jìn)店門结胀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赞咙,“玉大人,你說我怎么就攤上這事把跨。” “怎么了沼死?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵着逐,是天一觀的道長。 經(jīng)常有香客問我意蛀,道長耸别,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任县钥,我火速辦了婚禮秀姐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘若贮。我一直安慰自己省有,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布谴麦。 她就那樣靜靜地躺著蠢沿,像睡著了一般。 火紅的嫁衣襯著肌膚如雪匾效。 梳的紋絲不亂的頭發(fā)上舷蟀,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天,我揣著相機(jī)與錄音面哼,去河邊找鬼野宜。 笑死,一個胖子當(dāng)著我的面吹牛魔策,可吹牛的內(nèi)容都是我干的匈子。 我是一名探鬼主播,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼闯袒,長吁一口氣:“原來是場噩夢啊……” “哼旬牲!你這毒婦竟也來了仿粹?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤原茅,失蹤者是張志新(化名)和其女友劉穎吭历,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體擂橘,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡晌区,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了通贞。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片朗若。...
    茶點故事閱讀 40,912評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖昌罩,靈堂內(nèi)的尸體忽然破棺而出哭懈,到底是詐尸還是另有隱情,我是刑警寧澤茎用,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布遣总,位于F島的核電站,受9級特大地震影響轨功,放射性物質(zhì)發(fā)生泄漏旭斥。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一古涧、第九天 我趴在偏房一處隱蔽的房頂上張望垂券。 院中可真熱鬧,春花似錦羡滑、人聲如沸菇爪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽娄帖。三九已至,卻和暖如春昙楚,著一層夾襖步出監(jiān)牢的瞬間近速,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工堪旧, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留削葱,地道東北人。 一個月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓淳梦,卻偏偏與公主長得像析砸,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子爆袍,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,922評論 2 361