Hive官网:http://hive.apache.org
下载网址:http://archive.apache.org/dist/hive/
文档网址:https://cwiki.apache.org/confluence/display/Hive/LanguageManual
安装前提:3台虚拟机,安装了Hadoop
安装软件:Hive(2.3.7) + MySQL (5.7.26)
1、MySQL安装
1.1、删除MariaDB
centos7.6自带的 MariaDB(MariaDB是MySQL的一个分支),与要安装的MySQL有冲突,需要删除
# 查询是否安装了mariadb
rpm -aq | grep mariadb
# 删除mariadb。-e 删除指定的套件;--nodeps 不验证套件的相互关联性
rpm -e --nodeps mariadb-libs
1.2、安装依赖
yum install perl -y
yum install net-tools -y
1.3、安装MySQL
# 解压缩
tar xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
# 依次运行以下命令
rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
1.4、启动数据库
systemctl start mysqld
1.5、查找root密码
grep password /var/log/mysqld.log
1.6、修改 root 口令
# 进入MySQL,使用前面查询到的口令
mysql -u root -p
# 设置口令强度;
set global validate_password_policy=0;
# 将root口令设置为12345678;
set password for 'root'@'localhost' =password('12345678');
# 刷新
flush privileges;
validate_password_policy 密码策略(默认是1),可配置的值有以下:
0 or LOW 仅需需符合密码长度(由参数validate_password_length【默认为8】指定)
1 or MEDIUM 满足LOW策略,同时还需满足至少有1个数字,小写字母,大写字母和特殊字符
2 or STRONG 满足MEDIUM策略,同时密码不能存在字典文件(dictionary file)中
备注:个人开发环境,出于方便的目的设比较简单的密码;生产环境一定要设复杂密码
1.7、创建 hive 用户
-- 创建用户设置口令、授权、刷新
CREATE USER 'hive'@'%' IDENTIFIED BY '12345678';
GRANT ALL ON *.* TO 'hive'@'%';
FLUSH PRIVILEGES;
2、Hive 安装
安装步骤:
- 下载、上传、解压缩
- 修改环境变量
- 修改hive配置
- 拷贝JDBC的驱动程序
- 初始化元数据库
2.1、下载Hive软件,并解压缩
cd /opt/lagou/software
tar zxvf apache-hive-2.3.7-bin.tar.gz -C ../servers/
cd ../servers
mv apache-hive-2.3.7-bin hive-2.3.7
2.2、修改环境变量
# 在 /etc/profile 文件中增加环境变量
export HIVE_HOME=/opt/lagou/servers/hive-2.3.7
export PATH=$PATH:$HIVE_HOME/bin
# 执行并生效
source /etc/profile
2.3、修改 Hive 配置
- cd $HIVE_HOME/conf
- vi hive-site.xml 增加以下内容:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <!-- hive元数据的存储位置 --> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://linux123:3306/hivemetadata? createDatabaseIfNotExist=true&useSSL=false</value> <description>JDBC connect string for a JDBC metastore</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>hive</value> <description>username to use against metastore database</description> </property> <!-- 连接数据库的口令 --> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>12345678</value> <description>password to use against metastore database</description> </property> </configuration>
备注:
- 注意jdbc的连接串,如果没有 useSSL=false 会有大量警告
- 在xml文件中 & 表示 &
2.4、拷贝 MySQL JDBC 驱动程序
将 mysql-connector-java-5.1.46.jar 拷贝到 $HIVE_HOME/lib
2.5、初始化元数据库
[root@linux123 ~]$ schematool -dbType mysql -initSchema
2.6、启动Hive,执行命令
# 启动hive服务之前,请先启动hdfs、yarn的服务
[root@linux123 ~]$ hive # 进入hive命令
hive> show functions; # 在hive中执行命令
3、Hive 属性配置
可在 hive-site.xml 中增加以下常用配置,方便使用
3.1、数据存储位置
<property>
<!-- 数据默认的存储位置(HDFS) -->
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<description>location of default database for the warehouse</description>
</property>
3.2、显示当前库
<property>
<!-- 在命令行中,显示当前操作的数据库 -->
<name>hive.cli.print.current.db</name>
<value>true</value>
<description>Whether to include the current database in the Hive prompt.</description>
</property>
3.3、显示表头属性
<property>
<!-- 在命令行中,显示数据的表头 -->
<name>hive.cli.print.header</name>
<value>true</value>
</property>
3.4、本地模式
<property>
<!-- 操作小规模数据时,使用本地模式,提高效率 -->
<name>hive.exec.mode.local.auto</name>
<value>true</value>
<description>Let Hive determine whether to run in local mode automatically</description>
</property>
备注:当 Hive 的输入数据量非常小时,Hive 通过本地模式在单台机器上处理所有的任务。对于小数据集,执行时间会明显被缩短。当一个job满足如下条件才能真正使用本地模式:
- job的输入数据量必须小于参数:hive.exec.mode.local.auto.inputbytes.max(默认128MB)
- job的map数必须小于参数:hive.exec.mode.local.auto.tasks.max (默认4)
- job的reduce数必须为0或者1
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <!-- hive元数据的存储位置 --> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://hadoop3:3306/hivemetadata?createDatabaseIfNotExist=true&useSSL=false</value> <description>JDBC connect string for a JDBC metastore</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>hive</value> <description>username to use against metastore database</description> </property> <!-- 连接数据库的口令 --> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>12345678</value> <description>password to use against metastore database</description> </property> <property> <!-- 数据默认的存储位置(HDFS) --> <name>hive.metastore.warehouse.dir</name> <value>/user/hive/warehouse</value> <description>location of default database for the warehouse</description> </property> <property> <!-- 在命令行中,显示当前操作的数据库 --> <name>hive.cli.print.current.db</name> <value>true</value> <description>Whether to include the current database in the Hive prompt.</description> </property> <property> <!-- 在命令行中,显示数据的表头 --> <name>hive.cli.print.header</name> <value>true</value> </property> <property> <!-- 操作小规模数据时,使用本地模式,提高效率 --> <name>hive.exec.mode.local.auto</name> <value>true</value> <description>Let Hive determine whether to run in local mode automatically</description> </property> </configuration>
3.5、Hive的日志文件
Hive的log默认存放在 /tmp/root 目录下(root为当前用户名);这个位置可以修改
# 进入hive配置目录
cd $HIVE_HOME/conf
# 将hive-log4j2.properties.template重新复制一份去掉.template后缀
cp hive-log4j2.properties.template hive-log4j2.properties
# 修改下列属性以下内容:
property.hive.log.dir = /opt/lagou/servers/hive-2.3.7/logs
可以不修改,但是要知道位置。
注:Hadoop 2.x 中 NameNode RPC默认的端口号:8020
3.6、添加第三方用户(Hadoop)(了解)
groupadd hadoop
# -m:自动建立用户的登入目录
# -g:指定用户所属的起始群组
# -G<群组>:指定用户所属的附加群组
# -s:指定用户登入后所使用的shell
useradd -m hadoop -g hadoop -s /bin/bash
passwd hadoop
vi sudo
# 在100行后添加。允许用户执行sudo,免密
hadoop ALL=(ALL) NOPASSWD:ALL
3.7、小结:
- 添加了配置,使用Hive更方便;
- 删除了有冲突的软件包(hive)
- Hive的日志在哪里(/tmp/root)
- 第三方用户使用Hive。建议使用root用户
- NameNode 缺省的RPC(远程过程调用)端口号8020,经常使用的端口号9000
4、参数配置方式
4.1、查看参数配置信息
查看全部参数
hive> set;
查看某个参数
hive> set hive.exec.mode.local.auto;
hive.exec.mode.local.auto=false (查询结果)
4.2、参数配置的三种方式:
- 用户自定义配置文件(hive-site.xml)
- 启动hive时指定参数(-hiveconf)
- hive命令行指定参数(set)
配置信息的优先级:
set > -hiveconf > hive-site.xml > hive-default.xml
5、配置文件方式
- 默认配置文件:hive-default.xml
- 用户自定义配置文件:hive-site.xml
- 配置优先级:hive-site.xml > hive-default.xml
- 配置文件的设定对本机启动的所有Hive进程有效;
- 配置文件的设定对本机所有启动的Hive进程有效;
5.1、启动时指定参数值
启动Hive时,可以在命令行添加 -hiveconf param=value 来设定参数,这些设定仅对本次启动有效。
# 启动时指定参数
hive -hiveconf hive.exec.mode.local.auto=true
# 在命令行检查参数是否生效
hive> set hive.exec.mode.local.auto;
hive.exec.mode.local.auto=true
5.2、命令行修改参数
可在 Hive 命令行中使用SET关键字设定参数,同样仅对本次启动有效
hive> set hive.exec.mode.local.auto=false;
hive> set hive.exec.mode.local.auto;
hive.exec.mode.local.auto=false
set > -hiveconf > hive-site.xml > hive-default.xml
6、Hive命令
6.1、Hive
usage: hive
-d,--define <key=value> Variable substitution to apply to Hive
commands. e.g. -d A=B or --define A=B
--database <databasename> Specify the database to use
-e <quoted-query-string> SQL from command line
-f <filename> SQL from files
-H,--help Print help information
--hiveconf <property=value> Use value for given property
--hivevar <key=value> Variable substitution to apply to Hive
commands. e.g. --hivevar A=B
-i <filename> Initialization SQL file
-S,--silent Silent mode in interactive shell
-v,--verbose Verbose mode (echo executed SQL to the console)
-e:不进入hive交互窗口,执行sql语句
hive -e "select * from users"
-f:执行脚本中sql语句
# 创建文件hqlfile1.sql,内容:select * from users
# 执行文件中的SQL语句
hive -f hqlfile1.sql
# 执行文件中的SQL语句,将结果写入文件
hive -f hqlfile1.sql >> result1.log
6.2、退出Hive命令行
exit; 或者 quit;
6.3、在命令行执行 shell 命令 / dfs 命令
hive> ! ls;
hive> ! clear;
hive> dfs -ls / ;