第 1 章:Hive基本概念
1.1 Hive
1.1.1 Hive產(chǎn)生背景
HDFS來(lái)存儲(chǔ)海量的數(shù)據(jù)、MapReduce來(lái)對(duì)海量數(shù)據(jù)進(jìn)行分布式并行計(jì)算、Yarn來(lái)實(shí)現(xiàn)資源管理和作業(yè)調(diào)度。但是面對(duì)海量的數(shù)據(jù)和負(fù)責(zé)的業(yè)務(wù)邏輯,開發(fā)人員要編寫MR對(duì)數(shù)據(jù)進(jìn)行統(tǒng)計(jì)分析難度極大,所以就產(chǎn)生了Hive這個(gè)數(shù)倉(cāng)工具。Hive可以幫助開發(fā)人員將SQL語(yǔ)句轉(zhuǎn)化為MapReduce在yarn上跑。
1.1.2 hive簡(jiǎn)介
Hive是基于hadoop的一個(gè)數(shù)據(jù)倉(cāng)庫(kù)工具,將結(jié)構(gòu)化的數(shù)據(jù)文件映射成一張表,并提供類SQL(HQL)查詢功能。
1.1.3 Hive本質(zhì):將HQL(hiveSQL)轉(zhuǎn)化成MapReduce程序
1、Hive處理的數(shù)據(jù)存儲(chǔ)在HDFS
2、Hive分析數(shù)據(jù)底層的實(shí)現(xiàn)是MapReduce
3、執(zhí)行程序運(yùn)行在Yarn上
4、結(jié)構(gòu)化文件如何映射成一張表呢?借助存儲(chǔ)在元數(shù)據(jù)數(shù)據(jù)庫(kù)中的元數(shù)據(jù)來(lái)解析結(jié)構(gòu)化文件。
1.2 Hive架構(gòu)原理
1.2.1 Hive架構(gòu)介紹
1)用戶結(jié)構(gòu):Client | CLI(command-line interface)、JDBC/ODBC(jdbc訪問(wèn)hive) | |
---|---|---|
2)元數(shù)據(jù):Metastore | 元數(shù)據(jù)包括:表名、表所屬的數(shù)據(jù)庫(kù)(默認(rèn)是default)、表的擁有者、列/分區(qū)字段、表的類型(是否是外部表)、表的數(shù)據(jù)所在目錄等;默認(rèn)存儲(chǔ)在自帶的derby數(shù)據(jù)庫(kù)中,推薦使用MySQL存儲(chǔ)Metastore | |
3) Hadoop | 使用HDFS進(jìn)行存儲(chǔ),使用MapReduce進(jìn)行計(jì)算 | |
4) 驅(qū)動(dòng)器:Driver | 解析器(SQL Parser) | 將SQL字符串轉(zhuǎn)換成抽象語(yǔ)法樹AST,這一步一般都用第三方工具庫(kù)完成,比如antlr;對(duì)AST進(jìn)行語(yǔ)法分析,比如表是否存在、字段是否存在、SQL語(yǔ)義是否有誤 |
編譯器(Physical Plan) | 將AST編譯生成邏輯執(zhí)行計(jì)劃 | |
優(yōu)化器(Query Optimizer) | 對(duì)邏輯執(zhí)行計(jì)劃進(jìn)行優(yōu)化 | |
執(zhí)行器(Execution) | 把邏輯執(zhí)行計(jì)劃轉(zhuǎn)換成可以運(yùn)行的物理計(jì)劃。對(duì)于Hive來(lái)說(shuō),就是MR/Spark |
1.2.2 Hive的運(yùn)行機(jī)制
hive通過(guò)給用戶提供的一系列交互接口,接受到的用戶指令(SQL),使用自己Driver,結(jié)合元數(shù)據(jù)(metaStore),將這些指令翻譯成MapReduce,提交到Hadoop中執(zhí)行,最后,將執(zhí)行返回的結(jié)果輸出到用戶交互接口中。
1.3 Hive和數(shù)據(jù)庫(kù)比較
Hive | mysql | |
---|---|---|
語(yǔ)言 | 類sql | sql |
語(yǔ)言規(guī)模 | 大數(shù)據(jù)pd及以上 | 數(shù)據(jù)量小一般在百萬(wàn)左右到達(dá)單表極限 |
數(shù)據(jù)插入 | 能增加insert,不能update,delete | 能insert,update,delete |
數(shù)據(jù)存儲(chǔ) | Hdfs | 擁有自己的存儲(chǔ)空間 |
計(jì)算引擎 | MapReduce/Spark/tez | 自己的引擎innodb |
第 2 章:Hive安裝
2.1 修改hadoop相關(guān)參數(shù)
1)修改core-site.xml
1、配置該superUser允許通過(guò)代理訪問(wèn)的主機(jī)節(jié)點(diǎn)
2、配置該superUser允許通過(guò)代理用戶所屬組
3、配置該superUser允許通過(guò)代理的用戶
2)配置yarn-site.xml
1、NodeManager使用內(nèi)存數(shù),默認(rèn)是8G,修改成4G內(nèi)存
2、容器最小內(nèi)存,默認(rèn)512M
3、容器最大內(nèi)存,默認(rèn)是8G,修改成4G
4、關(guān)閉虛擬內(nèi)存檢查(默認(rèn)開啟)
3)分發(fā)修改后的配置文件
2.2 Hive解壓安裝
1)上傳壓縮包到linux的/opt/softsware目錄下
2)將/opt/softsware目錄下的壓縮包解壓到/opt/module目錄下
3)將解壓后的文件修改成hive
4)修改/etc/profile.d/my_env.sh文件,將hive的/bin目錄添加到環(huán)境變量
2.3 Hive元數(shù)據(jù)的三種部署方式
2.3.1 元數(shù)據(jù)庫(kù)之Derby
這種方式適用于輕量級(jí)或者單機(jī)模式的部署,通常用于測(cè)試或開發(fā)環(huán)境。配置相對(duì)簡(jiǎn)單,但不適合高可用性和大規(guī)模部署。
1、內(nèi)嵌模式示意圖:
2、Derby數(shù)據(jù)庫(kù):
Derby數(shù)據(jù)庫(kù)是Java編寫的內(nèi)存數(shù)據(jù)庫(kù),在內(nèi)嵌模式中與應(yīng)用程序共享一個(gè)JVM,應(yīng)用程序負(fù)責(zé)啟動(dòng)和停止。
3、初始化Derby數(shù)據(jù)庫(kù):
1)在hive根目錄下,使用/bin目錄下的schematool命令初始化hive自帶的Derby元數(shù)據(jù)庫(kù)
2)執(zhí)行上述初始化元數(shù)據(jù)庫(kù)時(shí),會(huì)發(fā)生存在jar包沖突問(wèn)題
3)解決jar包沖突問(wèn)題,只需要把hive的/lib目錄下的log4j~.jar重命名即可
4、啟動(dòng)Hive
1)執(zhí)行/bin目錄下的hive命令,就可以啟動(dòng)hive,并通過(guò)cli方式連接到hive
2)使用hive
- show databases; 查看當(dāng)前所有的數(shù)據(jù)庫(kù)
- show tables; 查看當(dāng)前所有的表
- create table test_derby(id int); 創(chuàng)建表
- insert into test_derby values(1001); 插入數(shù)據(jù)
- select * from test_derby; 查看數(shù)據(jù)
5、內(nèi)嵌模式只有一個(gè)JVM進(jìn)程
在內(nèi)嵌模式下,命令行執(zhí)行jps -ml命令,只能看到一個(gè)CliDriver進(jìn)程。
2.3.2 元數(shù)據(jù)庫(kù)之Mysql
這種方式更加適合生產(chǎn)環(huán)境,因?yàn)樗С侄嘤脩舨l(fā)訪問(wèn)和更好的可伸延性。需要額外的配置和管理數(shù)據(jù)庫(kù)服務(wù)。
1、直連模式示意圖:
2、Mysql安裝部署
1)檢測(cè)當(dāng)前系統(tǒng)是否安裝過(guò)Mysql,如果安裝過(guò)刪除掉
2)將Mysql安裝包上傳至/opt/software目錄下
3)解壓到/opt/software下新建的mysql_jars目錄
4)查看mysql_jars目錄下文件
5)在/opt/software/mysql_jars目錄下執(zhí)行rpm安裝,按順序
6)如果在mysql的數(shù)據(jù)存儲(chǔ)路徑下有文件存在,需要將其全部刪除,存儲(chǔ)路徑地址在/etc/my.cnf文件下datadir參數(shù)所對(duì)應(yīng)的值
7)初始化數(shù)據(jù)庫(kù),查看臨時(shí)的root用戶的密碼
8)啟動(dòng)mysql服務(wù)
9)登錄mysql,修改root用戶的密碼
10)修改mysql庫(kù)下的user表中的root用戶允許任意ip連接
11)刷新,使得修改生效
3、配置Hive元數(shù)據(jù)庫(kù)為MySQL
1)拷貝驅(qū)動(dòng)
Hive需要將元數(shù)據(jù)信息存儲(chǔ)到元數(shù)據(jù)庫(kù)mysql中,需要使用JDBC的方式連接到Mysql,所以,將Mysql的JDBC驅(qū)動(dòng)拷貝到Hive的lib目錄下,供hive調(diào)用。
2)配置Metastore到Mysql
在/opt/module/hive/conf目錄下新建hive-site.xml文件
(1)jdbc連接的URL
(2)jdbc連接的Driver
(3)jdbc連接的username
(4)jdbc連接的password
(5)Hive默認(rèn)在HDFS的工作目錄
(6)Hive元數(shù)據(jù)存儲(chǔ)的驗(yàn)證設(shè)置false
(7)元數(shù)據(jù)存儲(chǔ)授權(quán)設(shè)置false
4、Hive初始化元數(shù)據(jù)庫(kù)
在mysql中創(chuàng)建hive存儲(chǔ)元數(shù)據(jù)的數(shù)據(jù)庫(kù)metastore,再通過(guò)hive的初始化元數(shù)據(jù)庫(kù)操作創(chuàng)建表
1)登錄mysql
2)新建Hive元數(shù)據(jù)庫(kù)
3)初始化Hive元數(shù)據(jù)庫(kù)
5、啟動(dòng)Hive
1)啟動(dòng)Hive
2)使用hive
- show databases; 查看當(dāng)前所有的數(shù)據(jù)庫(kù)
- show tables; 查看當(dāng)前所有的表
- create table test_mysql(id int); 創(chuàng)建表
- insert into test_mysql values(1002); 插入數(shù)據(jù)
- select * from test_mysql; 查看數(shù)據(jù)
3)開啟另一個(gè)窗口測(cè)試,是否支持客戶端并發(fā)操作
6、在公司生產(chǎn)環(huán)境中,網(wǎng)絡(luò)環(huán)境非常的復(fù)雜,mysql的所在環(huán)境可能存在網(wǎng)絡(luò)隔離,無(wú)法直接訪問(wèn);另外,mysql的root賬戶和密碼在此模式下會(huì)存在泄露風(fēng)險(xiǎn),存在數(shù)據(jù)安全隱患。
2.3.3 元數(shù)據(jù)之MetaStore Server
在這種模式下,Hive與Hadoop生態(tài)系統(tǒng)中的其他組件共享元數(shù)據(jù),這種方式可以實(shí)現(xiàn)元數(shù)據(jù)的高度集成和優(yōu)化。
1、元數(shù)據(jù)服務(wù)模式示意圖:
2、元數(shù)據(jù)服務(wù)模式
在服務(wù)器端訪問(wèn)MetaStore服務(wù),客戶端利用Thrift協(xié)議通過(guò)MetaStore服務(wù)訪問(wèn)元數(shù)據(jù)庫(kù)。相比于內(nèi)嵌式,這種更適合在生產(chǎn)環(huán)境中部署使用。
3、將Mysql作為元數(shù)據(jù)庫(kù),配置元數(shù)據(jù)服務(wù)
1)首先,將hive的元數(shù)據(jù)庫(kù)配置為Mysql,編寫hive-site.xml文件。在配置完后,啟動(dòng)hive之前必須先啟動(dòng)元數(shù)據(jù)服務(wù),否則,hive啟動(dòng)后無(wú)法連接到元數(shù)據(jù)服務(wù)。
2)啟動(dòng)元數(shù)據(jù)服務(wù)
注意:?jiǎn)?dòng)后窗口不能再操作,需打開一個(gè)新的shell窗口做別的操作。
(1)啟動(dòng)hive,查看表和表中的數(shù)據(jù),是否是Mysql數(shù)據(jù)庫(kù)中的表。
(2)再另一個(gè)窗口啟動(dòng)hive,測(cè)試多客戶端能否同時(shí)連接操作。
2.4 hive的兩種訪問(wèn)方式
2.4.1 命令行方式
1、cli太過(guò)笨重,需要hive的jar支持。
2.4.2 HiveServe2 模式
1、JDBC訪問(wèn)Hive示意圖:
2、JDBC方式訪問(wèn)Hive
將hive包裝為服務(wù)發(fā)布出去,開發(fā)者使用JDBC的方式連接到服務(wù),從而操作hive,減少對(duì)hive環(huán)境的依賴。
3、開啟Hiveserver2
1)在hive-site.xml文件中添加如下配置信息
(1)指定hiveserver2連接的host
(2)指定hiveserver2連接的端口號(hào)
2)重啟MetaStore服務(wù)
3)啟動(dòng)hive服務(wù)(如果是使用元數(shù)據(jù)服務(wù)的模式,需要提前開啟元數(shù)據(jù)服務(wù))
4)啟動(dòng)beeline服務(wù)
2.6 Hive常用交互命令
2.6.1 查看bin/hive命令幫助
- bin/hive -help
2.6.2 命令中參數(shù)-e的使用
使用-e參數(shù),可以不進(jìn)入hive的交互窗口執(zhí)行sql語(yǔ)句
2.6.3 命令中參數(shù)-f的使用
使用-f參數(shù),可以不進(jìn)入hive交互窗口,執(zhí)行腳本中sql語(yǔ)句
1)在/opt/module/hive/下創(chuàng)建datas目錄并在目錄下創(chuàng)建hive-f.sql文件
2)文件中寫入正確的sql語(yǔ)句
3)執(zhí)行文件中的sql語(yǔ)句,還可以將結(jié)果寫入指定文件中
第 3 章 Hive數(shù)據(jù)類型
3.1 基本數(shù)據(jù)類型
Hive數(shù)據(jù)類型 | Java數(shù)據(jù)類型 | 長(zhǎng)度 |
---|---|---|
TINYINT | byte | 1byte有符號(hào)整數(shù) |
SWALINT | short | 2byte有符號(hào)整數(shù) |
INT | int | 4byte有符號(hào)整數(shù) |
BIGINT | long | 8byte有符號(hào)整數(shù) |
BOOLEAN | boolean | 布爾類型,true或者false |
FLOAT | float | 單精度浮點(diǎn)數(shù) |
DOUBLE | double | 雙精度浮點(diǎn)數(shù) |
STRING | string | 字符系列??梢允褂脝我?hào)或者雙引號(hào) |
TIMESTAMP | 時(shí)間類型 | |
BINARY | 字節(jié)數(shù)組 |
Hive的String類型不用聲明其中最多能存儲(chǔ)多少個(gè)字符,理論上它可以存儲(chǔ)2GB的字符數(shù)。
3.2 集合數(shù)據(jù)類型
數(shù)據(jù)類型 | 描述 | 語(yǔ)法示例 |
---|---|---|
STRUCT | 和c語(yǔ)言中的struct類似,都可以通過(guò)“點(diǎn)”符號(hào)訪問(wèn)元素內(nèi)容。例如:如果某個(gè)列的數(shù)據(jù)類型是STRUCT{first STRING, last String},那么第1個(gè)元素可以通過(guò)字段.first來(lái)引用。 | struct() 例如: struct<street:string,city:string> |
MAP | MAP是一組鍵-值對(duì)元組集合,使用數(shù)組表示法可以訪問(wèn)數(shù)據(jù)。例如:如果某個(gè)列的數(shù)據(jù)類型是MAP,其中鍵->值對(duì)是’first’->'john’和‘last’->‘doe’,那么可以通過(guò)字段名[‘last’]獲取最后一個(gè)元素 | map() 例如:map<string,int> |
ARRAY | 數(shù)組是一組具有相同類型和名稱的變量的集合。這些變量稱為數(shù)組的元素,每個(gè)數(shù)組元素都有一個(gè)編號(hào),編號(hào)從零開始。例如:數(shù)組值為[‘john’,‘doe’] ,那么第2個(gè)元素可以通過(guò)數(shù)組名[1]進(jìn)行引用 | Array() 例如:array |
3.3 案例操作
3.3.1 簡(jiǎn)單了解前后端的數(shù)據(jù)傳輸
3.3.2 數(shù)據(jù)結(jié)構(gòu)映射
1)假設(shè)某表有如下一行,我們用JSON格式來(lái)表示其數(shù)據(jù)結(jié)構(gòu)。在Hive下訪問(wèn)的格式為
{
"name": "songsong",
"friends": ["bingbing" , "lili"] , //列表Array,
"children": { //鍵值Map,
"xiao song": 19 ,
"xiaoxiao song": 18
}
"address": { //結(jié)構(gòu)Struct,
"street": "hui long guan" ,
"city": "beijing"
}
}
2)基于上述數(shù)據(jù)結(jié)構(gòu),我們?cè)贖ive里創(chuàng)建對(duì)應(yīng)的表,并導(dǎo)入數(shù)據(jù)。
在目錄/opt/module/hive/datas下創(chuàng)建本地測(cè)試文件personInfo.txt
- vim personInfo.txt
songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
3.3.3 測(cè)試案例
1)Hive上創(chuàng)建測(cè)試表personInfo
hive(default)>create table personInfo (
name string,
friends array<string>,
children map<string, int>,
address struct<street:string, city:string>
)
row format delimited
fields terminated by ','
collection items terminated by '_'
map keys terminated by ':'
lines terminated by '\n';
指定數(shù)據(jù)文件中行格式的分隔符
指定字段之間用’,’進(jìn)行分割
指定集合類型的元素之間用’_’進(jìn)行分割
指定map類型中key和value用’:’進(jìn)行分割
指定行之間的分隔符為’\n’
2)上傳數(shù)據(jù)到hdfs中上述表的對(duì)應(yīng)路徑
hadoop fs -put /opt/module/hive/datas/personInfo.txt /user/hive/warehouse/personInfo;
3)訪問(wèn)三種集合列里的數(shù)據(jù),以下分別是ARRAY,MAP,STRUCT的訪問(wèn)方式
select
friends[1],
children['xiao song'],
address.city
from personInfo
where name="songsong";
結(jié)果:
_c0 _c1 city
lili 18 beijing
3.4 類型轉(zhuǎn)換
1)Hive的基本數(shù)據(jù)類型進(jìn)行隱性轉(zhuǎn)換類似Java
2)隱式類型轉(zhuǎn)換規(guī)則如下
(1)所有整數(shù)類型都可以隱式的轉(zhuǎn)換為一個(gè)范圍更廣的類型,如INT可以轉(zhuǎn)換成BIGINT。
(2)所有整數(shù)類型、FLOAT和STRING類型都可以隱式地轉(zhuǎn)換成DOUBLE。
(3)TINYINT、SMALLINT、INT都可以轉(zhuǎn)換為FLOAT。
(4)BOOLEAN類型不可以轉(zhuǎn)換為任何其它的類型。
3)可以使用CAST操作顯示進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換
例如:CAST(‘1’ AS INT)將把字符串‘1’轉(zhuǎn)換成整數(shù)1;
第4章:DDL 數(shù)據(jù)定義
4.1 創(chuàng)建數(shù)據(jù)庫(kù)
1)創(chuàng)建數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)在HDFS上的默認(rèn)存儲(chǔ)路徑是/usr/hive/warehouse/*.db。
create database bigdata;
2)避免要?jiǎng)?chuàng)建的數(shù)據(jù)庫(kù)已經(jīng)存在,增加if not exists判斷。
create database if not exists bigdata;
3)創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),指定數(shù)據(jù)庫(kù)在HDFS上存放的位置
create database bigdata2 location '/bigdata2.db';
4.2 查詢數(shù)據(jù)庫(kù)
4.2.1 顯示數(shù)據(jù)庫(kù)
1)顯示數(shù)據(jù)庫(kù)
show databases;
2)過(guò)濾顯示查詢的數(shù)據(jù)庫(kù)
show databases like 'bigdata*';
4.2.2 查看數(shù)據(jù)庫(kù)詳情
1)顯示數(shù)據(jù)庫(kù)信息
desc database bigdata;
bigdata hdfs://hadoop102:9000/user/hive/warehouse/bigdata.db atguigu USER
2)顯示數(shù)據(jù)庫(kù)詳細(xì)信息,extended
desc database extended bigdata;
bigdata hdfs://hadoop102:9000/user/hive/warehouse/bigdata.db atguigu USER
3)創(chuàng)建數(shù)據(jù)庫(kù)bigdata3,并設(shè)置其createtime屬性
create database bigdata3 with dbproperties('createtime'='20211022');
4)再次查詢
desc database bigdata3
OK
bigdata3 hdfs://hadoop102:8020/user/hive/warehouse/bigdata3.db atguigu USER
desc database extended bigdata3
OK
bigdata3 hdfs://hadoop102:8020/user/hive/warehouse/bigdata3.db atguigu USER {createtime=20211022}
4.2.3 切換當(dāng)前數(shù)據(jù)庫(kù)
use bigdata;
4.3 修改數(shù)據(jù)庫(kù)
用戶可以使用ALTER DATABASE命令為某個(gè)數(shù)據(jù)庫(kù)的DBPROPERTIES設(shè)置鍵-值對(duì)屬性值,來(lái)描述這個(gè)數(shù)據(jù)庫(kù)的屬性信息。
alter database bigdata set dbproperties('createtime'='20211022');
4.4 刪除數(shù)據(jù)庫(kù)
1)刪除空數(shù)據(jù)庫(kù)
drop database if exists bigdata2
2)如果數(shù)據(jù)庫(kù)不為空,可以采用cascade命令,強(qiáng)制刪除
drop database bigdata cascade;
4.5 創(chuàng)建表
1)建表語(yǔ)句
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]
[LIKES existing_table_or_view_name]
2)字段解釋說(shuō)明
CREATE TABLE | 創(chuàng)建一個(gè)指定名稱的表。如果相同名稱的表已經(jīng)存在,則拋出異常;用戶可以用 IF NOT EXISTS 選項(xiàng)忽略這個(gè)異常 |
---|---|
EXTERNAL | 1)關(guān)鍵字可以讓用戶創(chuàng)建一個(gè)外部表,在建表的同時(shí)可以指定一個(gè)指向?qū)嶋H數(shù)據(jù)的路徑(LOCATION)2)在刪除表的適合,內(nèi)部表的元數(shù)據(jù)和數(shù)據(jù)都被一起刪除,外部表只刪除元數(shù)據(jù),不刪除數(shù)據(jù)。 |
COMMENT | 為表和列添加注釋 |
PARTITIONED BY | 創(chuàng)建分區(qū)表 |
CLUSTERED BY | 創(chuàng)建分桶表 |
SORTED BY | 不常用,對(duì)桶中的一個(gè)或多個(gè)列另外排序 |
ROW FROMAT | Fields 指定字段之間的分隔符;Collection 用于指定集合中元素的分隔符等 |
STORE AS | 指定存儲(chǔ)文件類型:如SEQUENCEFILE(二進(jìn)制序列文件)、TEXTFILE(文本)、RCFILE(列式存儲(chǔ)格式文件) |
LOCATION | 指定表在HDFS上的存儲(chǔ)位置 |
AS | 后跟查詢語(yǔ)句,根據(jù)查詢語(yǔ)句結(jié)果創(chuàng)建表 |
LIKE | 允許用戶復(fù)制現(xiàn)有的表結(jié)構(gòu),但是不復(fù)制數(shù)據(jù) |
4.5.1 管理表(內(nèi)部表)
1)理論
- 默認(rèn)創(chuàng)建的表都是所謂的管理表,有時(shí)也被稱為內(nèi)部表。
- 管理表,Hive會(huì)控制著元數(shù)據(jù)和真實(shí)數(shù)據(jù)的生命周期。
- Hive默認(rèn)會(huì)將這些表的數(shù)據(jù)存儲(chǔ)在hive.metastore.warehouse.dir定義目錄的子目錄下。
- 當(dāng)我們刪除一個(gè)管理表時(shí),Hive也會(huì)刪除這個(gè)表中數(shù)據(jù)。
- 管理表不適合和其他工具共享數(shù)據(jù)。
2)案例實(shí)操
創(chuàng)建數(shù)據(jù)文件,在/opt/module/hive/datas目錄下創(chuàng)建文件student.txt,編輯如下內(nèi)容:
vim student.txt
1001 ss1
1002 ss2
1003 ss3
1004 ss4
1005 ss5
1006 ss6
1007 ss7
1008 ss8
1009 ss9
(1)創(chuàng)建內(nèi)部表student
create table if not exists student(
id int,
name string
)
row format delimited
fields terminated by '\t'
stored as textfile
location '/user/hive/warehouse/student';
(2)查詢表的類型
desc formatted student;
Table Type: MANAGED_TABLE
(3)根據(jù)查詢結(jié)果創(chuàng)建表(查詢的結(jié)果會(huì)添加到新創(chuàng)建的表中)
create table if not exists student2 as select id, name from student;
(4)根據(jù)已經(jīng)存在的表結(jié)構(gòu)創(chuàng)建表
create table if not exists student3 like student;
(5)查詢表的類型
desc formatted student2;
Table Type: MANAGED_TABLE
(6)刪除表student2后,觀察表的元數(shù)據(jù)和數(shù)據(jù)文件是否還存在
drop table student2;
4.5.2 外部表
1、理論
因?yàn)楸硎峭獠勘?,所以Hive并非認(rèn)為其完全擁有這份數(shù)據(jù)。刪除該表并不會(huì)刪除掉這份數(shù)據(jù),不過(guò)描述表的元數(shù)據(jù)信息會(huì)被刪除掉。
元數(shù)據(jù)信息:指存儲(chǔ)在Hive元數(shù)據(jù)倉(cāng)庫(kù)中的關(guān)于表的信息,例如表名、表結(jié)構(gòu)(列名和數(shù)據(jù)類型)、表的物理位置(文件路徑)等。這些信息幫助Hive了解如何訪問(wèn)和解釋存儲(chǔ)在外部位置的數(shù)據(jù)。
2、管理表和外部表的使用場(chǎng)景
外部表多用來(lái)存儲(chǔ)原始數(shù)據(jù),采用外部表交易共享數(shù)據(jù)。在原始數(shù)據(jù)基礎(chǔ)上做大量的統(tǒng)計(jì)分析,中間用到的中間表、結(jié)果表多存于內(nèi)部表。
3、案例實(shí)操
1)創(chuàng)建teacher.txt
1001 teacher1
1002 teacher2
1003 teacher3
1004 teacher4
1005 teacher5
2)上傳數(shù)據(jù)到HDFS
hadoop fs -mkdir -p /school/teacher
hadoop fs -put teacher.txt /school/teacher
3)在hive中創(chuàng)建外部表teacher
create external table if not exists teacher(
id int,
name string
)
row format delimited fields terminated by '\t'
location '/school/teacher';
4)查看創(chuàng)建的表
show tables;
5)查看表格式化信息
desc formatted dept;
Table Type: EXTERNAL_TABLE
6)刪除外部表,觀察表的元數(shù)據(jù)和相應(yīng)hdfs中的數(shù)據(jù)
drop table dept;
外部表刪除后,hdfs中的數(shù)據(jù)還在,但是metadata中dept的元數(shù)據(jù)已被刪除
4.5.3 管理表與外部表的互相轉(zhuǎn)換
1)查詢表的類型
desc formatted student2;
Table Type: MANAGED_TABLE
2)修改內(nèi)部表student2為外部表
alter table student2 set tblproperties('EXTERNAL'='TRUE');
3)查詢表的類型
desc formatted student2;
Table Type: EXTERNAL_TABLE
4)修改外部表student2為內(nèi)部表
alter table student2 set tblproperties('EXTERNAL'='FALSE');
4.6 修改表
4.6.1 重命名表
1、語(yǔ)法
ALTER TABLE table_name RENAME TO new_table_name
2、實(shí)操案例
alter table student3 rename to student4;
4.6.2 增加/修改/替換列信息
1、語(yǔ)法
1)更新列
ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name]
2)增加和替換列
ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...)
2、實(shí)操案例
1)查詢表結(jié)構(gòu)
desc test2;
OK
col_name data_type comment
id int
2)更新列:將列名id修改為student_id,類型不變
alter table test2 change column id student_id int;
OK
Time taken: 0.083 seconds
desc test2;
OK
col_name data_type comment
student_id int
3)更新列:不修改列名,僅修改列的類型為string
alter table test2 change column student_id student_id string;
OK
Time taken: 0.083 seconds
desc test2;
OK
col_name data_type comment
student_id string
4)新增列:向test2表中新增一列,列名為name,類型為string
alter table test2 add columns(name string);
desc test2;
OK
col_name data_type comment
student_id string
name string
5)調(diào)整列的位置:現(xiàn)在想讓name的列在最前面,做如下操作
alter table test2 change name name string first;
OK
Time taken: 0.139 seconds
desc test2;
OK
col_name data_type comment
name string
student_id string
Time taken: 0.036 seconds, Fetched: 2 row(s)
6)調(diào)整列的位置:將name更新到指定列的后面,操作如下
alter table test2 change name name string after student_id;
OK
Time taken: 0.069 seconds
desc test2;
OK
col_name data_type comment
student_id string
name string
Time taken: 0.033 seconds, Fetched: 2 row(s)
7)替換列(替換所有的列)
alter table test2 replace columns(id double);
OK
Time taken: 0.058 seconds
desc test2;
OK
col_name data_type comment
id double
Time taken: 0.032 seconds, Fetched: 1 row(s)
4.7 刪除表
drop table test2;
4.8 清除表中數(shù)據(jù)(Truncate)
truncate table student;
注意:truncate 只能刪除管理表,不能刪除外部表中數(shù)據(jù)
第5章 DML 數(shù)據(jù)操作
5.1 數(shù)據(jù)導(dǎo)入
5.1.1 向表中狀態(tài)數(shù)據(jù)(Load)
1、基本語(yǔ)法
load data [local] inpath '數(shù)據(jù)的path' [overwrite] into table table_name [partition (partcol1=val1,…)];
Load data | 加載數(shù)據(jù) |
---|---|
Local | 表示從本地加載數(shù)據(jù)到hive表,否則是從HDFS加載數(shù)據(jù)到Hive表 |
Inpath | 表是加載數(shù)據(jù)的路徑 |
Overwrite | 表示覆蓋表中已有數(shù)據(jù),否則表示追加 |
Into table | 表示加載數(shù)據(jù)到哪張表中 |
Partition | 表示加載數(shù)據(jù)到指定分區(qū) 。通過(guò)分區(qū),可以將表中的數(shù)據(jù)分散存儲(chǔ)在不同的部分,通?;谀承┝械闹?。例如,可以根據(jù)日期、地區(qū)等屬性來(lái)分區(qū)。 |
2、實(shí)例操作
1)創(chuàng)建一張表student
create table student(
id string,
name string
)
row format delimited fields terminated by '\t';
2)加載本地文件到hive
load data local inpath '/opt/module/hive/datas/student.txt' into table default.student;
3)加載HDFS文件到hive中
(1)上傳文件到HDFS
dfs -put /opt/module/hive/datas/student.txt /input;
(2)加載HDFS上數(shù)據(jù)
load data inpath '/input/student.txt' into table default.student;
4)加載數(shù)據(jù)覆蓋表中已有的數(shù)據(jù)
load data inpath '/input/student.txt' overwrite into table default.student;
FAILED: SemanticException Line 1:17 Invalid path ''/input/student.txt'': No files matching path hdfs://hadoop102:8020/input/student.txt
竟然報(bào)錯(cuò)了,信息顯示文件不存在?
顯然,加載HDFS上的文件到hive表中,采用的類似剪切的方式,將文件拷貝到表的映射目錄下。
上傳文件到HDFS
dfs -put /opt/module/hive/datas/student.txt /input;
加載HDFS上數(shù)據(jù)
load data inpath '/input/student.txt' overwrite into table default.student;
5.1.2 向表中插入數(shù)據(jù)(Insert)
1)創(chuàng)建一張表
create table student2(id int, name string) row format delimited fields terminated by '\t';
2)基本插入數(shù)據(jù)
insert into table student2 values(1,'wangwu'),(2,'zhaoliu');
3)將查詢結(jié)果插入表中
insert overwrite table student2 select id, name from student ;
insert into | 以追加數(shù)據(jù)的方式插入到表或分區(qū),原有數(shù)據(jù)不會(huì)刪除 |
---|---|
insert overwrite | 會(huì)覆蓋表中已存在的數(shù)據(jù) |
注意:insert不支持只插入部分?jǐn)?shù)據(jù)
5.1.3 查詢語(yǔ)句中創(chuàng)建表并加載數(shù)據(jù)(AS Select)
根據(jù)查詢結(jié)果創(chuàng)建表
create table if not exists student4
as select id, name from student;
5.1.4 創(chuàng)建表時(shí)通過(guò)Location指定加載數(shù)據(jù)路徑
1、上傳數(shù)據(jù)到hdfs上
dfs -mkdir /input/student;
dfs -put /opt/module/hive/datas/student.txt /input/student/student.txt;
2、創(chuàng)建表,并指定在hdfs上的位置
create external table if not exists student5(
id int,
name string
)
row format delimited fields terminated by '\t'
location '/input/student';
3、查詢數(shù)據(jù)
select * from student5;
OK
student5.id student5.name
1001 ss1
1002 ss2
……
注意:hive創(chuàng)建表時(shí),默認(rèn)將表的名稱作為默認(rèn)HDFS上表對(duì)應(yīng)的存儲(chǔ)路徑的名稱,但是,如果你通過(guò)location指定存儲(chǔ)路徑,就不會(huì)修改路徑名稱為表名了。如上邊的表名為student5和其在HDFS上的存儲(chǔ)路徑student。
5.2 數(shù)據(jù)導(dǎo)入
5.2.1 Insert導(dǎo)入
1、將查詢的結(jié)果導(dǎo)出到本地
insert overwrite local directory '/opt/module/hive/datas/export/student'
select * from student;
2、將查詢的結(jié)果格式化導(dǎo)出到本地
insert overwrite local directory '/opt/module/hive/datas/export/student'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from bigdata1.student;
3、將查詢的結(jié)果導(dǎo)出到HDFS上(沒(méi)有l(wèi)ocal)
insert overwrite directory '/output/student'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student;
注意:insert導(dǎo)入時(shí),hive會(huì)自動(dòng)創(chuàng)建導(dǎo)出目錄,但是由于是overwrite,所以導(dǎo)出路徑一定要寫準(zhǔn)確,否則存在誤刪數(shù)據(jù)的可能。
5.3 數(shù)據(jù)遷移
export 和 import命令主要用于兩個(gè)Hadoop平臺(tái)集群之間Hive表遷移。(元數(shù)據(jù)源+真實(shí)數(shù)據(jù))文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-796197.html
5.3.1 Export導(dǎo)出到HDFS上
export table default.student2 to '/地址'; 導(dǎo)出到哪里
5.3.2 Import數(shù)據(jù)到指定Hive表中
import table student2 from '/地址 '; 從哪里導(dǎo)入
注意:先用export導(dǎo)出后,再將數(shù)據(jù)導(dǎo)入。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-796197.html
到了這里,關(guān)于大數(shù)據(jù)開發(fā)之Hive(基本概念、安裝、數(shù)據(jù)類型、DDL數(shù)據(jù)定義、DML數(shù)據(jù)操作)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!