一、Hive 介绍
Hive 是构建在 Hadoop 之上的数据仓库工具,由 Facebook 开发并开源,主要用于处理大规模结构化数据。它将用户编写的类 SQL 查询(HiveQL)转换为 Hadoop 生态系统中的计算任务(如 MapReduce、Tez、Spark),简化了大数据分析的门槛。
二、材料与文档
虚拟机前置条件:安装完成MySQL数据库,搭建完成Hadoop
Hive官网地址:Apache Hive
GitHub地址: GitHub - apache/hive: Apache Hive
文档查看地址:GettingStarted - Apache Hive - Apache Software Foundation
hive 4.0 依赖的 hadoop 版本最低是 3.3.6
这里我的Hadoop是3.3.1所以用hive3.1.2安装包
三、详细安装步骤
1.上传压缩包到虚拟机 /opt/modules
2.解压至安装位置 /opt/installs
tar -zxvf apache-hive-3.1.2-bin.tar.gz -C /opt/installs/
3.重命名为hive,方便辨识使用
mv apache-hive-3.1.2-bin/ hive
4.配置环境变量
在 /etc/profile.d/ 下找到配置文件 custom_env.sh 进行修改
或者:
vi /etc/profile.d/custom_env.sh 直接修改
修改为添加下面两行内容
export HIVE_HOME=/opt/installs/hive
export PATH=$HIVE_HOME/bin:$PATH
5.刷新环境变量:
source /etc/profile
6.配置hive-env.sh 位置 : /opt/installs/hive/conf
进入这个文件夹下:将 hive-env.sh.template 修改为 hive-env.sh
cp hive-env.sh.template hive-env.sh
修改为hive-env.sh 中末尾添加下列内容:
export HIVE_CONF_DIR=/opt/installs/hive/conf
export JAVA_HOME=/opt/installs/jdk
export HADOOP_HOME=/opt/installs/hadoop
export HIVE_AUX_JARS_PATH=/opt/installs/hive/lib
export HADOOP_HEAPSIZE=2048
7.启动Hadoop集群,准备修改集群配置
如果刚开机还没有启动hadoop集群,可以直接全部启动
start-all.sh
检查一下集群
jps-cluster.sh为自己写的脚本文件,跟基础jps功能一致,查看集群的进程
8.Hadoop上创建hive工作文件并授权
1、hdfs dfs -mkdir -p /user/hive/warehouse
2、hdfs dfs -mkdir -p /tmp/hive/
3、hdfs dfs -chmod 777 /user/hive/warehouse
4、hdfs dfs -chmod 777 /tmp/hive
9.检查MySQL服务
systemctl status mysqld
10.进入/opt/installs/hive/conf 创建 hive-site.xml 添加配置
cd /opt/installs/hive/conf
vi hive-site.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?><!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--><configuration>
<!--配置MySql的连接字符串-->
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true</value>
<description>JDBC connect string for a JDBC metastore</description>
</property>
<!--配置MySql的连接驱动-->
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.cj.jdbc.Driver</value>
<description>Driver class name for a JDBC metastore</description>
</property>
<!--配置登录MySql的用户-->
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
<description>username to use against metastore database</description>
</property>
<!--配置登录MySql的密码-->
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>123456</value>
<description>password to use against metastore database</description>
</property>
<!-- 以下两个不需要修改,只需要了解即可 -->
<!-- 该参数主要指定Hive的数据存储目录 -->
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
</property>
<!-- 该参数主要指定Hive的临时文件存储目录 -->
<property>
<name>hive.exec.scratchdir</name>
<value>/tmp/hive</value>
</property>
</configuration>
11.上传mysql的驱动包到 /op/installs/hive 的lib 文件夹下
12.初始化元数据(本质就是在mysql中创建数据库,并且添加元数据)
schematool --initSchema -dbType mysql
13.最终测试
输入hive,等待运行
成功进入
测试SQL命令
-- 进入后可以执行下面命令进行操作:
hive>show databases; -- 查看数据库
hive>show tables; -- 查看表
-- 创建表
hive> create table dog(id int,name string);
-- 查询表
hive> select * from dog;
-- 添加
hive> insert into dog values(1,'wangcai');
-- 查看表结构
hive> desc dog;
-- 退出
hive> quit;
如果你尝试了insert命令,80%不要怀疑你错了,耐心等待,它就是那么慢!
有用记得点赞!!!留下♥(ˆ◡ˆԅ)禁止白嫖