一释簿、Hive簡介
-
什么是Hive
- Hive是基于Hadoop的一個數據倉庫工具,可以將結構化的數據文件映射為一張數據庫表论熙,并 提供類SQL查詢功能隔盛。
- 本質是將SQL轉換為MapReduce程序旦签。
- Hive本身不存儲數據,完全依賴于HDFS和MapReduce肪跋,Hive可以將結構化的數據文件映射為 一張數據庫表歧蒋,Hive中表純邏輯,就是表的元數據。而Hbase是物理表谜洽,定位是NoSQL萝映。
-
為什么使用Hive
- 操作接口采用類SQL語法,提供快速開發(fā)的能力阐虚。
- 避免了去寫MapReduce序臂,減少開發(fā)人員的學習成本。
- 擴展功能很方便敌呈。
-
Hive的特點
- 可擴展
Hive可以自由的擴展集群的規(guī)模贸宏,一般情況下不需要重啟服務。 - 延展性
Hive支持用戶自定義函數磕洪,用戶可以根據自己的需求來實現自己的函數吭练。 - 容錯
良好的容錯性,節(jié)點出現問題SQL仍可完成執(zhí)行析显。
- 可擴展
-
Hive的運行模式
- 內嵌模式
將元數據保存在本地內嵌的 Derby 數據庫中鲫咽,這是使用Hive最簡單的方式。但是這種方式缺點也比較明顯谷异,因為一個內嵌的 Derby 數據庫每次只能訪問一個數據文件分尸,這也就意味著它不支持多會話連接。 - 本地模式
這種模式是將元數據保存在本地獨立的數據庫中(一般是MySQL)歹嘹,這用就可以支持多會話和多用戶連接了箩绍。 - 遠程模式
此模式應用于 Hive客戶端較多的情況。把MySQL數據庫獨立出來尺上,將元數據保存在遠端獨立的 MySQL服務中材蛛,避免了在每個客戶端都安裝MySQL服務從而造成冗余浪費的情況。
- 內嵌模式
二怎抛、安裝與配置
首先要安裝hadoop
略卑吭。-
下載hive
網址:http://hive.apache.org/downloads.html[hadoop@master ~]$ wget http://www-eu.apache.org/dist/hive/stable-2/apache-hive-2.1.1-bin.tar.gz [hadoop@master ~]$ tar -xvf apache-hive-2.1.1-bin.tar.gz [hadoop@master ~]$ cd apache-hive-2.1.1-bin [hadoop@master apache-hive-2.1.1-bin]$ ls bin conf examples hcatalog jdbc lib LICENSE NOTICE README.txt RELEASE_NOTES.txt scripts [hadoop@master apache-hive-2.1.1-bin]$ pwd /home/hadoop/apache-hive-2.1.1-bin
-
設置環(huán)境變量
[hadoop@master apache-hive-2.1.1-bin]$ vim ~/.bash_profile export HIVE_HOME=/home/hadoop/apache-hive-2.1.1-bin export PATH=$HIVE_HOME/bin:$PATH [hadoop@master apache-hive-2.1.1-bin]$ . ~/.bash_profile
-
內嵌模式
-
修改 Hive 配置文件
$HIVE_HOME/conf對應的是Hive的配置文件路徑,該路徑下的hive-site.xml是Hive工程的配置文件。默認情況下马绝,該文件并不存在豆赏,我們需要拷貝它的模版來實現:[hadoop@master conf]$ cp hive-default.xml.template hive-site.xml
hive-site.xml 的主要配置有:
#該參數指定了 Hive 的數據存儲目錄,默認位置在 HDFS 上面的 /user/hive/warehouse 路徑下富稻。 <property> <name>hive.metastore.warehouse.dir</name> <value>/user/hive/warehouse</value> <description>location of default database for the warehouse</description> </property> #該參數指定了 Hive 的數據臨時文件目錄掷邦,默認位置為 HDFS 上面的 /tmp/hive 路徑下。 <property> <name>hive.exec.scratchdir</name> <value>/tmp/hive</value> <description>HDFS root scratch dir for Hive jobs which gets created with write all (733) permission. For each connecting user, an HDFS scratch dir: ${hive.exec.scratchdir}/<username> is created, with ${hive.scratch.dir.permission}.</description> </property>
修改 Hive 目錄下 /conf/hive-env.sh 文件
[hadoop@master conf]$ cp hive-env.sh.template hive-env.sh # Set HADOOP_HOME to point to a specific hadoop install directory HADOOP_HOME=/home/hadoop/hadoop-2.7.3 # Hive Configuration Directory can be controlled by: export HIVE_CONF_DIR=/home/hadoop/apache-hive-2.1.1-bin/conf # Folder containing extra ibraries required for hive compilation/execution can be controlled by: export HIVE_AUX_JARS_PATH=/home/hadoop/apache-hive-2.1.1-bin/lib
-
創(chuàng)建必要目錄
[hadoop@master ~]$ hdfs dfs -ls / Found 3 items drwx------ - hadoop supergroup 0 2017-04-06 18:01 /tmp drwxr-xr-x - hadoop supergroup 0 2017-04-06 17:58 /user drwxr-xr-x - hadoop supergroup 0 2017-04-06 17:58 /usr [hadoop@master ~]$ hdfs dfs -ls /user Found 1 items drwxr-xr-x - hadoop supergroup 0 2017-04-08 11:00 /user/hadoop #創(chuàng)建目錄 [hadoop@master ~]$ hdfs dfs -mkdir -p /user/hive/warehouse [hadoop@master ~]$ hdfs dfs -mkdir -p /tmp/hive #賦予寫權限 [hadoop@master ~]$ hdfs dfs -chmod a+w /tmp/hive [hadoop@master ~]$ hdfs dfs -chmod a+w /user/hive/warehouse
-
修改 io.tmpdir 路徑
同時唉窃,要修改 hive-site.xml 中所有包含 ${system:java.io.tmpdir} 字段的 value 即路徑耙饰,你可以自己新建一個目錄來替換它,例如 /home/Hadoop/cloud/apache-hive-2.1.1-bin/iotmp纹份。然后使用vim全局替換命令替換#這里是本地路徑苟跪,不是hdfs路徑 [hadoop@master conf]$ mkdir /home/hadoop/apache-hive-2.1.1-bin/iotmp #vim全局替換 %s#${system:java.io.tmpdir}#/home/hadoop/cloud/apache-hive-2.1.1-bin/iotmp#g #還需要將如下的system:刪除 ${system:java.io.tmpdir}/${ system:user.name}
-
三廷痘、運行Hive
-
初始化
#首先要運行服務metastore [hadoop@master apache-hive-2.1.1-bin]$ hive --service metastore #初始化derby [hadoop@master apache-hive-2.1.1-bin]$ schematool -initSchema -dbType derby #啟動hive [hadoop@master apache-hive-2.1.1-bin]$ hive hive>
重新初始化derby需要刪除目錄:
[hadoop@master apache-hive-2.1.1-bin]$ rm -rf metastore_db/
-
創(chuàng)建數據庫
hive> create database db_hive_test; OK Time taken: 0.282 seconds
-
切換到新建數據庫并查看databases
hive> use db_hive_test; OK Time taken: 0.016 seconds hive> show databases; OK db_hive_test default Time taken: 0.013 seconds, Fetched: 2 row(s)
-
創(chuàng)建測試表
hive> create table student(id int,name string) row format delimited fields terminated by '\t'; OK Time taken: 0.4 seconds hive> desc student; OK id int name string Time taken: 0.052 seconds, Fetched: 2 row(s)
-
裝載本地數據到Hive測試表
#先在本地創(chuàng)建測試文件student.txt [hadoop@master hive]$ cat student.txt 1 zhangsan 2 baiqio 333 aaadf #上傳并加載測試文件到Hive表 hive> load data local inpath '~/hive/student.txt' into table db_hive_test.student; FAILED: SemanticException Line 1:23 Invalid path ''~/hive/student.txt'': No files matching path file:/home/hadoop/apache-hive-2.1.1-bin/~/hive/student.txt hive> load data local inpath 'hive/student.txt' into table db_hive_test.student; FAILED: SemanticException Line 1:23 Invalid path ''hive/student.txt'': No files matching path file:/home/hadoop/apache-hive-2.1.1-bin/hive/student.txt hive> load data local inpath '../hive/student.txt' into table db_hive_test.student; Loading data to table db_hive_test.student OK Time taken: 0.675 seconds
-
操作student表
hive> select * from student; OK 1 zhangsan 2 baiqio 333 aaadf Time taken: 1.006 seconds, Fetched: 3 row(s) hive> select * from student where id=1; OK 1 zhangsan Time taken: 0.445 seconds, Fetched: 1 row(s)
-
本地文件student.txt上傳到hdfs路徑
[hadoop@master hive]$ hdfs dfs -ls /user/hive/warehouse Found 1 items drwxrwxrwx - hadoop supergroup 0 2017-04-08 14:37 /user/hive/warehouse/db_hive_test.db [hadoop@master hive]$ hdfs dfs -ls /user/hive/warehouse/db_hive_test.db Found 1 items drwxrwxrwx - hadoop supergroup 0 2017-04-08 14:47 /user/hive/warehouse/db_hive_test.db/student [hadoop@master hive]$ hdfs dfs -ls /user/hive/warehouse/db_hive_test.db/student Found 1 items -rwxrwxrwx 2 hadoop supergroup 30 2017-04-08 14:47 /user/hive/warehouse/db_hive_test.db/student/student.txt
-
從HDFS文件導入數據到Hive
#先上傳文件 [hadoop@master hive]$ cat student.txt 4 zhangsan 5 baiqio 6 aaadf [hadoop@master hive]$ hdfs dfs -mkdir hive [hadoop@master hive]$ hdfs dfs -put student.txt hive/student.txt2 #再導入數據 hive> load data inpath 'hive/student.txt2' into table student; Loading data to table db_hive_test.student OK Time taken: 0.324 seconds hive> select * from student; OK 1 zhangsan 2 baiqio 333 aaadf 4 zhangsan 5 baiqio 6 aaadf Time taken: 0.21 seconds, Fetched: 6 row(s)
-
Hive查詢結果導出到文件
#注意:第一行無分號 hive> insert overwrite local directory '/home/wyp/Documents/result' hive> select * from test; #還可以導出到HDFS文件系統 hive> insert overwrite directory '/home/wyp/Documents/result' hive> select * from test; #最好指定列分隔符 hive> insert overwrite local directory '/home/wyp/Documents/result' hive> row format delimited hive> fields terminated by '\t' hive> select * from test;
將數據抽象成數據庫表后,對數據的操作和統計是非常方便的件已。