Hive详解(02) - Hive 3.1.2安装
Hive详解(02) - Hive 3.1.2安装
安装准备
Hive下载地址
Hive官网地址
http://hadoop102:10002/
启动beeline客户端(需要多等待一会)
[hadoop@hadoop102 ~]$ beeline -u jdbc:hive2://hadoop102:10000 -n hadoop
看到如下信息说明通过beeline客户端访问hive成功
[hadoop@hadoop102 ~]$ beeline -u jdbc:hive2://hadoop102:10000 -n hadoop
Connecting to jdbc:hive2://hadoop102:10000
Connected to: Apache Hive (version 3.1.2)
Driver: Hive JDBC (version 3.1.2)
Transaction isolation: TRANSACTION_REPEATABLE_READ
Beeline version 3.1.2 by Apache Hive
0: jdbc:hive2://hadoop102:10000> show tables;
- 遇到的问题
问题一:Beeline链接hive需要密码的情况:
[hadoop@hadoop102 ~]$ beeline
Beeline version 3.1.2 by Apache Hive
beeline> !connect jdbc:hive2://hadoop102:10000
Connecting to jdbc:hive2://hadoop102:10000
Enter username for jdbc:hive2://hadoop102:10000: hadoop #hive服务端操作系统用户名
Enter password for jdbc:hive2://hadoop102:10000: ****** #hive服务端操作系统密码
Connected to: Apache Hive (version 3.1.2)
Driver: Hive JDBC (version 3.1.2)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://hadoop102:10000>
问题二:在使用beeline链接hive时如果连接失败报错:
Error: Could not open client transport with JDBC Uri: jdbc:hive2://192.168.194.48:10000: Failed to open new session: java.lang.RuntimeException: RemoteException(AuthorizationException): User: hadoop is not allowed to impersonate hadoop (state=08S01,code=0)
解决办法:通过httpfs协议访问rest接口,以hadoop用户包装自己的方式操作HDFS
首先需要开启rest接口,
在hdfs-site.xml文件中加入:
dfs.webhdfs.enabled
true
然后在core-site.xml文件中加入:
绿色的hadoop是beeline> ! connect jdbc:hive2://192.168.194.48:10000登录时的用户名
当用不同的用户通过rest接口访问hdfs时可以配置多个用户如下图中同时配置了hue和hadoop用户
编写启动metastore和hiveserver2脚本
前台启动的方式导致需要打开多个shell窗口,且终端断开链接后服务就停止运行,可以使用如下方式后台方式启动
nohup hive --service metastore 2>&1 &
nohup hive --service hiveserver2 2>&1 &
编写启动脚本可以更方便的管理
[hadoop@hadoop102 ~]$ cd /opt/module/hive/bin/
[hadoop@hadoop102 bin]$ vi hiveservices.sh
文件中加入如下内容
#!/bin/bash
HIVE_LOG_DIR=/opt/module/hive/logs
if [ ! -d $HIVE_LOG_DIR ]
then
mkdir -p $HIVE_LOG_DIR
fi
#检查进程是否运行正常,参数1为进程名,参数2为进程端口
function check_process()
{
pid=$(ps -ef 2>/dev/null | grep -v grep | grep -i $1 | awk '{print $2}')
ppid=$(netstat -nltp 2>/dev/null | grep $2 | awk '{print $7}' | cut -d '/' -f 1)
echo $pid
[[ "$pid" =~ "$ppid" ]] && [ "$ppid" ] && return 0 || return 1
}
function hive_start()
{
metapid=$(check_process HiveMetastore 9083)
cmd="nohup hive --service metastore >$HIVE_LOG_DIR/metastore.log 2>&1 &"
cmd=$cmd" sleep 4; hdfs dfsadmin -safemode wait >/dev/null 2>&1"
[ -z "$metapid" ] && eval $cmd || echo "Metastroe服务已启动"
server2pid=$(check_process HiveServer2 10000)
cmd="nohup hive --service hiveserver2 >$HIVE_LOG_DIR/hiveServer2.log 2>&1 &"
[ -z "$server2pid" ] && eval $cmd || echo "HiveServer2服务已启动"
}
function hive_stop()
{
metapid=$(check_process HiveMetastore 9083)
[ "$metapid" ] && kill $metapid || echo "Metastore服务未启动"
server2pid=$(check_process HiveServer2 10000)
[ "$server2pid" ] && kill $server2pid || echo "HiveServer2服务未启动"
}
case $1 in
"start")
hive_start
;;
"stop")
hive_stop
;;
"restart")
hive_stop
sleep 2
hive_start
;;
"status")
check_process HiveMetastore 9083 >/dev/null && echo "Metastore服务运行正常" || echo "Metastore服务运行异常"
check_process HiveServer2 10000 >/dev/null && echo "HiveServer2服务运行正常" || echo "HiveServer2服务运行异常"
;;
*)
echo Invalid Args!
echo 'Usage: '$(basename $0)' start|stop|restart|status'
;;
esac
添加执行权限
[hadoop@hadoop102 bin]$ chmod u+x hiveservices.sh
使用脚本
启动:hiveservices.sh start
停止:hiveservices.sh stop
重启:hiveservices.sh restart
查看状态: hiveservices.sh status
Hive常用交互命令
[hadoop@hadoop102 hive]$ bin/hive -help
usage: hive
-d,--define
commands. e.g. -d A=B or --define A=B
--database
-e
-f
-H,--help Print help information
--hiveconf
--hivevar
commands. e.g. --hivevar A=B
-i
-S,--silent Silent mode in interactive shell
-v,--verbose Verbose mode (echo executed SQL to the console)
"-e"不进入hive的交互窗口执行sql语句
bin/hive -e "select id from student;"
"-f"执行脚本中sql语句
在/opt/module/hive/下创建datas目录并在datas目录下创建hivef.sql文件
touch hivef.sql
文件中写入正确的sql语句
select *from student;
执行文件中的sql语句
bin/hive -f /opt/module/hive/datas/hivef.sql
执行文件中的sql语句并将结果写入文件中
bin/hive -f /opt/module/hive/datas/hivef.sql > /opt/module/datas/hive_result.txt
Hive其他命令操作
退出hive窗口:
hive(default)>exit;
hive(default)>quit;
在hive cli命令窗口中如何查看hdfs文件系统
hive(default)>dfs -ls /;
查看在hive中输入的所有历史命令
进入到当前用户的根目录/root或/home/atguigu
查看. hivehistory文件
cat .hivehistory
在Hive中配置Tez引擎
Hive运行引擎Tez
Tez是一个Hive的运行引擎,性能优于MR。
用Hive直接编写MR程序,假设有四个有依赖关系的MR作业,上图中,绿色是Reduce Task,云状表示写屏蔽,需要将中间结果持久化写到HDFS。Tez可以将多个有依赖的作业转换为一个作业,这样只需写一次HDFS,且中间节点较少,从而大大提升作业的计算性能。
安装准备
hadoop配置支持LZO压缩,参考文档《Hadoop详解(07-1) - Hdfs支持LZO压缩配置》
tez官网:https://tez.apache.org/
tez安装包下载地址:https://downloads.apache.org/tez/0.10.1/apache-tez-0.10.1-bin.tar.gz
上传解压
[hadoop@hadoop102 software]$ tar -zxvf apache-tez-0.10.1-bin.tar.gz -C /opt/module/
修改名称
[hadoop@hadoop102 software]$ cd /opt/module/
[hadoop@hadoop102 module]$ mv apache-tez-0.10.1-bin/ tez-0.10.1
在Hive中配置Tez
- 在hive-env.sh文件中添加tez环境变量配置和依赖包环境变量配置
[hadoop@hadoop102 module]$ cd /opt/module/hive/conf/
[hadoop@hadoop102 conf]$ vi hive-env.sh
在文件末尾添加如下配置
#tez的解压目录
export TEZ_HOME=/opt/module/tez-0.10.1
export TEZ_JARS=""
for jar in `ls $TEZ_HOME |grep jar`; do
export TEZ_JARS=$TEZ_JARS:$TEZ_HOME/$jar
done
for jar in `ls $TEZ_HOME/lib`; do
export TEZ_JARS=$TEZ_JARS:$TEZ_HOME/lib/$jar
done
#导入lzo压缩jar包的环境变量
export HIVE_AUX_JARS_PATH=/opt/module/hadoop-3.1.3/share/hadoop/common/hadoop-lzo-0.4.21-SNAPSHOT.jar$TEZ_JARS
- 在hive-site.xml文件中添加如下配置,更改hive计算引擎
hive.execution.engine tez - 配置tez-site.xml
在/opt/module/hive/conf下面创建tez-site.xml文件
[hadoop@hadoop102 conf]$ vi tez-site.xml
在tez-site.xml添加如下内容
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
- 上传Tez目录到hdfs集群
1)将tez安装目录/opt/module/tez-0.9.1上传到HDFS的/tez路径
[hadoop@hadoop102 conf]$ hadoop fs -mkdir /tez
[hadoop@hadoop102 conf]$ hadoop fs -put /opt/module/tez-0.10.1/ /tez
[hadoop@hadoop102 conf]$ hadoop fs -ls /tez
Found 1 items
drwxr-xr-x - hadoop supergroup 0 2022-01-14 01:40 /tez/tez-0.10.1
- 测试
启动Hive
启动hive过程不报错,如果报错说明tez引擎配置有问题
[hadoop@hadoop102 hive]$ bin/hive
普通表数据测试
创建LZO表
hive (default)> create table student(
id int,
name string);
向表中添加数据
hive (default)> insert into student values(1,"zhangjk");
查询数据,如果没有报错就表示hive配置tez引擎成功
hive (default)> select * from student;
OK
student.id student.name
1 zhangjk
Time taken: 0.187 seconds, Fetched: 1 row(s)
使用LZO压缩的表测试
创建输入数据是lzo输出是text,支持json解析的分区表
hive (default)> drop table if exists log;
CREATE EXTERNAL TABLE log (`line` string)
PARTITIONED BY (`dt` string)
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION '/user/hive/warehouse/log';
在本地创建文本数据,添加测试数据并上传到hdfs中
[hadoop@hadoop102 module]$ vi 1.log
文件中添加如下测试数据
hello hadoop
hello hive
hello tez
上传到hdfs
hadoop fs -put /opt/module/1.log /user
加载数据
hive (gmall)> load data inpath '/user/1.log' into table log partition(dt='2022-01-01');
解决内存不足问题
如果在虚拟机上运行Tez时经常会出现内存不足道情况而被NodeManager杀死进程,如:
Caused by: org.apache.tez.dag.api.SessionNotRunning: TezSession has already shutdown. Application application_1546781144082_0005 failed 2 times due to AM Container for appattempt_1546781144082_0005_000002 exited with exitCode: -103 For more detailed output, check application tracking page:http://hadoop103:8088/cluster/app/application_1546781144082_0005Then, click on links to logs of each attempt. Diagnostics: Container [pid=11116,containerID=container_1546781144082_0005_02_000001] is running beyond virtual memory limits. Current usage: 216.3 MB of 1 GB physical memory used; 2.6 GB of 2.1 GB virtual memory used. Killing container.
这种问题是从机上运行的Container试图使用过多的内存,而被NodeManager kill掉了。
[摘录] The NodeManager is killing your container. It sounds like you are trying to use hadoop streaming which is running as a child process of the map-reduce task. The NodeManager monitors the entire process tree of the task and if it eats up more memory than the maximum set in mapreduce.map.memory.mb or mapreduce.reduce.memory.mb respectively, we would expect the Nodemanager to kill the task, otherwise your task is stealing memory belonging to other containers, which you don't want.
[摘录翻译]节点管理器正在杀死您的容器。听起来您正在尝试使用hadoop流,它作为map reduce任务的子进程运行。NodeManager监视任务的整个进程树,以及任务占用的内存是否超过mapreduce中设置的最大值。地图记忆力mb或mapreduce。减少记忆力我们希望节点管理器杀死该任务,否则您的任务将窃取属于其他容器的内存,这是您不想要的。
- 解决方法:
方案一:关掉虚拟内存检查。
修改yarn-site.xml
方案二:mapred-site.xml中设置Map和Reduce任务的内存配置
value中实际配置的内存需要根据自己机器内存大小及应用情况进行修改
修改完后重新启动hadoop集群和hive服务
常见错误及解决方案
- 如果更换Tez引擎后,执行任务卡住,可以尝试调节容量调度器的资源调度策略
将$HADOOP_HOME/etc/hadoop/capacity-scheduler.xml文件中的
Maximum percent of resources in the cluster which can be used to run
application masters i.e. controls number of concurrent running
applications.
改成
Maximum percent of resources in the cluster which can be used to run
application masters i.e. controls number of concurrent running
applications.
- 连接不上mysql数据库
(1)导错驱动包,应该把mysql-connector-java-5.1.27-bin.jar导入/opt/module/hive/lib的不是这个包。错把mysql-connector-java-5.1.27.tar.gz导入hive/lib包下。
(2)修改user表中的主机名称没有都修改为%,而是修改为localhost
- hive默认的输入格式处理是CombineHiveInputFormat,会对小文件进行合并。
hive (default)> set hive.input.format;
hive.input.format=org.apache.hadoop.hive.ql.io.CombineHiveInputFormat
可以采用HiveInputFormat就会根据分区数输出相应的文件。
hive (default)> set hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
- 启动mysql服务时,报MySQL server PID file could not be found! 异常。
在/var/lock/subsys/mysql路径下创建hadoop102.pid,并在文件中添加内容:4396
- 报service mysql status MySQL is not running, but lock file (/var/lock/subsys/mysql[失败])异常。
解决方案:在/var/lib/mysql 目录下创建: -rw-rw----. 1 mysql mysql 5 12月 22 16:41 hadoop102.pid 文件,并修改权限为 777。
- JVM堆内存溢出
描述:java.lang.OutOfMemoryError: Java heap space
解决:在yarn-site.xml中加入如下代码
- 虚拟内存限制
在yarn-site.xml中添加如下配置: