參考資料:
Hive3.1.2安裝指南_廈大數(shù)據(jù)庫(kù)實(shí)驗(yàn)室博客
Hive學(xué)習(xí)(一) 安裝 環(huán)境:CentOS 7 + Hadoop3.2 + Hive3.1 - 一個(gè)人、一座城 - 博客園
1.安裝hive
1.1下載地址hive
鏡像路徑
http://www.Apache.org/dyn/closer.cgi/hive 或者 https://mirrors.bfsu.edu.cn/apache/hive/hive-3.1.2/
wget https://mirrors.bfsu.edu.cn/apache/hive/hive-3.1.2/apache-hive-3.1.2-bin.tar.gz .
1.2.解壓到安裝路徑
tar -zxvf ./apache-hive-3.1.2-bin.tar.gz -C /usr/local
cd /usr/local
sudo mv apache-hive-3.1.2-bin hive
1.3.設(shè)置hive路徑
vi ~/.bash_profile
export HIVE_HOME=/usr/local/hive
export HIVE_CONF_DIR=${HIVE_HOME}/conf
export PATH=$PATH:$HIVE_HOME/bin
source ~/.bash_profile
1.4.修改/usr/local/hive/conf下的hive-site.xml
cd /usr/local/hive/conf
mv hive-default.xml.template hive-default.xml
vim hive-site.xml
<configuration>
<property>
<name>JAVAx.jdo.option.ConnectionURL</name>
<value>jdbc:MySQL://localhost:3306/hive?createDatabaseIfNotExist=true&useSSL=false</value>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>hive</value>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>hiveMhxzKhl88!</value>
</property>
<!--hive工作的hdfs臨時(shí)存儲(chǔ)空間-->
<property>
<name>hive.exec.scratchdir</name>
<value>/tmp/hive</value>
</property>
<!--hive工作的本地臨時(shí)存儲(chǔ)空間-->
<property>
<name>hive.exec.local.scratchdir</name>
<value>/home/xxx/hive/tmp</value>
</property>
<!--如果啟用了日志功能,則存儲(chǔ)操作日志的頂級(jí)目錄-->
<property>
<name>hive.server2.logging.operation.log.location</name>
<value>home/xxx/hive/tmp/operation_logs</value>
</property>
<!--Hive運(yùn)行時(shí)結(jié)構(gòu)化日志文件的位置-->
<property>
<name>hive.querylog.location</name>
<value>/home/xxx/hive/tmp</value>
</property>
<!--用于在遠(yuǎn)程文件系統(tǒng)中添加資源的臨時(shí)本地目錄-->
<property>
<name>hive.downloaded.resources.dir</name>
<value>/home/xxx/hive/tmp/${hive.session.id}_resources</value>
</property>
<property>
<name>hive.server2.authentication</name>
<value>NONE</value>
</property>
<property>
<name>dfs.permissions.enabled</name>
<value>false</value>
</property>
<property>
<name>hive.server2.enable.doAs</name>
<value>FALSE</value>
</property>
</configuration>
1.5 hive-env.sh
cp hive-env.sh.template hive-env.sh
#并編輯
export HIVE_CONF_DIR=${HIVE_HOME}/conf
export HIVE_AUX_JARS_PATH=${HIVE_HOME}/conf/lib
1.5 拷貝mysql驅(qū)動(dòng)包
wget https://cdn.mysql.com//archives/mysql-connector-java-5.1/mysql-connector-java-5.1.46.zip
unzip mysql-connector-java-5.1.46.zip
cp mysql-connector-java-5.1.46-bin.jar ${HIVE_HOME}/conf/lib
2.安裝mysql
2.1下載myql的版本
https://downloads.mysql.com/archives/community/
yum -y install mysql-community-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
No package mysql-community-server available.
Error: Nothing to do
如果yum沒(méi)辦法安裝,需要單獨(dú)下載安裝
mysql-community-release-el7-5.noarch.rpm
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
rpm -ivh mysql57-community-release-el7-10.noarch.rpm
yum install mysql-serve
查詢(xún)是否安裝ok
rpm -qa|grep mysql
修改默認(rèn)字符集
vi /etc/my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
systemctl restart mysqld.service
2.2 啟動(dòng)mysql服務(wù)
systemctl start mysqld.service
2.3 設(shè)置登錄密碼
查看臨時(shí)密碼
grep 'temporary password' /var/log/mysqld.log
2021-01-07T10:48:25.445856Z 1 [Note] A temporary password is generated for root@localhost: K:3q5rpb4+8R
試著用初始密碼登錄
mysql -p
Enter password:
設(shè)置初始密碼
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MhxzKhl88!'
2.4 新建hive數(shù)據(jù)庫(kù)。
這個(gè)hive數(shù)據(jù)庫(kù)與hive-site.xml中l(wèi)ocalhost:3306/hive的hive對(duì)應(yīng),用來(lái)保存hive元數(shù)據(jù)
mysql> create database hive;
. 配置mysql允許hive接入:
注意yourname是你當(dāng)前的用戶(hù)
mysql> grant all on *.* to hive@localhost identified by 'hiveMhxzKhl88!';
#將所有數(shù)據(jù)庫(kù)的所有表的所有權(quán)限賦給hive用戶(hù),by后面的是配置hive-site.xml中配置的連接密碼
mysql> flush privileges; #刷新mysql系統(tǒng)權(quán)限關(guān)系表
初始化數(shù)據(jù)庫(kù)
schematool -dbType mysql -initSchema
如果沒(méi)有初始化執(zhí)行命令的時(shí)候會(huì)報(bào)錯(cuò)
FAILED: HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.me
關(guān)于mysql的授權(quán)命令
use mysql;
#給某個(gè)用戶(hù)授權(quán)
格式:grant 權(quán)限 on 數(shù)據(jù)庫(kù).* to 用戶(hù)名@登錄主機(jī) identified by "密碼";
grant all privileges on testDB.* to test@localhost identified by '1234';
#如果想指定部分權(quán)限給一用戶(hù),可以這樣來(lái)寫(xiě):
grant select,update on testDB.* to test@localhost identified by '1234';
#刪除某個(gè)授權(quán)的用戶(hù)
Delete FROM user Where User='test' and Host='localhost';
#修改密碼
update mysql.user set password=password('新密碼') where User="test" and Host="localhost";
3.hive sql入門(mén)
3.1 Hive基本數(shù)據(jù)類(lèi)型
Hive支持基本數(shù)據(jù)類(lèi)型和復(fù)雜類(lèi)型, 基本數(shù)據(jù)類(lèi)型主要有數(shù)值類(lèi)型(INT、FLOAT、DOUBLE ) 、布爾型和字符串, 復(fù)雜類(lèi)型有三種:ARRAY、MAP 和 STRUCT。
a.基本數(shù)據(jù)類(lèi)型
- TINYINT: 1個(gè)字節(jié)
- SMALLINT: 2個(gè)字節(jié)
- INT: 4個(gè)字節(jié)
- BIGINT: 8個(gè)字節(jié)
- BOOLEAN: TRUE/FALSE
- FLOAT: 4個(gè)字節(jié),單精度浮點(diǎn)型
- DOUBLE: 8個(gè)字節(jié),雙精度浮點(diǎn)型
- STRING 字符串
b.復(fù)雜數(shù)據(jù)類(lèi)型
- ARRAY: 有序字段
- MAP: 無(wú)序字段
- STRUCT: 一組命名的字段
3.2 常用的HiveQL操作命令
3.2.1 庫(kù)操作
create database if not exists testdb; #創(chuàng)建數(shù)據(jù)庫(kù)
show databases; #查看Hive中包含數(shù)據(jù)庫(kù)
show databases like 'h.*'; #查看Hive中以h開(kāi)頭數(shù)據(jù)庫(kù)
describe databases; #查看hive數(shù)據(jù)庫(kù)位置等信息
alter database testdb set dbproperties; #為hive設(shè)置鍵值對(duì)屬性
use testdb; #切換到hive數(shù)據(jù)庫(kù)下
drop database if exists testdb; #刪除不含表的數(shù)據(jù)庫(kù)
drop database if exists testdb cascade; #刪除數(shù)據(jù)庫(kù)和它中的表
3.2.2 表操作
1.創(chuàng)建表
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]
- CREATE TABLE 創(chuàng)建一個(gè)指定名字的表。如果相同名字的表已經(jīng)存在,則拋出異常;用戶(hù)可以用 IF NOT EXIST 選項(xiàng)來(lái)忽略這個(gè)異常
- EXTERNAL 關(guān)鍵字可以讓用戶(hù)創(chuàng)建一個(gè)外部表,在建表的同時(shí)指定一個(gè)指向?qū)嶋H數(shù)據(jù)的路徑(LOCATION)
- LIKE 允許用戶(hù)復(fù)制現(xiàn)有的表結(jié)構(gòu),但是不復(fù)制數(shù)據(jù)
- COMMENT可以為表與字段增加描述
例如,建立一個(gè)usr表
create table if not exists testdb.usr(
uid string comment 'uid',
cuid string comment 'cuid',
type string comment 'type'
);
create table person(name STRING,age INT);
create table if not exists testdb.usr2(
id int,
name string,
address string
);
2.表查看
show tables in hive;
show tables 'u.*'; #查看hive中以u(píng)開(kāi)頭的表
describe hive.usr; #查看usr表相關(guān)信息
3.表修改
#重命名表
alter table usr rename to custom;
#修改列信息
alter table usr change column pwd password string after address;
#增加列
alter table usr add columns(hobby string);
#刪除替換列
alter table usr replace columns(uname string);
drop table if exists usr1;
4.導(dǎo)入數(shù)據(jù)
建立表,設(shè)定分隔符是t
create table if not exists testdb.testhive(unique_id string,uid string,cuid string,create_time int)
row format delimited fields terminated by 't';
load data local inpath "/home/team/r.txt" overwrite into table testhive;
Hive中追加導(dǎo)入數(shù)據(jù)的4種方式
從本地導(dǎo)入: load data local inpath '/home/st.txt' (overwrite) into table student;
從Hdfs導(dǎo)入: load data inpath '
/user/hive/warehouse/st.txt' (overwrite) into table student;
查詢(xún)導(dǎo)入: create table student_a as select * from student;(也可以具體查詢(xún)某項(xiàng)數(shù)據(jù))
查詢(xún)結(jié)果導(dǎo)入: insert (overwrite)into table student select * from student_a;
5.導(dǎo)出數(shù)據(jù)
insert overwrite local directory '/usr/local/hadoop/tmp/stu' select id,name from stu;
3.2.3查詢(xún)
和標(biāo)注sql語(yǔ)法基本一致,例如查詢(xún)一天的日活
select count(distinct cuid) from testhive;
Total MapReduce CPU Time Spent: 1 minutes 5 seconds 520 msec
OK
2942630
Time taken: 33.79 seconds, Fetched: 1 row(s)
3.3 hive命令的執(zhí)行方式
1).CLI 方式直接執(zhí)行 2).作為字符串通過(guò)shell調(diào)用hive –e執(zhí)行(-S開(kāi)啟靜默,去掉”OK”,”Time taken”)
Hql作為字符串在shell腳本中執(zhí)行,查詢(xún)結(jié)果可以直接導(dǎo)出到本地本件(默認(rèn)分隔符為t):
hive -e "use ${database};select * from tb" > tb.txt
如果字符串較長(zhǎng)的話(huà),可以按照如下方式書(shū)寫(xiě),sql=$(cat <<endtag 字符串endtag)方式可以將字符串復(fù)制給sql
file_path='/home/abc.txt'
sql=$(cat <<!EOF
USE pmp;
set mapred.queue.names=queue3;
drop table if exists people_targeted_delivery;
create table people_targeted_delivery
( special_tag_id int,
cnt bigint
);
INSERT OVERWRITE LOCAL DIRECTORY $file_path
ROW FORMAT DELIMITED FIELDS TERMINATED BY 't'
select special_tag_id,count(1) from t_pmp_special_user_tags group by special_tag_id;
!EOF)
############ execute begin ###########
echo $sql
$HIVE_HOME/bin/hive -e "$sql"
exitCode=$?
if [ $exitCode -ne 0 ];then
echo "[ERROR] hive execute failed!"
exit $exitCode
fi
3).作為獨(dú)立文件,通過(guò)shell調(diào)用 hive –f
mytest.hql書(shū)寫(xiě)我們編寫(xiě)好的hivesql文件
hive -f mytest.hql
4.配置遠(yuǎn)程機(jī)器訪問(wèn)
基于資源隔離的原則,不可能所有的hive操作會(huì)登錄到hive服務(wù)本地操作,更多的是在其他機(jī)器進(jìn)行.此時(shí)我們需要配置遠(yuǎn)程訪問(wèn).
4.1 遠(yuǎn)程配置
使用遠(yuǎn)程模式,需要在hadoop的core-site.xml文件中添加一下屬性
其中,XXX是用來(lái)代理其它用戶(hù)訪問(wèn)hdfs的用戶(hù)名,此處我的配置如下
<property>
<name>hadoop.proxyuser.xxx.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.xxx.groups</name>
<value>*</value>
</property>
重啟
#啟動(dòng)
./hadoop/sbin/start-all.sh
#./hadoop/sbin/stop-all.sh
#關(guān)閉安全模式
hdfs dfsadmin -safemode leave
設(shè)置hive-site.xml
<property>
<name>hive.metastore.uris</name>
<value>thrift://ip:9083</value>
</property>
設(shè)置防火墻
vi /etc/sysconfig/iptable
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9083 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 10000 -j ACCEPT
systemctl restart iptables.service
啟動(dòng)metastore或者h(yuǎn)iveserver2
nohup hive --service metastore &
# 下面這個(gè)支持beeline連接,官方
nohup hive --service hiveserver2 &
4.2 客戶(hù)端配置
- 確保安裝了java環(huán)境
yum localinstall jdk-8u151-linux-x64.rpm
- 新建一個(gè)hadoopclient 的目錄,用于存放hadoopclient和hiveclient,如下.所謂客戶(hù)端就是copy遠(yuǎn)程集群的目錄即可
# tree -L 1
.
├── hadoop
├── hive
- 配置正確環(huán)境變量
export JAVA_HOME=/usr/java/jdk1.8.0_151/
export HADOOP_HOME=/home/xxx/hadoopclient/hadoop
export HIVE_HOME=/home/xxx/hadoopclient/hive
export HIVE_CONF_DIR=${HIVE_HOME}/conf
export HADOOP_OPTS="-Djava.library.path=$HADOOP_HOME/lib:$HADOOP_HOME/lib/native"
export PATH=$JAVA_HOME/bin:$HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$HIVE_HOME/bin:$PATH
- 客戶(hù)端發(fā)起連接
- 方式1 -hive
繼續(xù)使用hivecli命令
hive
- 方式2-beeline(推薦)
beeline
!connect jdbc:hive2://ip:10000
#或者
beeline -u jdbc:hive2://ip:10000
beeline -u "jdbc:hive2://ip:10000/testdb;"
Beeline和其他工具有一些不同,執(zhí)行查詢(xún)都是正常的SQL輸入,但是如果是一些管理的命令,
比如進(jìn)行連接,中斷,退出,執(zhí)行Beeline命令需要帶上“!”,不需要終止符。常用命令介紹:
1、!connect url –連接不同的Hive2服務(wù)器
2、!exit –退出shell
3、!help –顯示全部命令列表
4、!verbose –顯示查詢(xún)追加的明細(xì)
The Beeline CLI 支持以下命令行參數(shù):
Option
Description
--autoCommit=[true/false] ---進(jìn)入一個(gè)自動(dòng)提交模式:beeline --autoCommit=true
--autosave=[true/false] ---進(jìn)入一個(gè)自動(dòng)保存模式:beeline --autosave=true
--color=[true/false] ---顯示用到的顏色:beeline --color=true
--delimiterForDSV= DELIMITER ---分隔值輸出格式的分隔符。默認(rèn)是“|”字符。
--fastConnect=[true/false] ---在連接時(shí),跳過(guò)組建表等對(duì)象:beeline --fastConnect=false
--force=[true/false] ---是否強(qiáng)制運(yùn)行腳本:beeline--force=true
--headerInterval=ROWS ---輸出的表間隔格式,默認(rèn)是100: beeline --headerInterval=50
--help ---幫助 beeline --help
--hiveconf property=value ---設(shè)置屬性值,以防被hive.conf.restricted.list重置:beeline --hiveconf prop1=value1
--hivevar name=value ---設(shè)置變量名:beeline --hivevar var1=value1
--incremental=[true/false] ---輸出增量
--isolation=LEVEL ---設(shè)置事務(wù)隔離級(jí)別:beeline --isolation=TRANSACTION_SERIALIZABLE
--maxColumnWidth=MAXCOLWIDTH ---設(shè)置字符串列的最大寬度:beeline --maxColumnWidth=25
--maxWidth=MAXWIDTH ---設(shè)置截?cái)鄶?shù)據(jù)的最大寬度:beeline --maxWidth=150
--nullemptystring=[true/false] ---打印空字符串:beeline --nullemptystring=false
--numberFormat=[pattern] ---數(shù)字使用DecimalFormat:beeline --numberFormat="#,###,##0.00"
--outputformat=[table/vertical/csv/tsv/dsv/csv2/tsv2] ---輸出格式:beeline --outputformat=tsv
--showHeader=[true/false] ---顯示查詢(xún)結(jié)果的列名:beeline --showHeader=false
--showNestedErrs=[true/false] ---顯示嵌套錯(cuò)誤:beeline --showNestedErrs=true
--showWarnings=[true/false] ---顯示警告:beeline --showWarnings=true
--silent=[true/false] ---減少顯示的信息量:beeline --silent=true
--truncateTable=[true/false] ---是否在客戶(hù)端截?cái)啾淼牧?
--verbose=[true/false] ---顯示詳細(xì)錯(cuò)誤信息和調(diào)試信息:beeline --verbose=true
-d <driver class> ---使用一個(gè)驅(qū)動(dòng)類(lèi):beeline -d driver_class
-e <query> ---使用一個(gè)查詢(xún)語(yǔ)句:beeline -e "query_string"
-f <file> ---加載一個(gè)文件:beeline -f filepath 多個(gè)文件用-e file1 -e file2
-n <username> ---加載一個(gè)用戶(hù)名:beeline -n valid_user
-p <password> ---加載一個(gè)密碼:beeline -p valid_password
-u <database URL> ---加載一個(gè)JDBC連接字符串:beeline -u db_URL
4.3 查詢(xún)并導(dǎo)出結(jié)果
這個(gè)理由小陷阱,--outputformat=tsv要放到-e前面才生效
beeline -u "jdbc:hive2://ip:10000/testdb;" --outputformat=tsv -e "select count(*) from testhive;" > r.txt
5.遇到的錯(cuò)誤
Hive JDBC:Permission denied: user=anonymous, access=EXECUTE, inode=”/tmp”
解決辦法:報(bào)錯(cuò)內(nèi)容提示hive沒(méi)有/tmp目錄的權(quán)限,賦予權(quán)限即可:(注意:該tmp目錄為hdfs的目錄,不是Linux系統(tǒng)的目錄)
hadoop fs -chmod 777 /tmp
#hadoop fs -chmod -R 777 /tmp






