目录
一、安装准备
2、下载地址
3、官方文档
1、解压文件
1、复制mysql jdbc驱动包到hive lib目录
一、安装准备
1、Hive官网
2、下载地址
3、官方文档
GettingStarted - Apache Hive - Apache Software Foundation
4、SSH免密配置
大数据入门之 ssh 免密码登录_qq262593421的博客
5、Zookeeper安装
大数据高可用技术之zookeeper3.4.5安装配置_qq262593421的博客
6、Hadoop安装
二、解压Hive
1、解压文件
cd /usr/local/hadoop
tar zxpf apache-hive-2.3.2-bin.tar.gz
2、创建软链接
ln -s /usr/local/hadoop/apache-hive-2.3.2-bin /usr/local/hadoop/hive
三、环境变量配置
1、hive环境变量配置
vim /etc/profile
export HIVE_HOME=/usr/local/hadoop/hive
export PATH=$PATH:$HIVE_HOME/bin
2、配置环境立即生效
source /etc/profile
四、Hive配置
cd $HIVE_HOME/conf
touch hive-env.sh hive-site.xml
chmod +x hive-env.sh
1、hive-env.sh配置
export HADOOP_HEAPSIZE=4096
export JAVA_HOME=/usr/java/jdk1.8
export HADOOP_HOME=/usr/local/hadoop/hadoop
export HIVE_HOME=/usr/local/hadoop/hive
export HIVE_CONF_DIR=/usr/local/hadoop/hive/conf
export HBASE_HOME=/usr/local/hadoop/hbase
export SPARK_HOME=/usr/local/hadoop/spark
export ZOO_HOME=/usr/local/hadoop/zookeeper
2、hive-site.xml配置
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- Hive数据库在HDFS上的数据存储路径 -->
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<description>location of default database for the warehouse</description>
</property>
<!-- Hive临时文件,用于存储每个查询的临时/中间数据集,通常在完成查询后由配置单元客户端清除 -->
<property>
<name>hive.exec.local.scratchdir</name>
<value>/usr/local/hadoop/hive/tmp</value>
<description>Local scratch space for Hive jobs</description>
</property>
<!-- Hive添加资源时的临时目录 -->
<property>
<name>hive.downloaded.resources.dir</name>
<value>/usr/local/hadoop/hive/tmp/resources</value>
<description>Temporary local directory for added resources in the remote file system.</description>
</property>
<!-- Hive运行时结构化日志文件的位置 -->
<property>
<name>hive.querylog.location</name>
<value>/user/hadoop/hive/logs</value>
<description>Location of Hive run time structured log file</description>
</property>
<!-- 用于验证metastore和hivejar包是否一致问题,默认为true。false:hive升级版本不一致只会警告 -->
<property>
<name>hive.metastore.schema.verification</name>
<value>false</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>
<!-- 使用MySQL作为hive的元数据Metastore数据库 -->
<property>
<name>hive.metastore.db.type</name>
<value>mysql</value>
<description>
Expects one of [derby, oracle, mysql, mssql, postgres].
Type of database used by the metastore. Information schema & JDBCStorageHandler depend on it.
</description>
</property>
<!-- 使用MySQL作为hive的元数据Metastore数据库 -->
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://hadoop001:3306/hive?createDatabaseIfNotExist=true&useSSL=false</value>
</property>
<!-- MySQL JDBC驱动程序类 -->
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
<description>Driver class name for a JDBC metastore</description>
</property>
<!-- 连接到MySQL服务器的用户名 -->
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>hive</value>
<description>Username to use against metastore database</description>
</property>
<!-- 连接MySQL服务器的密码 -->
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>hive</value>
<description>Comma separated list of configuration options which should not be read by normal user like passwords</description>
</property>
<!-- 初始化Hive数据库自动创建schema -->
<property>
<name>datanucleus.schema.autoCreateAll</name>
<value>true</value>
<description>Auto creates necessary schema on a startup if one doesn't exist. Set this to false, after creating it once.To enable auto create also set hive.metastore.schema.verification=false. Auto creation is not recommended for production use cases, run schematool command instead.</description>
</property>
<!-- Hive server2 thrift 主机 -->
<property>
<name>hive.server2.thrift.bind.host</name>
<value>hadoop001</value>
<description>Bind host on which to run the HiveServer2 Thrift service.</description>
</property>
<!-- Hive server2 thrift 端口 -->
<property>
<name>hive.server2.thrift.port</name>
<value>10000</value>
<description>Port number of HiveServer2 Thrift interface when hive.server2.transport.mode is 'binary'.</description>
</property>
<!-- Thrift Metastore服务器的主机和端口 默认 <value/> -->
<property>
<name>hive.metastore.uris</name>
<!-- <value>thrift://hadoop001:9083</value> -->
<description>Thrift URI for the remote metastore. Used by metastore client to connect to remote metastore.</description>
</property>
<!-- 如果启用了日志功能,则存储操作日志的顶级目录 -->
<property>
<name>hive.server2.logging.operation.log.location</name>
<value>/usr/local/hadoop/hive/tmp/operation_logs</value>
<description>Top level directory where operation logs are stored if logging functionality is enabled</description>
</property>
<!-- HiveServer2 webUI 监听主机 -->
<property>
<name>hive.server2.webui.host</name>
<value>hadoop001</value>
<!-- <value>0.0.0.0</value> -->
<description>The host address the HiveServer2 WebUI will listen on</description>
</property>
<!-- HiveServer2 webUI 监听端口 -->
<property>
<name>hive.server2.webui.port</name>
<value>10002</value>
<description>The port the HiveServer2 WebUI will listen on. This can beset to 0 or a negative integer to disable the web UI</description>
</property>
<!-- HiveServer2 webUI 最大线程数 -->
<property>
<name>hive.server2.webui.max.threads</name>
<value>50</value>
<description>The max HiveServer2 WebUI threads</description>
</property>
<!-- HiveServer2 webUI SSL验证 -->
<property>
<name>hive.server2.webui.use.ssl</name>
<value>false</value>
<description>Set this to true for using SSL encryption for HiveServer2 WebUI.</description>
</property>
<!-- HiveServer2 账户 -->
<property>
<name>hive.server2.thrift.client.user</name>
<value>root</value>
<description>Username to use against thrift client</description>
</property>
<!-- HiveServer2 密码 -->
<property>
<name>hive.server2.thrift.client.password</name>
<value>root</value>
<description>Password to use against thrift client</description>
</property>
</configuration>
五、初始化Hive
1、复制mysql jdbc驱动包到hive lib目录
cd $HIVE_HOME/lib
wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar
wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.16/mysql-connector-java-8.0.16.jar
2、MySQL创建用户并赋予权限
-- 创建hive用户,密码为hive
CREATE USER 'hive'@'%' IDENTIFIED BY 'hive';
-- 赋予hive用户全部权限
GRANT ALL PRIVILEGES ON *.* TO 'hive'@'%' IDENTIFIED BY 'hive' WITH GRANT OPTION;
-- 刷新权限
FLUSH PRIVILEGES;
3、启动zk和hadoop集群
zkServer.sh start
hdfs --daemon start zkfc
start-all.sh
4、创建hive目录并赋权
hadoop fs -mkdir /tmp
hadoop fs -mkdir /user/hive/warehouse
hadoop fs -chmod g+w /tmp
hadoop fs -chmod g+w /user/hive/warehouse
5、初始化hive数据库
schematool -dbType mysql -initSchema
6、查看hive初始化的数据库
六、启动Hive
1、启动hive客户端
hive
SHOW DATABASES;
CREATE DATABASE db01;
USE db01;
set hive.cli.print.current.db=true;
CREATE TABLE pokes (foo INT, bar STRING);
CREATE TABLE invites (foo INT, bar STRING) PARTITIONED BY (ds STRING);
SHOW TABLES;
SHOW TABLES '.*s';
DESCRIBE invites;
2、HDFS查看Hive目录
http://hadoop001:9870/explorer.html#/user/hive/warehouse/db01.db
3、启动 HiveServer2 服务
nohup hiveserver2 > /dev/null 2>&1 &
HiveServer2服务支持多线程多用户同时连接,还同时还支持JDBC连接
JDBC驱动:org.apache.hive.jdbc.HiveDriver
JDBCURL:jdbc:hive2://hadoop001:10000/dbname
4、查看 Hive 日志
tail -n 300 /tmp/root/hive.log
5、查看 HiveServer2 webui
HiveServer2 webui 也可以查看 hive日志和配置文件
6、beeline连接Hive
beeline
!connect jdbc:hive2://hadoop001:10000
SHOW DATABASES;
SHOW TABLES;
SELECT * FROM sqoop_user;
!quit
七、Hive连接代码
1、hive jdbc连接
官方参考:HiveClient - Apache Hive - Apache Software Foundation
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class TestHiveQuery {
private static final String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive2://node3:10000/default", "hive", "hive");
Statement stmt = con.createStatement();
String tableName = "default.emp";
// select * query
String sql = "select * from " + tableName;
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
}
}
}