一、Hive簡介
1.1 Hive是什么
由Facebook開源的一款基于hadoop的用于統(tǒng)計海量結(jié)構(gòu)化數(shù)據(jù)的一個數(shù)據(jù)倉庫。
(1)建立在Hadoop HDFS上的數(shù)據(jù)倉庫基礎(chǔ)架構(gòu)
(2)可以用來進行ETL
(3)定義了類似于SQL,成為HQL
(4)允許MapReduce自定義
1.2 Hive官網(wǎng)
http://hive.apache.org/
1.3 Hive適用場景
(1)日志分析:大部分互聯(lián)網(wǎng)公司使用hive進行日志分析,包括百度、淘寶等。
(2)統(tǒng)計網(wǎng)站一個時間段內(nèi)的pv、uv
(3)多維度數(shù)據(jù)分析
(4)海量結(jié)構(gòu)化數(shù)據(jù)離線分析
1.4 Hive不合適場景
(1)hive不是一個關(guān)系型數(shù)據(jù)庫
(2)Hive不適合做為在線事務(wù)處理(OLTP)的系統(tǒng)
(3)Hive不適合做實時查詢和行級更新的操作
1.5 Hive的優(yōu)點
(1)簡單容易上手:提供了類SQL查詢語言HQL
(2)可擴展:為超大數(shù)據(jù)集設(shè)計了計算/擴展能力(MR作為計算引擎,HDFS作為存儲系統(tǒng)),一般情況下不需要重啟服務(wù)Hive可以自由的擴展集群的規(guī)模。
(3)提供統(tǒng)一的元數(shù)據(jù)管理
(4)延展性:Hive支持用戶自定義函數(shù),用戶可以根據(jù)自己的需求來實現(xiàn)自己的函數(shù)
(5)容錯:良好的容錯性,節(jié)點出現(xiàn)問題SQL仍可完成執(zhí)行
1.6 Hive的缺點
(1)hive的HQL表達能力有限
1)迭代式算法無法表達,比如pagerank
2)數(shù)據(jù)挖掘方面,比如kmeans
(2)hive的效率比較低
1)hive自動生成的mapreduce作業(yè),通常情況下不夠智能化
2)hive調(diào)優(yōu)比較困難,粒度較粗
3)hive可控性差
1.7 Hive的體系結(jié)構(gòu)

1.8 Hive支持的數(shù)據(jù)類型
復(fù)雜類型
| array_type
| map_type
| struct_type
簡單類型
|TINYINT
| SMALLINT
| INT
| BIGINT
| BOOLEAN
| FLOAT
| DOUBLE
| STRING
1.9 Hive特點
1.在HDFS上處理數(shù)據(jù),但是元數(shù)據(jù)通常保存在關(guān)系型數(shù)據(jù)庫中
2.Hive被設(shè)計用來處理離線數(shù)據(jù)分析(OLAP)
3.Hive提供了類似sql類型的語言HiveQL(HQL)用來進行查詢操作。
4.Hive具有易懂,快速,可伸縮,可擴展的特性
1.10 Hive與傳統(tǒng)數(shù)據(jù)庫的對比

