Hive概述
Hive是基于Hadoop的一個數(shù)據(jù)倉庫工具,可以將結(jié)構(gòu)化的數(shù)據(jù)文件映射為一張表设江,并提供類SQL查詢功能惯裕。
Hive本質(zhì)是:將HQL轉(zhuǎn)化成MapReduce程序。
Hive處理的數(shù)據(jù)存儲在HDFS中绣硝,分析數(shù)據(jù)底層的實現(xiàn)可以是MapReduce蜻势、tes或者Spark,其執(zhí)行程序運行在Yarn上鹉胖。
Hive優(yōu)缺點
優(yōu)點:
- 使用簡單握玛,類SQL語法易于使用。
- 可擴展性甫菠,可以隨時擴展集群規(guī)模挠铲。
- 延展性,支持自定義函數(shù)寂诱。
- 無需開發(fā)MapReduce程序拂苹。
缺點:
- 效率低延遲高,對處理大數(shù)據(jù)有優(yōu)勢痰洒。
- 不支持記錄級別的增刪改操作瓢棒。
- 不支持事物。
- 調(diào)優(yōu)困難丘喻。
Hive安裝
只在集群中的主節(jié)點服務(wù)器中進行安裝配置即可脯宿,安裝包可以去官方主頁https://hive.apache.org/進行下載:
使用Xftp將安裝包上傳到hadoop-1的/usr目錄下:
進入/user目錄,使用tar命令將壓縮包進行解壓泉粉,執(zhí)行命令:
# tar zxvf apache-hive-2.3.6-bin.tar.gz
解壓完成后會在/usr目錄下生成apache-hive-2.3.6-bin目錄:
使用vim編輯環(huán)境變量:
# vim /etc/profile
新增內(nèi)容如下:
export HIVE_HOME=/usr/apache-hive-2.3.6-bin
export PATH=$HIVE_HOME/bin:$PATH
保存退出连霉,榴芳,執(zhí)行命令使修改生效:
# source /etc/profile
Hive配置
使用如下命令,在HDFS中創(chuàng)建/root/hive和/root/hive/warehouse兩個目錄:
# hadoop fs -mkdir -p /root/tmp
# hadoop fs -mkdir -p /root/hive/warehouse
使用如下命令,為目錄賦予權(quán)限:
# hadoop fs -chmod a+w /root/tmp
# hadoop fs -chmod a+w /root/hive/warehouse
hive-site.xml文件配置
使用如下命令,進入Hive配置文件目錄嘁灯,查看文件:
# cd /usr/apache-hive-2.3.6-bin/conf
# ll
現(xiàn)在沒有hive-site.xml文件汇陆,使用如下命令,拷貝hive-default.xml.template文件為hive-site.xml文件:
# cp hive-default.xml.template hive-site.xml
使用vim編輯hive-site.xml文件:
# vim hive-site.xml
將配置文件中的內(nèi)容做如下更改:
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/root/hive/warehouse</value>
<description>location of default database for the warehouse</description>
</property>
<property>
<name>hive.exec.scratchdir</name>
<value>/root/tmp</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>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost:3306/metastore_db?createDatabaseIfNotExist=true&useSSL=false</value>
<description>
JDBC connect string for a JDBC metastore.
To use SSL to encrypt/authenticate the connection, provide database-specific SSL flag in the connection URL.
For example, jdbc:postgresql://myhost/db?ssl=true for postgres database.
</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>Password@123!</value>
<description>password to use against metastore database</description>
</property>
<property>
<name>hive.metastore.schema.verification</name>
<value>true</value>
<description>
Enforce metastore schema version consistency.
True: Verify that version information stored in is compatible with one from Hive jars. Also disable automatic
schema migration attempt. Users are required to manually migrate schema after Hive upgrade which ensures
proper metastore schema migration. (Default)
False: Warn if the version information stored in metastore doesn't match with one from in Hive jars.
</description>
</property>
然后將該配置文件中的所有${system:java.io.tmpdir}替換為/usr/apache-hive-2.3.6-bin/tmp;
將所有的${system:user.name}替換為root。
使用如下命令,在/usr/apache-hive-2.3.6-bin目錄下創(chuàng)建tmp目錄谍夭,并賦予權(quán)限:
# mkdir /usr/apache-hive-2.3.6-bin/tmp
# chmod a+w /usr/apache-hive-2.3.6-bin/tmp
hive-env.sh文件配置
使用如下命令,拷貝hive-env.sh.template文件為hive-env.sh文件:
# cp hive-env.sh.template hive-env.sh
使用vim編輯hive-env.sh文件:
# vim hive-env.sh
添加Hadoop的安裝路徑:
HADOOP_HOME=/usr/hadoop-2.7.7
添加數(shù)據(jù)庫驅(qū)動包
由于Hive默認(rèn)使用derby數(shù)據(jù)庫存儲元數(shù)據(jù)憨募,只能單一訪問(不能同時打開兩個Hive客戶端)紧索,所以此處使用本機安裝的MySQL 5.7數(shù)據(jù)庫,前文已經(jīng)記錄MySQL的安裝(前文連接:http://www.reibang.com/p/a40c70791cb5)菜谣,所以將數(shù)據(jù)庫連接的驅(qū)動包放到/usr/apache-hive-2.3.6-bin/lib目錄下:
Hive使用
使用Hive前珠漂,保證Hadoop和MySQL數(shù)據(jù)庫已經(jīng)啟動完成狀態(tài)。
執(zhí)行如下命令尾膊,進行MySQL的初始化(只需安裝配置完畢首次使用執(zhí)行):
# schematool -initSchema -dbType mysql
登錄數(shù)據(jù)庫媳危,使用如下命令進行查詢:
> show databases;
可以看到在hive-site.xml中配置的數(shù)據(jù)庫metastore_db已經(jīng)創(chuàng)建:
使用如下命令進行數(shù)據(jù)庫metastore_db的表查詢:
> use metastore_db;
> show tables;
可以查詢到初始化數(shù)據(jù)庫生成的Hive相關(guān)的表:
使用如下命令進入Hive:
# hive
Hive測試
查看數(shù)據(jù)庫:
> show databases;
創(chuàng)建數(shù)據(jù)庫:
> create database testhive;
進入某數(shù)據(jù)庫:
> use testhive;
顯示某數(shù)據(jù)庫的表信息:
> show tables;
創(chuàng)建數(shù)據(jù)庫表:
> create table testtable(id int,name string,age int) row format delimited fields terminated by ' ' lines terminated by '\n';
查看數(shù)據(jù)庫表結(jié)構(gòu):
> desc testtable;
刪除某數(shù)據(jù)庫:
> drop database if exists testhive;
Hive加載本地文件數(shù)據(jù),在/home目錄下創(chuàng)建一個test.txt文件冈敛,寫入以下內(nèi)容:
1 Dcl_Snow 18
2 Dcl 19
3 Snow 20
執(zhí)行如下命令待笑,將文件中的數(shù)據(jù)加載到testhive數(shù)據(jù)庫中的testtable表中:
> load data local inpath '/home/test.txt' into table testhive.testtable;
查看表中數(shù)據(jù):
> select * from testhive.testtable;
建表時的分隔符,換行符抓谴,都要與test.txt中數(shù)據(jù)的分隔符和換行符相同暮蹂,否則查詢表數(shù)據(jù)時,會顯示數(shù)據(jù)都是NULL癌压。