Hive概述
Hive是基於Hadoop的一個數據倉庫工具,可以將結構化的數據文件映射為一張表,並提供類SQL查詢功能。
Hive本質是:將HQL轉化成MapReduce程序。
Hive處理的數據存儲在HDFS中,分析數據底層的實現可以是MapReduce、tes或者Spark,其執行程序運行在Yarn上。
Hive優缺點
優點:
1.使用簡單,類SQL語法易於使用。
2.可擴展性,可以隨時擴展集群規模。
3.延展性,支持自定義函數。
4.無需開發MapReduce程序。
缺點:
1.效率低延遲高,對處理大數據有優勢。
2.不支持記錄級別的增刪改操作。
3.不支持事物。
4.調優困難。
Hive安裝
只在集群中的主節點服務器中進行安裝配置即可,安裝包可以去官方主頁https://hive.apache.org/進行下載:
使用Xftp將安裝包上傳到hadoop-1的/usr目錄下:
進入/user目錄,使用tar命令將壓縮包進行解壓,執行命令:
1 # tar zxvf apache-hive-2.3.6-bin.tar.gz
解壓完成后會在/usr目錄下生成apache-hive-2.3.6-bin目錄:

使用vim編輯環境變量:
1 # vim /etc/profile
新增內容如下:
1 export HIVE_HOME=/usr/apache-hive-2.3.6-bin 2 export PATH=$HIVE_HOME/bin:$PATH

保存退出,,執行命令使修改生效:
1 # source /etc/profile
Hive配置
使用如下命令,在HDFS中創建/root/hive和/root/hive/warehouse兩個目錄:
1 # hadoop fs -mkdir -p /root/tmp 2 # hadoop fs -mkdir -p /root/hive/warehouse

使用如下命令,為目錄賦予權限:
1 # hadoop fs -chmod a+w /root/tmp 2 # hadoop fs -chmod a+w /root/hive/warehouse
hive-site.xml文件配置
使用如下命令,進入Hive配置文件目錄,查看文件:
1 # cd /usr/apache-hive-2.3.6-bin/conf 2 # ll

現在沒有hive-site.xml文件,使用如下命令,拷貝hive-default.xml.template文件為hive-site.xml文件:
1 # cp hive-default.xml.template hive-site.xml

使用vim編輯hive-site.xml文件:
1 # vim hive-site.xml

將配置文件中的內容做如下更改:
1 <property> 2 <name>hive.metastore.warehouse.dir</name> 3 <value>/root/hive/warehouse</value> 4 <description>location of default database for the warehouse</description> 5 </property> 6 <property> 7 <name>hive.exec.scratchdir</name> 8 <value>/root/tmp</value> 9 <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> 10 </property> 11 <property> 12 <name>javax.jdo.option.ConnectionURL</name> 13 <value>jdbc:mysql://localhost:3306/metastore_db?createDatabaseIfNotExist=true&useSSL=false</value> 14 <description> 15 JDBC connect string for a JDBC metastore. 16 To use SSL to encrypt/authenticate the connection, provide database-specific SSL flag in the connection URL. 17 For example, jdbc:postgresql://myhost/db?ssl=true for postgres database. 18 </description> 19 </property> 20 <property> 21 <name>javax.jdo.option.ConnectionDriverName</name> 22 <value>com.mysql.jdbc.Driver</value> 23 <description>Driver class name for a JDBC metastore</description> 24 </property> 25 <property> 26 <name>javax.jdo.option.ConnectionUserName</name> 27 <value>root</value> 28 <description>Username to use against metastore database</description> 29 </property> 30 <property> 31 <name>javax.jdo.option.ConnectionPassword</name> 32 <value>Password@123!</value> 33 <description>password to use against metastore database</description> 34 </property> 35 <property> 36 <name>hive.metastore.schema.verification</name> 37 <value>true</value> 38 <description> 39 Enforce metastore schema version consistency. 40 True: Verify that version information stored in is compatible with one from Hive jars. Also disable automatic 41 schema migration attempt. Users are required to manually migrate schema after Hive upgrade which ensures 42 proper metastore schema migration. (Default) 43 False: Warn if the version information stored in metastore doesn't match with one from in Hive jars. 44 </description> 45 </property>
然后將該配置文件中的所有\${system:java.io.tmpdir}替換為/usr/apache-hive-2.3.6-bin/tmp;
將所有的${system:user.name}替換為root。
使用如下命令,在/usr/apache-hive-2.3.6-bin目錄下創建tmp目錄,並賦予權限:
1 # mkdir /usr/apache-hive-2.3.6-bin/tmp 2 # chmod a+w /usr/apache-hive-2.3.6-bin/tmp

hive-site.sh文件配置
使用如下命令,拷貝hive-env.sh.template文件為hive-env.sh文件:
1 # cp hive-env.sh.template hive-env.sh
使用vim編輯hive-env.sh文件:
1 # vim hive-env.sh
添加Hadoop的安裝路徑:
1 HADOOP_HOME=/usr/hadoop-2.7.7

添加數據庫驅動包
由於Hive默認使用derby數據庫存儲元數據,只能單一訪問(不能同時打開兩個Hive客戶端),所以此處使用本機安裝的MySQL 5.7數據庫,前文已經記錄MySQL的安裝(前文連接: https://www.cnblogs.com/Dcl-Snow/p/11969388.html),所以將數據庫連接的驅動包放到/usr/apache-hive-2.3.6-bin/lib目錄下:

Hive使用
使用Hive前,保證Hadoop和MySQL數據庫已經啟動完成狀態。
執行如下命令,進行MySQL的初始化(只需安裝配置完畢首次使用執行):
1 # schematool -initSchema -dbType mysql

登錄數據庫,使用如下命令進行查詢:
1 > show databases;
可以看到在hive-site.xml中配置的數據庫metastore_db已經創建:

使用如下命令進行數據庫metastore_db的表查詢:
1 > use metastore_db; 2 > show tables;
可以查詢到初始化數據庫生成的Hive相關的表:

使用如下命令進入Hive:
1 # hive

Hive測試
查看數據庫:
1 > show databases;
創建數據庫:
1 > create database testhive;
進入某數據庫:
1 > use testhive;
顯示某數據庫的表信息:
1 > show tables;
創建數據庫表:
1 > create table testtable(id int,name string,age int) row format delimited fields terminated by ' ' lines terminated by '\n';
查看數據庫表結構:
1 > desc testtable;
刪除某數據庫:
1 > drop database if exists testhive;
Hive加載本地文件數據,在/home目錄下創建一個test.txt文件,寫入以下內容:
1 1 Dcl_Snow 18 2 2 Dcl 19 3 3 Snow 20
執行如下命令,將文件中的數據加載到testhive數據庫中的testtable表中:
1 > load data local inpath '/home/test.txt' into table testhive.testtable;
查看表中數據:
1 > select * from testhive.testtable;

注意:
建表時的分隔符,換行符,都要與test.txt中數據的分隔符和換行符相同,否則查詢表數據時,會顯示數據都是NULL。