二、Hive安裝
2.1?Hive安裝地址
1.Hive官網(wǎng)地址
2.文檔查看地址
https://cwiki.apache.org/confluence/display/Hive/GettingStarted
3.下載地址
http://archive.apache.org/dist/hive/
4.github地址
https://github.com/apache/hive
2.2 Hive安裝部署
1.Hive安裝及配置
(1)把apache-hive-1.2.1-bin.tar.gz上傳到linux的/opt/software目錄下
(2)解壓apache-hive-1.2.1-bin.tar.gz到/opt/module/目錄下面
[atguigu@hadoop102 software]$ tar -zxvf apache-hive-1.2.1-bin.tar.gz -C /opt/module/
(3)修改apache-hive-1.2.1-bin.tar.gz的名稱為hive
[atguigu@hadoop102 module]$ mv apache-hive-1.2.1-bin/ hive
(4)修改/opt/module/hive/conf目錄下的hive-env.sh.template名稱為hive-env.sh
[atguigu@hadoop102 conf]$ mv hive-env.sh.template hive-env.sh
(5)配置hive-env.sh文件
(a)配置HADOOP_HOME路徑
export HADOOP_HOME=/opt/module/hadoop-2.7.2
(b)配置HIVE_CONF_DIR路徑
export HIVE_CONF_DIR=/opt/module/hive/conf
2.Hadoop集群配置
(1)必須啟動hdfs和yarn
[atguigu@hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh
[atguigu@hadoop103 hadoop-2.7.2]$ sbin/start-yarn.sh
(2)在HDFS上創(chuàng)建/tmp和/user/hive/warehouse兩個目錄并修改他們的同組權(quán)限可寫
[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -mkdir /tmp
[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -mkdir -p /user/hive/warehouse
[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -chmod g+w /tmp
[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -chmod g+w /user/hive/warehouse
3.Hive基本操作
(1)啟動hive
[atguigu@hadoop102 hive]$ bin/hive
(2)查看數(shù)據(jù)庫
hive>?show databases;
(3)打開默認數(shù)據(jù)庫
hive>?use default;
(4)顯示default數(shù)據(jù)庫中的表
hive>?show tables;
(5)創(chuàng)建一張表
hive> create table student(id int, name string);
(6)顯示數(shù)據(jù)庫中有幾張表
hive>?show tables;
(7)查看表的結(jié)構(gòu)
hive>?desc student;
(8)向表中插入數(shù)據(jù)
hive> insert into student values(1000,"ss");
(9)查詢表中數(shù)據(jù)
hive> select * from student;
(10)退出hive
hive> quit;
2.3將本地文件導(dǎo)入Hive案例
需求
將本地/opt/module/datas/student.txt這個目錄下的數(shù)據(jù)導(dǎo)入到hive的student(id int, name string)表中。
1.?dāng)?shù)據(jù)準(zhǔn)備
在/opt/module/datas這個目錄下準(zhǔn)備數(shù)據(jù)
(1)在/opt/module/目錄下創(chuàng)建datas
[atguigu@hadoop102 module]$ mkdir datas
(2)在/opt/module/datas/目錄下創(chuàng)建student.txt文件并添加數(shù)據(jù)
[atguigu@hadoop102 datas]$ touch student.txt
[atguigu@hadoop102 datas]$ vi student.txt
1001 zhangshan
1002 lishi
1003 zhaoliu
注意以tab鍵間隔。
2.Hive實際操作
(1)啟動hive
[atguigu@hadoop102 hive]$ bin/hive
(2)顯示數(shù)據(jù)庫
hive>?show databases;
(3)使用default數(shù)據(jù)庫
hive>?use default;
(4)顯示default數(shù)據(jù)庫中的表
hive>?show tables;
(5)刪除已創(chuàng)建的student表
hive> drop table student;
(6)創(chuàng)建student表, 并聲明文件分隔符’\t’
hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED
?BY '\t';
(7)加載/opt/module/datas/student.txt 文件到student數(shù)據(jù)庫表中。
hive> load data local inpath '/opt/module/datas/student.txt' into table student;
(8)Hive查詢結(jié)果
hive> select * from student;
OK
1001 zhangshan
1002 lishi
1003 zhaoliu
Time taken: 0.266 seconds, Fetched: 3 row(s)
3.遇到的問題
再打開一個客戶端窗口啟動hive,會產(chǎn)生java.sql.SQLException異常。
Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException:
?Unable to instantiate
?org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
????????at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:522)
????????at org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:677)
????????at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:621)
????????at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
????????at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
????????at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
????????at java.lang.reflect.Method.invoke(Method.java:606)
????????at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
????????at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
????????at org.apache.hadoop.hive.metastore.MetaStoreUtils.newInstance(MetaStoreUtils.java:1523)
????????at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.<init>(RetryingMetaStoreClient.java:86)
????????at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.getProxy(RetryingMetaStoreClient.java:132)
????????at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.getProxy(RetryingMetaStoreClient.java:104)
????????at org.apache.hadoop.hive.ql.metadata.Hive.createMetaStoreClient(Hive.java:3005)
????????at org.apache.hadoop.hive.ql.metadata.Hive.getMSC(Hive.java:3024)
????????at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:503)
... 8 more
原因是,Metastore默認存儲在自帶的derby數(shù)據(jù)庫中,推薦使用MySQL存儲Metastore;
2.4 MySql安裝
2.4.1安裝包準(zhǔn)備
1.查看mysql是否安裝,如果安裝了,卸載mysql
(1)查看
[root@hadoop102桌面]# rpm -qa|grep mysql
mysql-libs-5.1.73-7.el6.x86_64
(2)卸載
[root@hadoop102桌面]# rpm -e --nodeps mysql-libs-5.1.73-7.el6.x86_64
2.解壓mysql-libs.zip文件到當(dāng)前目錄
[root@hadoop102 software]# unzip mysql-libs.zip
[root@hadoop102 software]# ls
mysql-libs.zip
mysql-libs
3.進入到mysql-libs文件夾下
[root@hadoop102 mysql-libs]# ll
總用量76048
-rw-r--r--. 1 root root 18509960 3月 ?26 2015 MySQL-client-5.6.24-1.el6.x86_64.rpm
-rw-r--r--. 1 root root ?3575135 12月 ?1 2013 mysql-connector-java-5.1.27.tar.gz
-rw-r--r--. 1 root root 55782196 3月 ?26 2015 MySQL-server-5.6.24-1.el6.x86_64.rpm
2.4.2安裝MySql服務(wù)器
1.安裝mysql服務(wù)端
[root@hadoop102 mysql-libs]# rpm -ivh MySQL-server-5.6.24-1.el6.x86_64.rpm
2.查看產(chǎn)生的隨機密碼
[root@hadoop102 mysql-libs]# cat /root/.mysql_secret
OEXaQuS8IWkG19Xs
3.查看mysql狀態(tài)
[root@hadoop102 mysql-libs]# service mysql status
4.啟動mysql
[root@hadoop102 mysql-libs]# service mysql start
2.4.3安裝MySql客戶端
1.安裝mysql客戶端
[root@hadoop102 mysql-libs]# rpm -ivh MySQL-client-5.6.24-1.el6.x86_64.rpm
2.鏈接mysql
[root@hadoop102 mysql-libs]# mysql -uroot -pOEXaQuS8IWkG19Xs
3.修改密碼
mysql>SET PASSWORD=PASSWORD('000000');
4.退出mysql
mysql>exit
2.4.4 MySql中user表中主機配置
配置只要是root用戶+密碼,在任何主機上都能登錄MySQL數(shù)據(jù)庫。
1.進入mysql
[root@hadoop102 mysql-libs]# mysql -uroot -p000000
2.顯示數(shù)據(jù)庫
mysql>show databases;
3.使用mysql數(shù)據(jù)庫
mysql>use mysql;
4.展示mysql數(shù)據(jù)庫中的所有表
mysql>show tables;
5.展示user表的結(jié)構(gòu)
mysql>desc user;
6.查詢user表
mysql>select User, Host, Password from user;
7.修改user表,把Host表內(nèi)容修改為%
mysql>update user set host='%' where host='localhost';
8.刪除root用戶的其他host
mysql>delete from user where Host='hadoop102';
mysql>delete from user where Host='127.0.0.1';
mysql>delete from user where Host='::1';
9.刷新
mysql>flush privileges;
10.退出
mysql>quit;
2.5 Hive元數(shù)據(jù)配置到MySql
2.5.1驅(qū)動拷貝
1.在/opt/software/mysql-libs目錄下解壓mysql-connector-java-5.1.27.tar.gz驅(qū)動包
[root@hadoop102 mysql-libs]# tar -zxvf mysql-connector-java-5.1.27.tar.gz
2.拷貝/opt/software/mysql-libs/mysql-connector-java-5.1.27目錄下的mysql-connector-java-5.1.27-bin.jar到/opt/module/hive/lib/
[root@hadoop102 mysql-connector-java-5.1.27]# cp mysql-connector-java-5.1.27-bin.jar
?/opt/module/hive/lib/
2.5.2配置Metastore到MySql
1.在/opt/module/hive/conf目錄下創(chuàng)建一個hive-site.xml
[atguigu@hadoop102 conf]$ touch hive-site.xml
[atguigu@hadoop102 conf]$ vi hive-site.xml
2.根據(jù)官方文檔配置參數(shù),拷貝數(shù)據(jù)到hive-site.xml文件中
https://cwiki.apache.org/confluence/display/Hive/AdminManual+MetastoreAdmin
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
??<name>javax.jdo.option.ConnectionURL</name>
??<value>jdbc:mysql://hadoop102:3306/metastore?createDatabaseIfNotExist=true</value>
??<description>JDBC connect string for a JDBC metastore</description>
</property>
<property>
??<name>javax.jdo.option.ConnectionDriverName</name>
??<value>com.mysql.jdbc.Driver</value>
??<description>Driver class name for a JDBC metastore</description>
</property>
<property>
??<name>javax.jdo.option.ConnectionUserName</name>
??<value>root</value>
??<description>username to use against metastore database</description>
</property>
<property>
??<name>javax.jdo.option.ConnectionPassword</name>
??<value>000000</value>
??<description>password to use against metastore database</description>
</property>
</configuration>
3.配置完畢后,如果啟動hive異常,可以重新啟動虛擬機。(重啟后,別忘了啟動hadoop集群)
2.5.3多窗口啟動Hive測試
[atguigu@hadoop102 mysql-libs]$ mysql -uroot -p000000
查看有幾個數(shù)據(jù)庫
mysql> show databases;
+--------------------+
| Database ??????????|
+--------------------+
| information_schema |
| mysql ????????????|
| performance_schema |
| test ??????????????|
+--------------------+
2.再次打開多個窗口,分別啟動hive
[atguigu@hadoop102 hive]$ bin/hive
3.啟動hive后,回到MySQL窗口查看數(shù)據(jù)庫,顯示增加了metastore數(shù)據(jù)庫
mysql> show databases;
+--------------------+
| Database ??????????|
+--------------------+
| information_schema |
| metastore ?????????|
| mysql ????????????|
| performance_schema |
| test ??????????????|
+--------------------+
2.6 HiveJDBC訪問
2.6.1 啟動hiveserver2服務(wù)
[atguigu@hadoop102 hive]$ bin/hiveserver2
2.6.2 啟動beeline
[atguigu@hadoop102 hive]$ bin/beeline
Beeline version 1.2.1 by Apache Hive
beeline>
2.6.3 連接hiveserver2
beeline> !connect jdbc:hive2://hadoop102:10000(回車)
Connecting to jdbc:hive2://hadoop102:10000
Enter username for jdbc:hive2://hadoop102:10000: atguigu(回車)
Enter password for jdbc:hive2://hadoop102:10000: (直接回車)
Connected to: Apache Hive (version 1.2.1)
Driver: Hive JDBC (version 1.2.1)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://hadoop102:10000> show databases;
+----------------+--+
| database_name ?|
+----------------+--+
| default ???????|
| hive_db2 ??????|
+----------------+--+
2.7 Hive常用交互命令
1.“-e”不進入hive的交互窗口執(zhí)行sql語句
[atguigu@hadoop102 hive]$ bin/hive -e "select id from student;"
2.“-f”執(zhí)行腳本中sql語句
(1)在/opt/module/datas目錄下創(chuàng)建hivef.sql文件
[atguigu@hadoop102 datas]$ touch hivef.sql
文件中寫入正確的sql語句
select *from student;
(2)執(zhí)行文件中的sql語句
[atguigu@hadoop102 hive]$ bin/hive -f /opt/module/datas/hivef.sql
(3)執(zhí)行文件中的sql語句并將結(jié)果寫入文件中
[atguigu@hadoop102 hive]$ bin/hive -f /opt/module/datas/hivef.sql ?> /opt/module/datas/hive_result.txt
2.8 Hive其他命令操作
1.退出hive窗口:
hive(default)>exit;
hive(default)>quit;
在新版的hive中沒區(qū)別了,在以前的版本是有的:
exit:先隱性提交數(shù)據(jù),再退出;
quit:不提交數(shù)據(jù),退出;
2.在hive cli命令窗口中如何查看hdfs文件系統(tǒng)
hive(default)>dfs -ls /;
3.在hive cli命令窗口中如何查看本地文件系統(tǒng)
hive(default)>! ls /opt/module/datas;
4.查看在hive中輸入的所有歷史命令
(1)進入到當(dāng)前用戶的根目錄/root或/home/atguigu
(2)查看. hivehistory文件
[atguigu@hadoop102 ~]$ cat .hivehistory
2.9 Hive常見屬性配置
2.9.1 Hive數(shù)據(jù)倉庫位置配置
1)Default數(shù)據(jù)倉庫的最原始位置是在hdfs上的:/user/hive/warehouse路徑下。
2)在倉庫目錄下,沒有對默認的數(shù)據(jù)庫default創(chuàng)建文件夾。如果某張表屬于default數(shù)據(jù)庫,直接在數(shù)據(jù)倉庫目錄下創(chuàng)建一個文件夾。
3)修改default數(shù)據(jù)倉庫原始位置(將hive-default.xml.template如下配置信息拷貝到hive-site.xml文件中)。
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<description>location of default database for the warehouse</description>
</property>
配置同組用戶有執(zhí)行權(quán)限
bin/hdfs dfs -chmod g+w /user/hive/warehouse
2.9.2查詢后信息顯示配置
1)在hive-site.xml文件中添加如下配置信息,就可以實現(xiàn)顯示當(dāng)前數(shù)據(jù)庫,以及查詢表的頭信息配置。
<property>
<name>hive.cli.print.header</name>
<value>true</value>
</property>
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
</property>
2)重新啟動hive,對比配置前后差異。
(1)配置前,如圖6-2所示


