赞
踩
下载网址:http://archive.apache.org/dist/hive/
文档网址:https://cwiki.apache.org/confluence/display/Hive/LanguageManual
安装前提:3台虚拟机,安装了Hadoop
安装软件:Hive(2.3.7) + MySQL (5.7.26)
备注:Hive的元数据默认存储在自带的 derby 数据库中,生产中多采用MySQL
derby:java语言开发占用资源少,单进程,单用户。仅仅适用于个人的测试。
软件 | linux121 | linux122 | linux123 |
Hadoop | √ | √ | √ |
MySQL | √ | ||
Hive | √ |
# hive安装包
apache-hive-2.3.7-bin.tar.gz# MySQL安装包
mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar# MySQL的JDBC驱动程序
mysql-connector-java-5.1.46.jar# 整体的安装步骤:
1、安装MySQL
2、安装配置Hive
3、Hive添加常用配置
资料下载地址:链接:https://pan.baidu.com/s/1cquJaKeVrVBc2sLBD58A3Q?pwd=98zx
Hive中使用MySQL存储元数据,MySQL的版本 5.7.26。安装步骤:
centos7.6自带的 MariaDB(MariaDB是MySQL的一个分支),与要安装的MySQL有冲突,需要删除。
# 查询是否安装了mariadb
rpm -aq | grep mariadb# 删除mariadb。-e 删除指定的套件;--nodeps 不验证套件的相互关联性
rpm -e --nodeps mariadb-libs
yum install perl -y
yum install net-tools -y
# 解压缩
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
systemctl start mysqld
grep password /var/log/mysqld.log
# 进入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),可配置的值有以下:
备注:个人开发环境,出于方便的目的设比较简单的密码;生产环境一定要设复杂密码!
-- 创建用户设置口令、授权、刷新
CREATE USER 'hive'@'%' IDENTIFIED BY '12345678';
GRANT ALL ON *.* TO 'hive'@'%';
FLUSH PRIVILEGES;
安装步骤:
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
# 在 /etc/profile 文件中增加环境变量
export HIVE_HOME=/opt/lagou/servers/hive-2.3.7
export PATH=$PATH:$HIVE_HOME/bin# 执行并生效
source /etc/profile
- <?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>

备注:
将 mysql-connector-java-5.1.46.jar 拷贝到 $HIVE_HOME/lib
[root@linux123 ~]$ schematool -dbType mysql -initSchema
# 启动hive服务之前,请先启动hdfs、yarn的服务
[root@linux123 ~]$ hive # 进入hive命令
hive> show functions; # 在hive中执行命令
可在 hive-site.xml 中增加以下常用配置,方便使用。
- <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>
备注:当 Hive 的输入数据量非常小时,Hive 通过本地模式在单台机器上处理所有的任务。对于小数据集,执行时间会明显被缩短。当一个job满足如下条件才能真正使用本地模式:
- <?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>

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
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
建议:现阶段使用root用户
-- 查看全部参数
hive> set;
-- 查看某个参数
hive> set hive.exec.mode.local.auto;
hive.exec.mode.local.auto=false (查询结果)
- 用户自定义配置文件(hive-site.xml)
- 启动hive时指定参数(-hiveconf)
- hive命令行指定参数(set)
配置信息的优先级:
set > -hiveconf > hive-site.xml > hive-default.xml
启动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
可在 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
- 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
exit; 或者 quit;
hive> ! ls;
hive> ! clear;
hive> dfs -ls / ;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。