赞
踩
一、什么是Hive
1、Hive简介
Hive是由Facebook开源,基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提供类SQL查询功能。Hive主要是做海量数据的分析和计算的。
2、为什么会有Hive?它解决了什么问题?
下面通过一个案例,来快速了解一下Hive。
例如:需求,统计单词出现个数。
(1)在Hadoop中我们用MapReduce程序实现的,需要些Mapper、Reducer和Driver三个类,并实现对应逻辑,相对繁琐。
(2)如果通过Hive SQL实现,一行就搞定了,简单方便,容易理解。
3、Hive本质
Hive是一个Hadoop客户端,用于将HQL(Hive SQL)转化为MapReduce程序。
(1)Hive中每张表的数据存储在HDFS。
(2)Hive分析数据底层的实现是MapReduce(也可配置为Spack或者Tez)。
(3)执行程序运行在Yarn上。
二、Hive架构原理
1、架构图
2、服务
Metastore:存储元数据,表字段、表数据在hdfs上的路径等
HiveServer2:提供jdbc或odbc访问接口,提供用户认证功能
Hive Client:Hive客户端
3、执行流程
(1)用户创建table
(2)Metastore中记录对应表的路径
(3)在hdfs中映射表关系
(4)用户根据业务需求编写相应的HQL语句
(5)Driver翻译sql为MapReduce
(6)提交yarn执行
(7)将结果写入hdfs路径或临时表
(8)如果是查询语句,返回计算结果
4、用户接口:Client
CLI(command-line interface)、JDBC/ODBC。
说明:JDBC和ODBC的区别。
(1)JDBC的移植性比ODBC好。
(2)两者使用的语言不同,JDBC在Java编程时使用,ODBC一般在C/C++编程时使用。
5、元数据:Metastore
元数据包括:数据库(默认是default)、表名、表的拥有者、列/分区字段、表的类型(是否外部表)、表的数据所在目录等。
默认存储在自带的derby数据库中,由于derby数据库只支持单客户端访问,生产环境中为了多人开发,推荐使用MySQL存储Metastore,但是安装配置太麻烦。
6、驱动器:Driver
(1)解析器(SQLParser):将SQL字符串转换成抽象语法树(AST)。
(2)语义分析(Semantic Analyzer):将AST进一步划分为QueryBlock。
(3)逻辑计划生成器(Logical Plan Gen):将语法树生成逻辑计划。
(4)逻辑优化器(Logical Optimizer):对逻辑计划进行优化。
(5)物理计划生成器(Physical Plan Gen):根据优化后的逻辑计划生成物理计划。
(6)物理优化器(Physical Optimizer):对物理计划进行优化。
(7)执行器(Execution):执行该计划,得到查询结果并返回给客户端。
三、Hive安装
1、下载安装包
- cd /tmp
- wget https://dlcdn.apache.org/hive/hive-3.1.3/apache-hive-3.1.3-bin.tar.gz
- tar -zxvf apache-hive-3.1.3-bin.tar.gz -C /appserver
- cd /appserver
- mv apache-hive-3.1.3-bin/ hive
2、配置环境变量
- vi /etc/profile
- 添加
- export HIVE_HOME=/appserver/hive
- export PATH=$PATH:$HIVE_HOME/bin
- 使配置生效
- source /etc/profile
3、初始化元数据库(默认是derby数据库,不使用)
bin/schematool -dbType derby -initSchema
因为内嵌数据库,只能使用本机命令行客户端,不能使用外部JDBC/ODBC客户端。
四、Hive连接mysql
1、安装mysql
略过
搜索rpm包的网站:https://pkgs.org/
2、下载mysql驱动jar包
将mysql的JDBC驱动拷贝到hive的lib目录下。
cp /tmp/mysql-connector-j-8.0.32.jar $HIVE_HOME/lib
3、mysql中创建metastore数据库和hive用户
- create database metastore default charset utf8mb4 collate utf8mb4_general_ci;
- create user 'hive'@'%' identified by 'hive';
- grant all privileges on metastore.* to 'hive'@'%';
- flush privileges;
4、在$HIVE_HOME/conf目录下,建立hive-site.xml文件
- cd /appserver/hive/conf
- vi hive-site.xml
- 添加
- <configuration>
- <!-- jdbc连接的URL -->
- <property>
- <name>javax.jdo.option.ConnectionURL</name>
- <value>jdbc:mysql://192.168.52.11:3306/metastore?serverTimezone=UTC</value>
- <description>
- JDBC connect string for a JDBC metastore.
- To use SSL to encrypt/authenticate the connection, provide database-specific SSL flag in the connection URL.
- For example, jdbc:postgresql://myhost/db?ssl=true for postgres database.
- </description>
- </property>
- <!-- jdbc连接的Driver -->
- <property>
- <name>javax.jdo.option.ConnectionDriverName</name>
- <value>com.mysql.jdbc.Driver</value>
- <description>Driver class name for a JDBC metastore</description>
- </property>
- <!-- jdbc连接的username -->
- <property>
- <name>javax.jdo.option.ConnectionUserName</name>
- <value>hive</value>
- <description>Username to use against metastore database</description>
- </property>
- <!-- jdbc连接的password -->
- <property>
- <name>javax.jdo.option.ConnectionPassword</name>
- <value>hive</value>
- <description>password to use against metastore database</description>
- </property>
- <!-- hive默认在HDFS的工作目录 -->
- <property>
- <name>hive.metastore.warehouse.dir</name>
- <value>/user/hive/warehouse</value>
- <description>location of default database for the warehouse</description>
- </property>
- </configuration>
5、初始化hive元数据库
bin/schematool -dbType mysql -initSchema
6、验证元数据是否配置成功
- bin/hive
- SLF4J: Class path contains multiple SLF4J bindings.
- SLF4J: Found binding in [jar:file:/appserver/hive/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
- SLF4J: Found binding in [jar:file:/appserver/hadoop/hadoop-3.3.4/share/hadoop/common/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
- SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
- SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
- Hive Session ID = 17c34420-ff60-4aa8-975b-abd7e121e22a
-
- Logging initialized using configuration in jar:file:/appserver/hive/lib/hive-common-3.1.3.jar!/hive-log4j2.properties Async: true
- Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
- Hive Session ID = bd993361-f2c9-4cfc-8dff-3e1bf157edf9
- Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
- hive> show databases;
- OK
- default
- Time taken: 0.494 seconds, Fetched: 1 row(s)
7、使用hive
- hive> create table stu(id int, name string);
- OK
- Time taken: 0.807 seconds
- hive> insert into stu values(1,"ss");
- Query ID = root_20230209122931_16eca7d9-d918-4d48-8611-02e51d6b3c9b
- Total jobs = 3
- Launching Job 1 out of 3
- Number of reduce tasks determined at compile time: 1
- In order to change the average load for a reducer (in bytes):
- set hive.exec.reducers.bytes.per.reducer=<number>
- In order to limit the maximum number of reducers:
- set hive.exec.reducers.max=<number>
- In order to set a constant number of reducers:
- set mapreduce.job.reduces=<number>
- Starting Job = job_1675910632302_0001, Tracking URL = http://hadoop001:8088/proxy/application_1675910632302_0001/
- Kill Command = /appserver/hadoop/hadoop-3.3.4/bin/mapred job -kill job_1675910632302_0001
- Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1
- 2023-02-09 12:29:54,504 Stage-1 map = 0%, reduce = 0%
- 2023-02-09 12:30:22,464 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 1.84 sec
- 2023-02-09 12:30:31,668 Stage-1 map = 100%, reduce = 100%, Cumulative CPU 3.25 sec
- MapReduce Total cumulative CPU time: 3 seconds 250 msec
- Ended Job = job_1675910632302_0001
- Stage-4 is selected by condition resolver.
- Stage-3 is filtered out by condition resolver.
- Stage-5 is filtered out by condition resolver.
- Moving data to directory hdfs://hadoop001:8020/user/hive/warehouse/stu/.hive-staging_hive_2023-02-09_12-29-31_229_8346627652307208831-1/-ext-10000
- Loading data to table default.stu
- MapReduce Jobs Launched:
- Stage-Stage-1: Map: 1 Reduce: 1 Cumulative CPU: 3.25 sec HDFS Read: 15184 HDFS Write: 235 SUCCESS
- Total MapReduce CPU Time Spent: 3 seconds 250 msec
- OK
- Time taken: 65.381 seconds
- hive> select * from stu;
- OK
- 1 ss
- Time taken: 0.516 seconds, Fetched: 1 row(s)
五、hive元数据库介绍
1、DBS表
存储数据库信息。
2、TBLS表
存储我们在hive中创建表的信息。
3、SDS表
存储建的表的位置信息。
4、COLUMNS_V2表
存储和字段相关的信息。
六、hiveserver2服务部署
1、hiveserver2服务
Hive的hiveserver2服务的作用是提供jdbc/odbc接口,为用户提供远程访问Hive数据的功能,例如用户期望在个人电脑中访问远程服务中的Hive数据,就需要hiveserver2。
2、访问用户说明
在远程访问Hive数据时,客户端并未直接访问Hadoop集群,而是由hiveserver2代理访问。由于Hadoop集群中的数据具备访问权限控制,所以此时需考虑一个问题:那就是访问Hadoop集群的用户身份是谁?是hiveserver2的启动用户?还是客户端的登录用户?
答案是都有可能,具体是谁,由hiveserver2的hive.server2.enable.doAs参数决定,该参数的含义是是否启用hiveserver2用户模拟的功能。若启用,则hiveserver2会模拟成客户端的登录用户去访问Hadoop集群的数据,不启用,则hiveserver2会直接使用启动用户访问Hadoop集群数据。
模拟用户的功能默认是开启的。
生产环境,推荐开启用户模拟功能,因为开启后才能保证各用户之间的权限隔离。
3、Hadoop配置代理用户
hiveserver2的模拟用户功能,依赖于Hadoop提供的proxy user(代理用户功能),只有Hadoop中的代理用户才能模拟其他用户的身份访问Hadoop集群。因此,需要将hiveserver2的启动用户设置为Hadoop的代理用户。
4、修改hadoop配置文件core-site.xml(例子,按实际情况修改)
- cd $HADOOP_HOME/etc/hadoop
- vi core-site.xml
- 添加
- <!-- 配置所有节点的user001用户都可作为代理用户 -->
- <property>
- <name>hadoop.proxyuser.user001.hosts</name>
- <value>*</value>
- </property>
- <!-- 配置user001用户能够代理的用户组为任意组 -->
- <property>
- <name>hadoop.proxyuser.user001.groups</name>
- <value>*</value>
- </property>
- <!-- 配置user001用户能够代理的用户为任意用户 -->
- <property>
- <name>hadoop.proxyuser.user001.users</name>
- <value>*</value>
- </property>
说明:
(1)配置代理用户
<name>hadoop.proxyuser.user001.hosts</name>
<value>*</value>
将哪个节点的哪个用户作为hadoop的代理用户,下面的value值是节点的主机名。允许所有节点上面的user001用户作为代理用户。谁启动hiveserver2用户名就写谁。
(2)配置代理用户能代理哪个组
<name>hadoop.proxyuser.user001.groups</name>
<value>*</value>
user001这个代理用户可以代理所有的组。
(3)配置代理用户能代理哪些用户
<name>hadoop.proxyuser.user001.users</name>
<value>*</value>
user001用户,能代理所有用户。
5、重启hadoop
6、修改hive配置文件hive-site.xml
- cd $HIVE_HOME/conf
- vi hive-site.xml
- 添加
- <!-- 指定hiveserver2连接的host -->
- <property>
- <name>hive.server2.thrift.bind.host</name>
- <value>hadoop001</value>
- </property>
- <!-- 指定hiveserver2连接的端口号 -->
- <property>
- <name>hive.server2.thrift.port</name>
- <value>10000</value>
- </property>
配置hive日志:
- cd $HIVE_HOME/conf
- cp hive-log4j2.properties.template hive-log4j2.properties
- vim hive-log4j2.properties
- 修改
- property.hive.log.dir = /appserver/hive/logs
7、测试
(1)启动hiveserver2
- bin/hive --service hiveserver2
- 或者
- bin/hiveserver2
- 后台启动
- nohup hiveserver2 >> /appserver/hive/logs/hiveserver2.log 2>&1 &
- 停止
- ps -ef|grep hiveserver2|grep -v 'grep'|awk '{print $2}'|xargs -n1 kill -9
-
- 2023-02-15 16:17:07: Starting HiveServer2
- SLF4J: Class path contains multiple SLF4J bindings.
- SLF4J: Found binding in [jar:file:/appserver/hive/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
- SLF4J: Found binding in [jar:file:/appserver/hadoop/hadoop-3.3.4/share/hadoop/common/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
- SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
- SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
- Hive Session ID = f60d9227-c830-49a9-9b51-9e279dc9597f
- Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
RunJar就是hiveserver2
- jps -ml
-
- 7360 org.apache.zookeeper.server.quorum.QuorumPeerMain /appserver/zookeeper/bin/../conf/zoo.cfg
- 7073 org.apache.hadoop.yarn.server.nodemanager.NodeManager
- 8533 org.apache.hadoop.util.RunJar /appserver/hive/lib/hive-service-3.1.3.jar org.apache.hive.service.server.HiveServer2
- 6393 org.apache.hadoop.hdfs.server.datanode.DataNode
- 8825 sun.tools.jps.Jps -ml
- 6266 org.apache.hadoop.hdfs.server.namenode.NameNode
- 6842 org.apache.hadoop.yarn.server.resourcemanager.ResourceManager
- 7322 org.apache.hadoop.mapreduce.v2.hs.JobHistoryServer
- 6603 org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode
- 7822 org.apache.hadoop.hbase.regionserver.HRegionServer start
七、hiveserver2应用
1、使用命令行客户端beeline进行远程访问
(1)启动
- cd /appserver/hive/bin
- ./beeline
- SLF4J: Class path contains multiple SLF4J bindings.
- SLF4J: Found binding in [jar:file:/appserver/hive/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
- SLF4J: Found binding in [jar:file:/appserver/hadoop/hadoop-3.3.4/share/hadoop/common/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
- SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
- SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
- Beeline version 3.1.3 by Apache Hive
- beeline>
(2)连接
- beeline> !connect jdbc:hive2://hadoop001:10000
- Connecting to jdbc:hive2://hadoop001:10000
- Enter username for jdbc:hive2://hadoop001:10000: root
- Enter password for jdbc:hive2://hadoop001:10000:
- Connected to: Apache Hive (version 3.1.3)
- Driver: Hive JDBC (version 3.1.3)
- Transaction isolation: TRANSACTION_REPEATABLE_READ
- 0: jdbc:hive2://hadoop001:10000>
输入的用户名就是hiveserver2以什么身份访问hadoop,密码由于没有启用认证功能直接回车。
(3)执行sql语句
- 0: jdbc:hive2://hadoop001:10000> show tables;
- INFO : Compiling command(queryId=root_20230222140625_743ec057-5e62-4073-acc7-7b849fe994e2): show tables
- INFO : Concurrency mode is disabled, not creating a lock manager
- INFO : Semantic Analysis Completed (retrial = false)
- INFO : Returning Hive schema: Schema(fieldSchemas:[FieldSchema(name:tab_name, type:string, comment:from deserializer)], properties:null)
- INFO : Completed compiling command(queryId=root_20230222140625_743ec057-5e62-4073-acc7-7b849fe994e2); Time taken: 0.572 seconds
- INFO : Concurrency mode is disabled, not creating a lock manager
- INFO : Executing command(queryId=root_20230222140625_743ec057-5e62-4073-acc7-7b849fe994e2): show tables
- INFO : Starting task [Stage-0:DDL] in serial mode
- INFO : Completed executing command(queryId=root_20230222140625_743ec057-5e62-4073-acc7-7b849fe994e2); Time taken: 0.08 seconds
- INFO : OK
- INFO : Concurrency mode is disabled, not creating a lock manager
- +-----------+
- | tab_name |
- +-----------+
- | stu |
- +-----------+
- 1 row selected (0.951 seconds)
- 0: jdbc:hive2://hadoop001:10000>
- 0: jdbc:hive2://hadoop001:10000> select * from stu;
- INFO : Compiling command(queryId=root_20230222140741_60a79525-fab0-483c-8e0f-a8090fa79107): select * from stu
- INFO : Concurrency mode is disabled, not creating a lock manager
- INFO : Semantic Analysis Completed (retrial = false)
- INFO : Returning Hive schema: Schema(fieldSchemas:[FieldSchema(name:stu.id, type:int, comment:null), FieldSchema(name:stu.name, type:string, comment:null)], properties:null)
- INFO : Completed compiling command(queryId=root_20230222140741_60a79525-fab0-483c-8e0f-a8090fa79107); Time taken: 1.132 seconds
- +---------+-----------+
- | stu.id | stu.name |
- +---------+-----------+
- | 1 | ss |
- +---------+-----------+
- 1 row selected (1.363 seconds)
- INFO : Concurrency mode is disabled, not creating a lock manager
- INFO : Executing command(queryId=root_20230222140741_60a79525-fab0-483c-8e0f-a8090fa79107): select * from stu
- INFO : Completed executing command(queryId=root_20230222140741_60a79525-fab0-483c-8e0f-a8090fa79107); Time taken: 0.001 seconds
- INFO : OK
- INFO : Concurrency mode is disabled, not creating a lock manager
- 0: jdbc:hive2://hadoop001:10000>
(4)一键登录
beeline -u jdbc:hive2://hadoop001:10000 -n root
2、图形化客户端
免费的DBeaver
收费的DataGrip图形化客户端
八、Metastore概述
Hive的metastore服务的作用是为Hive CLI或者Hiveserver2提供元数据访问接口。
1、metastore运行模式
metastore有两种运行模式,分别为嵌入式模式和独立服务模式。下面分别对两种模式进行说明。
(1)嵌入式模式
获取元数据时,Hiveserver2、Hive CLI直接连接mysql数据库。
(2)独立服务模式
单独启动一个metastore服务。
2、生产环境中,不推荐使用嵌入式模式
(1)嵌入式模式下,每个Hive CLI都需要直接连接元数据库,当Hive CLI较多时,数据库压力会比较大。
(2)每个客户端都需要用户元数据库的读写权限,元数据库的安全得不到很好的保证。
九、Metastore配置
1、嵌入式模式配置
嵌入式模式下,只需保证Hiveserver2和每个Hive CLI的配置文件hive-site.xml中包含连接元数据所需要的参数即可。
- <!-- jdbc连接的URL -->
- <property>
- <name>javax.jdo.option.ConnectionURL</name>
- <value>jdbc:mysql://192.168.52.11:3306/metastore?serverTimezone=UTC</value>
- <description>
- JDBC connect string for a JDBC metastore.
- To use SSL to encrypt/authenticate the connection, provide database-specific SSL flag in the connection URL.
- For example, jdbc:postgresql://myhost/db?ssl=true for postgres database.
- </description>
- </property>
- <!-- jdbc连接的Driver -->
- <property>
- <name>javax.jdo.option.ConnectionDriverName</name>
- <value>com.mysql.cj.jdbc.Driver</value>
- <description>Driver class name for a JDBC metastore</description>
- </property>
- <!-- jdbc连接的username -->
- <property>
- <name>javax.jdo.option.ConnectionUserName</name>
- <value>hive</value>
- <description>Username to use against metastore database</description>
- </property>
- <!-- jdbc连接的password -->
- <property>
- <name>javax.jdo.option.ConnectionPassword</name>
- <value>hive</value>
- <description>password to use against metastore database</description>
- </property>
2、独立服务模式配置
首先,保证metastore服务的配置文件hive-site.xml中包含连接元数据库所需的参数。
其次,保证Hiveserver2和每个Hive CLI的配置文件hive-site.xml中包含访问metastore服务的参数。
- <!-- 指定metastore服务的地址 -->
- <property>
- <name>hive.metastore.uris</name>
- <value>thrift://hadoop001:9083</value>
- </property>
注意:主机名需要改为metastore服务所在节点,端口号无需修改,metastore服务的默认端口就是9083。
3、启动metastore服务
nohup hive --service metastore &
4、停止metastore服务
ps -ef|grep metastore|grep -v 'grep'|awk '{print $2}'|xargs -n1 kill -9
5、查看
- jps -ml
- 3009 org.apache.zookeeper.server.quorum.QuorumPeerMain /appserver/zookeeper/bin/../conf/zoo.cfg
- 3587 org.apache.hadoop.util.RunJar /appserver/hive/lib/hive-service-3.1.3.jar org.apache.hive.service.server.HiveServer2
- 3492 org.apache.hadoop.hbase.regionserver.HRegionServer start
- 2950 org.apache.hadoop.mapreduce.v2.hs.JobHistoryServer
- 6118 sun.tools.jps.Jps -ml
- 5993 org.apache.hadoop.util.RunJar /appserver/hive/lib/hive-metastore-3.1.3.jar org.apache.hadoop.hive.metastore.HiveMetaStore
- 1754 org.apache.hadoop.hdfs.server.namenode.NameNode
- 3274 org.apache.hadoop.hbase.master.HMaster start
- 2571 org.apache.hadoop.yarn.server.nodemanager.NodeManager
- 1901 org.apache.hadoop.hdfs.server.datanode.DataNode
- 2159 org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode
- 2415 org.apache.hadoop.yarn.server.resourcemanager.ResourceManager
6、如果同时配置jdbc和metastore,优先使用metastore。
参考资料:
启动脚本
https://blog.51cto.com/qchenz/5847300
十、其他
1、start-BigData.sh
- #!/bin/bash
- echo "============================================"
- /appserver/hadoop/hadoop-3.3.4/sbin/start-dfs.sh
- /appserver/hadoop/hadoop-3.3.4/sbin/start-yarn.sh
- mapred --daemon start historyserver
- echo "============================================"
- /appserver/zookeeper/bin/zkServer.sh start
- start-hbase.sh
- echo "============================================"
- nohup hive --service metastore &
- echo "============================================"
- nohup hiveserver2 >> /appserver/hive/logs/hiveserver2.log 2>&1 &
- echo "============================================"
2、stop-BigData.sh
- #!/bin/bash
- echo "============================================"
- ps -ef|grep hiveserver2|grep -v 'grep'|awk '{print $2}'|xargs -n1 kill -9
- echo "============================================"
- ps -ef|grep metastore|grep -v 'grep'|awk '{print $2}'|xargs -n1 kill -9
- echo "============================================"
- stop-hbase.sh
- /appserver/zookeeper/bin/zkServer.sh stop
- echo "============================================"
- mapred --daemon stop historyserver
- /appserver/hadoop/hadoop-3.3.4/sbin/stop-yarn.sh
- /appserver/hadoop/hadoop-3.3.4/sbin/stop-dfs.sh
- echo "============================================"
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。