2.9.3 Hive運行日志信息配置
1.Hive的log默認存放在/tmp/atguigu/hive.log目錄下(當(dāng)前用戶名下)
2.修改hive的log存放日志到/opt/module/hive/logs
(1)修改/opt/module/hive/conf/hive-log4j.properties.template文件名稱為
hive-log4j.properties
[atguigu@hadoop102 conf]$ pwd
/opt/module/hive/conf
[atguigu@hadoop102 conf]$ mv hive-log4j.properties.template hive-log4j.properties
(2)在hive-log4j.properties文件中修改log存放位置
hive.log.dir=/opt/module/hive/logs
2.9.4參數(shù)配置方式
1.查看當(dāng)前所有的配置信息
hive>set;
2.參數(shù)的配置三種方式
(1)配置文件方式
默認配置文件:hive-default.xml
用戶自定義配置文件:hive-site.xml
注意:用戶自定義配置會覆蓋默認配置。另外,Hive也會讀入Hadoop的配置,因為Hive是作為Hadoop的客戶端啟動的,Hive的配置會覆蓋Hadoop的配置。配置文件的設(shè)定對本機啟動的所有Hive進程都有效。
(2)命令行參數(shù)方式
啟動Hive時,可以在命令行添加-hiveconf param=value來設(shè)定參數(shù)。
例如:
[atguigu@hadoop103 hive]$ bin/hive -hiveconf mapred.reduce.tasks=10;
注意:僅對本次hive啟動有效
查看參數(shù)設(shè)置:
hive (default)> set mapred.reduce.tasks;
(3)參數(shù)聲明方式
可以在HQL中使用SET關(guān)鍵字設(shè)定參數(shù)
例如:
hive (default)> set mapred.reduce.tasks=100;
注意:僅對本次hive啟動有效。
查看參數(shù)設(shè)置
hive (default)> set mapred.reduce.tasks;
上述三種設(shè)定方式的優(yōu)先級依次遞增。即配置文件<命令行參數(shù)<參數(shù)聲明。注意某些系統(tǒng)級的參數(shù),例如log4j相關(guān)的設(shè)定,必須用前兩種方式設(shè)定,因為那些參數(shù)的讀取在會話建立以前已經(jīng)完成了。、
第3章Hive數(shù)據(jù)類型
3.1基本數(shù)據(jù)類型

對于Hive的String類型相當(dāng)于數(shù)據(jù)庫的varchar類型,該類型是一個可變的字符串,不過它不能聲明其中最多能存儲多少個字符,理論上它可以存儲2GB的字符數(shù)。
3.2集合數(shù)據(jù)類型

Hive有三種復(fù)雜數(shù)據(jù)類型ARRAY、MAP 和 STRUCT。ARRAY和MAP與Java中的Array和Map類似,而STRUCT與C語言中的Struct類似,它封裝了一個命名字段集合,復(fù)雜數(shù)據(jù)類型允許任意層次的嵌套。
案例實操
[if !supportLists]1)?[endif]假設(shè)某表有如下一行,我們用JSON格式來表示其數(shù)據(jù)結(jié)構(gòu)。在Hive下訪問的格式為
{
????"name": "songsong",
"friends": ["bingbing" , "lili"] , ??????//列表Array,
"children": { ?????????????????????//鍵值Map,
????????"xiao song": 18 ,
????????"xiaoxiao song": 19
????}
"address": { ?????????????????????//結(jié)構(gòu)Struct,
????????"street": "hui long guan" ,
????????"city": "beijing"
????}
}
2)基于上述數(shù)據(jù)結(jié)構(gòu),我們在Hive里創(chuàng)建對應(yīng)的表,并導(dǎo)入數(shù)據(jù)。
創(chuàng)建本地測試文件test.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
注意:MAP,STRUCT和ARRAY里的元素間關(guān)系都可以用同一個字符表示,這里用“_”。
3)Hive上創(chuàng)建測試表test
create table test(
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';
字段解釋:
row format delimited fields terminated by ',' ?--列分隔符
collection items terminated by '_' ? --MAP STRUCT和 ARRAY 的分隔符(數(shù)據(jù)分割符號)
map keys terminated by ':' -- MAP中的key與value的分隔符
lines terminated by '\n'; --行分隔符
4)導(dǎo)入文本數(shù)據(jù)到測試表
hive (default)> load data local inpath ‘/opt/module/datas/test.txt’into table test
5)訪問三種集合列里的數(shù)據(jù),以下分別是ARRAY,MAP,STRUCT的訪問方式
hive (default)> select friends[1],children['xiao song'],address.city from test
where name="songsong";
OK
_c0 ????_c1 ????city
lili ???18 ?????beijing
Time taken: 0.076 seconds, Fetched: 1 row(s)
3.3類型轉(zhuǎn)化
Hive的原子數(shù)據(jù)類型是可以進行隱式轉(zhuǎn)換的,類似于Java的類型轉(zhuǎn)換,例如某表達式使用INT類型,TINYINT會自動轉(zhuǎn)換為INT類型,但是Hive不會進行反向轉(zhuǎn)化,例如,某表達式使用TINYINT類型,INT不會自動轉(zhuǎn)換為TINYINT類型,它會返回錯誤,除非使用CAST操作。
1.隱式類型轉(zhuǎn)換規(guī)則如下
(1)任何整數(shù)類型都可以隱式地轉(zhuǎn)換為一個范圍更廣的類型,如TINYINT可以轉(zhuǎn)換成INT,INT可以轉(zhuǎn)換成BIGINT。
(2)所有整數(shù)類型、FLOAT和STRING類型都可以隱式地轉(zhuǎn)換成DOUBLE。
(3)TINYINT、SMALLINT、INT都可以轉(zhuǎn)換為FLOAT。
(4)BOOLEAN類型不可以轉(zhuǎn)換為任何其它的類型。
2.可以使用CAST操作顯示進行數(shù)據(jù)類型轉(zhuǎn)換
例如CAST('1' AS INT)將把字符串'1' 轉(zhuǎn)換成整數(shù)1;如果強制類型轉(zhuǎn)換失敗,如執(zhí)行CAST('X' AS INT),表達式返回空值 NULL。
第4章DDL數(shù)據(jù)定義
4.1創(chuàng)建數(shù)據(jù)庫
1)創(chuàng)建一個數(shù)據(jù)庫,數(shù)據(jù)庫在HDFS上的默認存儲路徑是/user/hive/warehouse/*.db。
hive (default)> create database db_hive;
2)避免要創(chuàng)建的數(shù)據(jù)庫已經(jīng)存在錯誤,增加if not exists判斷。(標(biāo)準(zhǔn)寫法)
hive?(default)> create database db_hive;
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Database db_hive already exists
hive (default)> create database if not exists db_hive;
3)創(chuàng)建一個數(shù)據(jù)庫,指定數(shù)據(jù)庫在HDFS上存放的位置
hive (default)> create database db_hive2 location '/db_hive2.db';
4.2查詢數(shù)據(jù)庫
4.2.1顯示數(shù)據(jù)庫
1.顯示數(shù)據(jù)庫
hive> show databases;
2.過濾顯示查詢的數(shù)據(jù)庫
hive> show databases like 'db_hive*';
OK
db_hive
db_hive_1
4.2.2查看數(shù)據(jù)庫詳情
1.顯示數(shù)據(jù)庫信息
hive> desc database db_hive;
OK
db_hive hdfs://hadoop102:9000/user/hive/warehouse/db_hive.db atguiguUSER
2.顯示數(shù)據(jù)庫詳細信息,extended
hive> desc database extended db_hive;
OK
db_hive hdfs://hadoop102:9000/user/hive/warehouse/db_hive.db atguiguUSER
40.3.3切換當(dāng)前數(shù)據(jù)庫
hive (default)> use db_hive;
4.3.3切換當(dāng)前數(shù)據(jù)庫
hive (default)> use db_hive;
4.3修改數(shù)據(jù)庫
用戶可以使用ALTER DATABASE命令為某個數(shù)據(jù)庫的DBPROPERTIES設(shè)置鍵-值對屬性值,來描述這個數(shù)據(jù)庫的屬性信息。數(shù)據(jù)庫的其他元數(shù)據(jù)信息都是不可更改的,包括數(shù)據(jù)庫名和數(shù)據(jù)庫所在的目錄位置。
hive (default)> alter database db_hive set dbproperties('createtime'='20170830');
在hive中查看修改結(jié)果
hive> desc database extended db_hive;
db_name comment location ???????owner_name ?????owner_type ?????parameters
db_hive ????????hdfs://hadoop102:8020/user/hive/warehouse/db_hive.db ???atguigu USER ???{createtime=20170830}
4.4刪除數(shù)據(jù)庫
1.刪除空數(shù)據(jù)庫
hive>drop database db_hive2;
2.如果刪除的數(shù)據(jù)庫不存在,最好采用 if exists判斷數(shù)據(jù)庫是否存在
hive> drop database db_hive;
FAILED: SemanticException [Error 10072]: Database does not exist: db_hive
hive> drop database if exists db_hive2;
3.如果數(shù)據(jù)庫不為空,可以采用cascade命令,強制刪除
hive> drop database db_hive;
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. InvalidOperationException(message:Database db_hive is not empty. One or more tables exist.)
hive> drop database db_hive cascade;
4.5創(chuàng)建表
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]
2.字段解釋說明?
(1)CREATE TABLE 創(chuàng)建一個指定名字的表。如果相同名字的表已經(jīng)存在,則拋出異常;用戶可以用 IF NOT EXISTS 選項來忽略這個異常。
(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ù)的位置做任何改變。在刪除表的時候,內(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
DELIMITED [FIELDS TERMINATED BY char] [COLLECTION ITEMS TERMINATED BY char]
????????[MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]
???| SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]
用戶在建表的時候可以自定義SerDe或者使用自帶的SerDe。如果沒有指定ROW FORMAT 或者ROW FORMAT DELIMITED,將會使用自帶的SerDe。在建表的時候,用戶還需要為表指定列,用戶在指定表的列的同時也會指定自定義的SerDe,Hive通過SerDe確定表的具體的列的數(shù)據(jù)。
SerDe是Serialize/Deserilize的簡稱,目的是用于序列化和反序列化。
(8)STORED AS指定存儲文件類型
常用的存儲文件類型:SEQUENCEFILE(二進制序列文件)、TEXTFILE(文本)、RCFILE(列式存儲格式文件)
如果文件數(shù)據(jù)是純文本,可以使用STORED AS TEXTFILE。如果數(shù)據(jù)需要壓縮,使用 STORED AS SEQUENCEFILE。
(9)LOCATION :指定表在HDFS上的存儲位置。
(10)LIKE允許用戶復(fù)制現(xiàn)有的表結(jié)構(gòu),但是不復(fù)制數(shù)據(jù)。
4.5.1管理表
1.理論
默認創(chuàng)建的表都是所謂的管理表,有時也被稱為內(nèi)部表。因為這種表,Hive會(或多或少地)控制著數(shù)據(jù)的生命周期。Hive默認情況下會將這些表的數(shù)據(jù)存儲在由配置項hive.metastore.warehouse.dir(例如,/user/hive/warehouse)所定義的目錄的子目錄下。 當(dāng)我們刪除一個管理表時,Hive也會刪除這個表中數(shù)據(jù)。管理表不適合和其他工具共享數(shù)據(jù)。
2.案例實操
(1)普通創(chuàng)建表
create table if not exists student2(
id int, name string
)
row format delimited fields terminated by '\t'
stored as textfile
location '/user/hive/warehouse/student2';
(2)根據(jù)查詢結(jié)果創(chuàng)建表(查詢的結(jié)果會添加到新創(chuàng)建的表中)
create table if not exists student3?as select id, name from student;
(3)根據(jù)已經(jīng)存在的表結(jié)構(gòu)創(chuàng)建表
create table if not exists student4 like student;
(4)查詢表的類型
hive (default)> desc formatted student2;
Table Type: ????????????MANAGED_TABLE ?
4.5.2外部表
1.理論
因為表是外部表,所以Hive并非認為其完全擁有這份數(shù)據(jù)。刪除該表并不會刪除掉這份數(shù)據(jù),不過描述表的元數(shù)據(jù)信息會被刪除掉。
2.管理表和外部表的使用場景
每天將收集到的網(wǎng)站日志定期流入HDFS文本文件。在外部表(原始日志表)的基礎(chǔ)上做大量的統(tǒng)計分析,用到的中間表、結(jié)果表使用內(nèi)部表存儲,數(shù)據(jù)通過SELECT+INSERT進入內(nèi)部表。