Hive
基础
概念
- 基于Hadoop的数据仓库工具,将结构化的数据文件映射为一张表,提供类SQL查询功能
- 本质:将HQL转化成MapReduce程序
- Hive处理的数据存储在HDFS
- Hive分析数据底层的实现是MapReduce/Sprak…
- 执行程序运行在Yarn上
- 优点
- 简单、容易上手
- 支持用户自定义函数
- 缺点
- HQL表达能力有限:迭代式算法无法表达
- 效率比较低
- 官网地址:http://hive.apache.org/
- 文档查看地址:https://cwiki.apache.org/confluence/display/Hive/GettingStarted
- 下载地址:http://archive.apache.org/dist/hive/
- github地址:https://github.com/apache/hive
安装
安装MySql
Hive默认使用的元数据库为derby,开启Hive之后就会占用元数据库,且不与其他客户端共享数据,如果想多窗口操作就会报错,操作比较局限
修改mysql库下的user表中的root用户允许任意ip连接
1
2
3
4
5
6
7alter user 'root'@'localhost' identified with mysql_native_password by 'root';
update mysql.user set host='%' where user='root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY PASSWORD 'root';
flush privileges;
vim /etc/mysql/mysql.conf.d/mysqld.cnf
bind_address=0.0.0.0
安装Hive
添加环境变量:/etc/profile
1
2
3#HIVE_HOME
export HIVE_HOME=/hive
export PATH=$PATH:$HIVE_HOME/bin解决日志Jar包冲突
1
mv $HIVE_HOME/lib/log4j-slf4j-impl-2.10.0.jar $HIVE_HOME/lib/log4j-slf4j-impl-2.10.0.bak
拷贝MySQL驱动
1
cp /opt/software/mysql-connector-java-5.1.48.jar $HIVE_HOME/lib
配置Metastore到MySql
1
2# 在$HIVE_HOME/conf目录下新建hive-site.xml文件
vim $HIVE_HOME/conf/hive-site.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- jdbc连接的URL -->
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://bigdata1:3306/metastore?useSSL=false</value>
</property>
<!-- jdbc连接的Driver-->
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.cj.jdbc.Driver</value>
</property>
<!-- jdbc连接的username-->
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
</property>
<!-- jdbc连接的password -->
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>root</value>
</property>
<!-- Hive默认在HDFS的工作目录 -->
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
</property>
<!-- Hive元数据存储的验证 -->
<property>
<name>hive.metastore.schema.verification</name>
<value>false</value>
</property>
<!-- 元数据存储授权 -->
<property>
<name>hive.metastore.event.db.notification.api.auth</name>
<value>false</value>
</property>
<!-- 打印表头 -->
<property>
<name>hive.cli.print.header</name>
<value>true</value>
</property>
<!-- 显示所在数据库 -->
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
</property>
</configuration>配置运行日志:hive-log4j2.properties
1
2# Hive的log默认存放在/tmp/atguigu/hive.log目录下(当前用户名下)
property.hive.log.dir=/hive/logs新建Hive元数据库
1
create database metastore;
初始化Hive元数据库
1
schematool -initSchema -dbType mysql -verbose
启动hadoop集群
启动Hive
1
bin/hive
使用Hive
1
2
3
4
5show databases;
show tables;
create table test (id int);
insert into test values(1);
select * from test;使用元数据服务的方式访问Hive
若配置过以后以后使用Hive的时候一定要启动元数据服务
在hive-site.xml文件中添加如下配置信息
1
2
3
4
5<!-- 指定存储元数据要连接的地址 -->
<property>
<name>hive.metastore.uris</name>
<value>thrift://bigdata1:9083</value>
</property>启动metastore
1
2# 启动后窗口不能再操作,需打开一个新的shell窗口做别的操作
hive --service metastore启动hive
使用JDBC方式访问Hive
在hive-site.xml文件中添加如下配置信息
1
2
3
4
5
6
7
8
9
10
11<!-- 指定hiveserver2连接的host -->
<property>
<name>hive.server2.thrift.bind.host</name>
<value>bigdata1</value>
</property>
<!-- 指定hiveserver2连接的端口号 -->
<property>
<name>hive.server2.thrift.port</name>
<value>10000</value>
</property>启动hiveserver2
1
2# 启动后窗口不能再操作,需打开一个新的shell窗口做别的操作
bin/hive --service hiveserver2启动beeline客户端
1
bin/beeline -u jdbc:hive2://bigdata1:10000 -n 用户名
编写启动脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59vim $HIVE_HOME/bin/hiveservices.sh
#!/bin/bash
HIVE_LOG_DIR=$HIVE_HOME/logs
if [ ! -d $HIVE_LOG_DIR ]
then
mkdir -p $HIVE_LOG_DIR
fi
#检查进程是否运行正常,参数1为进程名,参数2为进程端口
function check_process()
{
pid=$(ps -ef 2>/dev/null | grep -v grep | grep -i $1 | awk '{print $2}')
ppid=$(netstat -nltp 2>/dev/null | grep $2 | awk '{print $7}' | cut -d '/' -f 1)
echo $pid
[[ "$pid" =~ "$ppid" ]] && [ "$ppid" ] && return 0 || return 1
}
function hive_start()
{
metapid=$(check_process HiveMetastore 9083)
cmd="nohup hive --service metastore >$HIVE_LOG_DIR/metastore.log 2>&1 &"
cmd=$cmd" sleep 4; hdfs dfsadmin -safemode wait >/dev/null 2>&1"
[ -z "$metapid" ] && eval $cmd || echo "Metastroe服务已启动"
server2pid=$(check_process HiveServer2 10000)
cmd="nohup hive --service hiveserver2 >$HIVE_LOG_DIR/hiveServer2.log 2>&1 &"
[ -z "$server2pid" ] && eval $cmd || echo "HiveServer2服务已启动"
}
function hive_stop()
{
metapid=$(check_process HiveMetastore 9083)
[ "$metapid" ] && kill $metapid || echo "Metastore服务未启动"
server2pid=$(check_process HiveServer2 10000)
[ "$server2pid" ] && kill $server2pid || echo "HiveServer2服务未启动"
}
case $1 in
"start")
hive_start
;;
"stop")
hive_stop
;;
"restart")
hive_stop
sleep 2
hive_start
;;
"status")
check_process HiveMetastore 9083 >/dev/null && echo "Metastore服务运行正常" || echo "Metastore服务运行异常"
check_process HiveServer2 10000 >/dev/null && echo "HiveServer2服务运行正常" || echo "HiveServer2服务运行异常"
;;
*)
echo Invalid Args!
echo 'Usage: '$(basename $0)' start|stop|restart|status'
;;
esac
# 添加执行权限
chmod u+x $HIVE_HOME/bin/hiveservices.sh参数配置方式
配置文件方式:用户自定义配置会覆盖默认配置
- Hive也会读入Hadoop的配置,因为Hive是作为Hadoop的客户端启动的
命令行参数方式:仅对本次hive启动有效
1
hive -hiveconf mapred.reduce.tasks=10;
参数声明方式:在HQL中使用SET关键字设定参数,仅对本次hive启动有效
1
2
3
4# 查看
set mapred.reduce.tasks
# 设置
set mapred.reduce.tasks=100;
命令
- -e:不进入hive的交互窗口执行sql语句
- -f:执行脚本中sql语句
- 在hive cli中查看hdfs文件系统:
dfs -ls /;
- 查看所有历史命令(家目录下):
cat .hivehistory
数据类型
基本数据类型
Hive | Java | 长度 | 例子 |
---|---|---|---|
TINYINT | byte | 1byte有符号整数 | 20 |
SMALINT | short | 2byte有符号整数 | 20 |
INT | int | 4byte有符号整数 | 20 |
BIGINT | long | 8byte有符号整数 | 20 |
BOOLEAN | boolean | 布尔类型,true或者false | TRUE FALSE |
FLOAT | float | 单精度浮点数 | 3.14159 |
DOUBLE | double | 双精度浮点数 | 3.14159 |
STRING | string | 字符系列。可以指定字符集。可以使用单引号或者双引号。 | ‘now is the time’ “for all good men” |
TIMESTAMP | 时间类型 | ||
BINARY | 字节数组 |
集合数据类型
数据类型 | 描述 | 语法示例 |
---|---|---|
STRUCT | 和c语言中的struct类似,都可以通过“点”符号访问元素内容。例如,如果某个列的数据类型是STRUCT{first STRING, last STRING},那么第1个元素可以通过字段.first来引用。 | struct() 例如struct<street:string, city:string> |
MAP | MAP是一组键-值对元组集合,使用数组表示法可以访问数据。例如,如果某个列的数据类型是MAP,其中键->值对是’first’->’John’和’last’->’Doe’,那么可以通过字段名[‘last’]获取最后一个元素 | map() 例如map<string, int> |
ARRAY | 数组是一组具有相同类型和名称的变量的集合。这些变量称为数组的元素,每个数组元素都有一个编号,编号从零开始。例如,数组值为[‘John’, ‘Doe’],那么第2个元素可以通过数组名[1]进行引用。 | Array() 例如array<string> |
案例
1 |
|
1 |
|
类型转换
隐式类型转换规则
- 整数类型可转换为范围更广的类型
- 整数类型、FLOAT和STRING类型可转换成DOUBLE
- TINYINT、SMALLINT、INT可转换为FLOAT
- BOOLEAN不可以转换为其它的类型
显示类型转换
使用
CAST()
转换,若转换失败则为NULL1
select '1'+2, cast('1'as int) + 2;
DDL数据定义
数据库
建库语法
1 |
|
创建库
1 |
|
查看库
1 |
|
修改库
1 |
|
删除库
1 |
|
数据表
建表语法
1 |
|
内部表
- 默认,删除表时,Hive会删除表中数据
1 |
|
外部表
- 删除表时,Hive不会删除表中数据
1 |
|
查看表
1 |
|
修改表
1 |
|
删除表
1 |
|
DML数据操作
导入数据
Load导入
1 |
|
1 |
|
Insert导入
insert into
:以追加数据的方式插入到表或分区,原有数据不会删除insert overwrite
:会覆盖表中已存在的数据- insert不支持插入部分字段
1 |
|
As Select导入
1 |
|
Location导入
1 |
|
Import导入
- 先用
export
导出后,再将数据导入
1 |
|
导出数据
概述
- 如果表中的列的值为null,导出到文件中以后通过
\N
来表示
Insert导出
1 |
|
Hadoop导出
1 |
|
Hive Shell导出
1 |
|
Export导出
export
和import
主要用于两个Hadoop平台集群之间Hive表迁移(还会导出元数据)
1 |
|
Sqoop导出
清除表中数据
1 |
|
查询
查询语法
1 |
|
运算符
算数运算符
运算符 | 描述 |
---|---|
A+B | A和B 相加 |
A-B | A减去B |
A*B | A和B 相乘 |
A/B | A除以B |
A%B | A对B取余 |
A&B | A和B按位取与 |
A|B | A和B按位取或 |
A^B | A和B按位取异或 |
~A | A按位取反 |
比较运算符
操作符 | 支持的数据类型 | 描述 |
---|---|---|
A=B | 基本数据类型 | 如果A等于B则返回TRUE,反之返回FALSE |
A<=>B | 基本数据类型 | 如果A和B都为NULL,则返回TRUE,如果一边为NULL,返回False |
A<>B, A!=B | 基本数据类型 | A或者B为NULL则返回NULL;如果A不等于B,则返回TRUE,反之返回FALSE |
A<B | 基本数据类型 | A或者B为NULL,则返回NULL;如果A小于B,则返回TRUE,反之返回FALSE |
A<=B | 基本数据类型 | A或者B为NULL,则返回NULL;如果A小于等于B,则返回TRUE,反之返回FALSE |
A>B | 基本数据类型 | A或者B为NULL,则返回NULL;如果A大于B,则返回TRUE,反之返回FALSE |
A>=B | 基本数据类型 | A或者B为NULL,则返回NULL;如果A大于等于B,则返回TRUE,反之返回FALSE |
A [NOT] BETWEEN B AND C | 基本数据类型 | 如果A,B或者C任一为NULL,则结果为NULL。如果A的值大于等于B而且小于或等于C,则结果为TRUE,反之为FALSE。如果使用NOT关键字则可达到相反的效果。 |
A IS NULL | 所有数据类型 | 如果A等于NULL,则返回TRUE,反之返回FALSE |
A IS NOT NULL | 所有数据类型 | 如果A不等于NULL,则返回TRUE,反之返回FALSE |
IN(数值1, 数值2) | 所有数据类型 | 使用 IN运算显示列表中的值 |
A [NOT] LIKE B | STRING 类型 | B是一个SQL下的简单正则表达式,也叫通配符模式,如果A与其匹配的话,则返回TRUE;反之返回FALSE。B的表达式说明如下:‘x%’表示A必须以字母‘x’开头,‘%x’表示A必须以字母’x’结尾,而‘%x%’表示A包含有字母’x’,可以位于开头,结尾或者字符串中间。如果使用NOT关键字则可达到相反的效果。 |
A RLIKE B, A REGEXP B | STRING 类型 | B是基于java的正则表达式,如果A与其匹配,则返回TRUE;反之返回FALSE。匹配使用的是JDK中的正则表达式接口实现的,因为正则也依据其中的规则。例如,正则表达式必须和整个字符串A相匹配,而不是只需与其字符串匹配。 |
RLike
- 是Hive中这个功能的一个扩展,其可以通过Java的正则表达式这个更强大的语言来指定匹配条件
1 |
|
逻辑运算符
操作符 | 含义 |
---|---|
AND | 逻辑并 |
OR | 逻辑或 |
NOT | 逻辑否 |
分组
分组之后,select后面只能跟组标识(分组字段)和聚合函数(分组函数)
计算emp表每个部门的平均工资
1
select deptno ,avg(sal) avg_sal from emp group by deptno
计算emp每个部门中每个岗位的最高薪水
1
select deptno ,job ,max(sal) max_sal from emp group by deptno,job;
计算emp中每个部门中最高薪水的那个人.
1
2
3
4
5
6
7
8
9
10
11-- 错误
select deptno ,max(sal) max_sal ,ename from emp group by deptno
-- 正确
select
e.deptno , e.sal , e.ename
from
emp e
join
(select deptno ,max(sal) max_sal from emp group by deptno ) a
on e.deptno = a.deptno and e.sal = a.max_sal;计算emp中除了CLERK岗位之外的剩余员工的每个部门的平均工资大于1000的部门和平均工资
1
2
3
4
5
6
7
8
9
10select
deptno , avg(sal) avg_sal
from
emp
where
job != 'CLERK'
group by
deptno
having
avg_sal >2000;
Join
内连接
内连接的结果集取交集
1
2
3
4
5
6
7
8A inner join B on ...
-- emp 和 dept共有的数据(内连接)
select
e.ename, d.deptno
from
emp e inner join dept d
on e.deptno = d.deptno;
外连接
主表(驱动表)和从表(匹配表)
外连接的结果集主表的所有数据 + 从表中与主表匹配的数据
左外连接
1 |
|
右外连接
1 |
|
1 |
|
全外连接
- union all:将结果集拼接到一起,不去重
- union:将结果集拼接到一起,去重
- full outer join
1 |
|
排序
Order By
- 全局排序,只有一个Reducer
Sort By
- 每个Reduce内部排序
- 大规模数据集order by效率非常低。很多情况不需要全局排序,可使用sort by
- Sort by为每个reducer产生一个排序文件,进行内部排序,对全局结果集不是排序
- 一般与分区一起用,未指定分区则会随机分区
Distribute By
- 分区
- distribute by可以指定运行的Reducer(通常是为了进行后续的聚集操作)
- 类似MR中partition(自定义分区)进行分区,结合sort by使用
- 一定要分配多reduce进行处理,否则无法看到distribute by的效果
- 根据分区字段的hash码与reduce的个数进行模除运算得出
- DISTRIBUTE BY语句要写在SORT BY语句之前
1 |
|
Cluster By
- distribute by和sort by字段相同时,可以使用cluster by方式
- 只能是升序排序,不能指定排序规则
1 |
|
分区/分桶表
分区表
概念
- Hive中的分区就是分目录
分区基本操作
创建分区表
1 |
|
查分区
1 |
|
加分区
1 |
|
删分区
1 |
|
二级分区
创建二级分区表
1 |
|
分区表与数据产生关联的方式
上传数据后修复
1
2
3
4
5
6-- 创建分区目录
hadoop fs -mkdir /user/hive/warehouse/mydb.db/dept_partition/day=20200404
-- 上传数据
hadoop fs -put /opt/module/hive-3.1.2/datas/dept_20200403.log /user/hive/warehouse/mydb.db/dept_partition/day=20200404
-- 分区修复
msck repair table dept_partition;上传数据后添加分区
1
2
3
4
5
6-- 创建分区目录
hadoop fs -mkdir /user/hive/warehouse/mydb.db/dept_partition/day=20200405
-- 上传数据
hadoop fs -put /opt/module/hive-3.1.2/datas/dept_20200403.log /user/hive/warehouse/mydb.db/dept_partition/day=20200405
-- 添加分区
alter table dept_partition add partition(day='20200405');创建文件夹后load数据到分区
1
2
3
4-- 创建分区目录
hadoop fs -mkdir /user/hive/warehouse/mydb.db/dept_partition/day=20200406
-- Load数据到分区
load data local inpath '/opt/module/hive-3.1.2/datas/dept_20200403.log' into table dept_partition partition(day='20200406')
动态分区
参数设置
1
2
3
4
5
6
7
8
9
10
11
12# 开启动态分区功能(默认true,开启)
hive.exec.dynamic.partition=true
# 设置为非严格模式(动态分区的模式,默认strict,表示必须指定至少一个分区为静态分区,nonstrict模式表示允许所有的分区字段都可以使用动态分区。)
hive.exec.dynamic.partition.mode=nonstrict
# 在所有执行MR的节点上,最大一共可以创建多少个动态分区。默认1000
hive.exec.max.dynamic.partitions=1000
# 在每个执行MR的节点上,最大可以创建多少个动态分区。该参数需要根据实际的数据来设定。比如:源数据中包含了一年的数据,即day字段有365个值,那么该参数就需要设置成大于365,如果使用默认值100,则会报错。
hive.exec.max.dynamic.partitions.pernode=100
# 整个MR Job中,最大可以创建多少个HDFS文件。默认100000
hive.exec.max.created.files=100000
# 当有空分区生成时,是否抛出异常。一般不需要设置。默认false
hive.error.on.empty.partition=false具体操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22-- 创建分区表
create table dept_partition_dy(
deptno int,
dname string,
loc string
)
partitioned by (day string)
row format delimited fields terminated by '\t';
-- 从本地load可能会出错,MapTask节点可能没当前的数据
-- 老版本load操作不跑MR会报错,需要使用到中间表
load data inpath '/dept_partition_dy.txt' into table dept_partition_dy;
create table dept_dy(
deptno int,
dname string,
loc string,
day string
)
row format delimited fields terminated by '\t';
load data local inpath '/opt/module/hive-3.1.2/datas/dept_partition_dy.txt' into table dept_dy;
分桶表
分区针对的是数据的存储路径,分桶针对的是数据文件
分桶字段与桶数进行取模操作
创建分桶表
1
2
3
4
5
6
7
8
9
10
11create table student_bucket(
id int,
name string
)
-- 按照id分桶
clustered by (id) into 4 buckets
row format delimited fields terminated by '\t';
-- 从本地load可能会出错,MapTask节点可能没当前的数据
-- 老版本load操作不跑MR会报错,需要使用到中间表
load data inpath '/student.txt' into table student_bucket;
抽样查询
对于非常大的数据集,有时用户需要使用的是一个具有代表性的查询结果而不是全部结果。Hive可以通过对表进行抽样来满足这个需求
1
2
3
4
5
6-- x:第几个桶的数据
-- y:分几个桶
-- x的值必须小于等于y的值,否则报错
TABLESAMPLE(BUCKET x OUT OF y)
select * from stu_buck tablesample(bucket 1 out of 4 on id);
函数
常用函数
系统内置函数
1 |
|
日期函数
- Hive能识别的日期格式
yyyy-MM-dd HH:mm:ss
1 |
|
字符串函数
1 |
|
取整函数
1 |
|
集合操作
1 |
|
多维分析
1 |
|
空字段赋值
- 如果value为NULL,则NVL函数返回default_value的值,否则返回value的值,如果两个参数都为NULL ,则返回NULL
1 |
|
CASE函数
1 |
|
行转列
1 |
|
列转行
1 |
|
窗口函数
1 |
|
Rank
1 |
|
自定义函数
步骤
官方地址:https://cwiki.apache.org/confluence/display/Hive/HivePlugins
导入依赖
1
2
3
4
5<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>3.1.2</version>
</dependency>继承Hive提供的类
1
2org.apache.hadoop.hive.ql.udf.generic.GenericUDF
org.apache.hadoop.hive.ql.udf.generic.GenericUDTF实现类中的抽象方法
运行函数
方式1:永久使用
- 放入lib目录
- 重启Hive
方式2:临时使用,hive窗口关闭后会失效
在hive的命令行窗口创建函数
添加jar
1
add jar linux_jar_path
创建function
1
2
3create [temporary] function [dbname.]function_name AS class_name;
create temporary function mylen as 'cc.mousse.CalStrLenUDF';在hive的命令行窗口删除函数
1
drop [temporary] function [if exists] [dbname.]function_name;
UDF函数
- user-defined function,一进一出
1 |
|
UDTF函数
- User-Defined Table-Generating Functions,一进多出
1 |
|
UDAF函数
- User-Defined Aggregation Function,聚集函数,多进一出
压缩存储
压缩
参数配置
- mapred-site.xml
参数 | 默认值 | 阶段 | 建议 |
---|---|---|---|
io.compression.codecs (在core-site.xml中配置) | org.apache.hadoop.io.compress.DefaultCodec, org.apache.hadoop.io.compress.GzipCodec, org.apache.hadoop.io.compress.BZip2Codec, org.apache.hadoop.io.compress.Lz4Codec | 输入压缩 | Hadoop使用文件扩展名判断是否支持某种编解码器 |
mapreduce.map.output.compress | false | mapper输出 | 这个参数设为true启用压缩 |
mapreduce.map.output.compress.codec | org.apache.hadoop.io.compress.DefaultCodec | mapper输出 | 使用LZO、LZ4或snappy编解码器在此阶段压缩数据 |
mapreduce.output.fileoutputformat.compress | false | reducer输出 | 这个参数设为true启用压缩 |
mapreduce.output.fileoutputformat.compress.codec | org.apache.hadoop.io.compress. DefaultCodec | reducer输出 | 使用标准工具或者编解码器,如gzip和bzip2 |
mapreduce.output.fileoutputformat.compress.type | RECORD | reducer输出 | SequenceFile输出使用的压缩类型:NONE和BLOCK |
开启Map输出阶段压缩
开启hive中间传输数据压缩功能
1
set hive.exec.compress.intermediate=true;
开启mapreduce中map输出压缩功能
1
set mapreduce.map.output.compress=true;
设置mapreduce中map输出数据的压缩方式
1
2set mapreduce.map.output.compress.codec=
org.apache.hadoop.io.compress.SnappyCodec;执行查询语句
1
select count(ename) name from emp;
开启Reduce输出阶段压缩
开启hive最终输出数据压缩功能
1
set hive.exec.compress.output=true;
开启mapreduce最终输出数据压缩
1
set mapreduce.output.fileoutputformat.compress=true;
设置mapreduce最终数据输出压缩为块压缩
1
set mapreduce.output.fileoutputformat.compress.type=BLOCK;
测试一下输出结果是否是压缩文件
1
2insert overwrite local directory
'/opt/module/hive/datas/distribute-result' select * from emp distribute by deptno sort by empno desc;
存储
创建
1 |
|
列式存储和行式存储
1 |
|
- 行存储
- 查询满足条件的一整行数据的时候,列存储则需要去每个聚集的字段找到对应的每个列的值,行存储只需要找到其中一个值,其余的值都在相邻地方,所以此时行存储查询的速度更快
- TEXTFILE,SEQUENCEFILE
- 列存储
- 因为每个字段的数据聚集存储,在查询只需要少数几个字段的时候,能大大减少读取的数据量;每个字段的数据类型一定是相同的,列式存储可以针对性的设计更好的设计压缩算法
- ORC,PARQUET
TextFile格式
- 默认,数据不压缩,磁盘开销大,数据解析开销大
- 可结合Gzip、Bzip2使用,但使用Gzip这种方式,hive不会对数据进行切分,从而无法对数据进行并行操作
Orc格式
1 |
|
由1个或多个stripe组成,每个stripe一般为HDFS的块大小,每一个stripe包含多条记录,这些记录按照列进行独立存储
不能直接load
默认使用ZLIB压缩
1
2
3
4
5
6create table xxx(
)
stored as orc
tblproperties("orc.compress"="ZLIB");
-- 设置orc存储不使用压缩
tblproperties("orc.compress"="NONE");
Parquet格式
以二进制方式存,不可直接读取,文件中包括该文件的数据和元数据,自解析
1
2stored as parquet
tblproperties("parquet.compression"="SNAPPY");
调优
执行计划
不会真正执行SQL
基本语法
1
EXPLAIN [EXTENDED | DEPENDENCY | AUTHORIZATION] query
查看详细执行计划
1
2explain extended select * from emp;
explain extended select deptno, avg(sal) avg_sal from emp group by deptno;
Fetch抓取
Hive中对某些情况的查询可以不必使用MapReduce计算
该属性修改为more以后,在全局查找、字段查找、limit查找等都不走mapreduce
1
2
3
4
5
6
7
8
9
10
11
12<property>
<name>hive.fetch.task.conversion</name>
<value>more</value>
<description>
Expects one of [none, minimal, more].
Some select queries can be converted to single FETCH task minimizing latency.
Currently the query should be single sourced not having any subquery and should not have any aggregations or distincts (which incurs RS), lateral views and joins.
0. none : disable hive.fetch.task.conversion
1. minimal : SELECT STAR, FILTER on partition columns, LIMIT only
2. more : SELECT, FILTER, LIMIT only (support TABLESAMPLE and virtual columns)
</description>
</property>
本地模式
可以通过本地模式在单台机器上处理所有的任务。对于小数据集,执行时间可以明显被缩短
可以通过设置hive.exec.mode.local.auto的值为true,来让Hive在适当的时候自动启动这个优化
1
2
3
4
5
6-- 开启本地mr
set hive.exec.mode.local.auto=true;
-- 设置local mr的最大输入数据量,当输入数据量小于这个值时采用local mr的方式,默认为134217728,即128M
set hive.exec.mode.local.auto.inputbytes.max=50000000;
-- 设置local mr的最大输入文件个数,当输入文件个数小于这个值时采用local mr的方式,默认为4
set hive.exec.mode.local.auto.input.files.max=10;
表的优化
小表Join大表
新版的hive已经对小表JOIN大表和大表JOIN小表进行了优化。小表放在左边和右边已经没有明显区别
参数设置
1
2
3
4-- 设置自动选择Mapjoin,默认为true
set hive.auto.convert.join = true;
-- 大表小表的阈值设置(默认25M以下认为是小表)
set hive.mapjoin.smalltable.filesize = 25000000;
大表Join大表
空Key过滤
- 有时join超时是因为某些key对应的数据太多,而相同key对应的数据都会发送到相同的reducer上,从而导致内存不够。此时我们应该仔细分析这些异常的key,很多情况下,这些key对应的数据是异常数据,我们需要在SQL语句中进行过滤。例如key对应的字段为空
1 |
|
空Key转换
- 有时虽然某个key为空对应的数据很多,但是相应的数据不是异常数据,必须要包含在join的结果中,此时我们可以表a中key为空的字段赋一个随机的值,使得数据随机均匀地分不到不同的reducer上
1 |
|
Sort Merge Bucket join
1 |
|
Group By
默认Map阶段同一Key数据分发给一个reduce,当一个key数据过大时就倾斜
不是所有的聚合操作都需要在Reduce端完成,很多聚合操作都可以先在Map端进行部分聚合,最后在Reduce端得出最终结果
开启Map端聚合参数设置
1
2
3
4
5
6
7-- 是否在Map端进行聚合,默认为True
set hive.map.aggr = true
-- 在Map端进行聚合操作的条目数目
set hive.groupby.mapaggr.checkinterval = 100000
-- 有数据倾斜的时候进行负载均衡(默认是false)
set hive.groupby.skewindata = true
-- 当选项设定为 true,生成的查询计划会有两个MR Job。第一个MR Job中,Map的输出结果会随机分布到Reduce中,每个Reduce做部分聚合操作,并输出结果,这样处理的结果是相同的Group By Key有可能被分发到不同的Reduce中,从而达到负载均衡的目的;第二个MR Job再根据预处理的数据结果按照Group By Key分布到Reduce中(这个过程可以保证相同的Group By Key被分布到同一个Reduce中),最后完成最终的聚合操作
去重统计
- 数据量小的时候无所谓,数据量大的情况下,由于COUNT DISTINCT操作需要用一个Reduce Task来完成,这一个Reduce需要处理的数据量太大,就会导致整个Job很难完成,一般COUNT DISTINCT使用先GROUP BY再COUNT的方式替换,但是需要注意group by造成的数据倾斜问题
1 |
|
笛卡尔积
- 尽量避免笛卡尔积,join的时候不加on条件,或者无效的on条件,Hive只能使用1个reducer来完成笛卡尔积
行列过滤
- 列处理:在SELECT中,只拿需要的列,如果有分区,尽量使用分区过滤,少用SELECT *
- 行处理:在分区剪裁中,当使用外关联时,如果将副表的过滤条件写在Where后面,那么就会先全表关联,之后再过滤
1 |
|
分区分桶
设置MapReduce数
复杂文件增加Map数
当input的文件都很大,任务逻辑复杂,map执行非常慢的时候,可以考虑增加Map数
降低切片值增加Map数,反之减少
设置最大切片值为100个字节
1
2
3computeSliteSize(Math.max(minSize,Math.min(maxSize,blocksize)))=blocksize=128M
set mapreduce.input.fileinputformat.split.maxsize=100;
小文件进行合并
在map执行前合并小文件,减少map数
CombineHiveInputFormat具有对小文件进行合并的功能(系统默认的格式),HiveInputFormat没有对小文件合并功能
1
set hive.input.format= org.apache.hadoop.hive.ql.io.CombineHiveInputFormat;
在MapReduce的任务结束时合并小文件的设置
1
2
3
4
5
6
7
8-- 在map-only任务结束时合并小文件,默认true
SET hive.merge.mapfiles = true;
-- 在map-reduce任务结束时合并小文件,默认false
SET hive.merge.mapredfiles = true;
-- 合并文件的大小,默认256M
SET hive.merge.size.per.task = 268435456;
-- 当输出文件的平均大小小于该值时,启动一个独立的map-reduce任务进行文件merge
SET hive.merge.smallfiles.avgsize = 16777216;
合理设置Reduce数
处理大数据量利用合适的reduce数;使单个reduce任务处理数据量大小要合适
调整方法1
1
2
3
4
5
6-- 每个Reduce处理的数据量默认是256MB
hive.exec.reducers.bytes.per.reducer=256000000
-- 每个任务最大的reduce数,默认为1009
hive.exec.reducers.max=1009
-- 计算reducer数的公式
N=min(参数2,总输入数据量/参数1)调整方法2:mapred-default.xml
1
2-- 设置每个job的Reduce个数
set mapreduce.job.reduces = 15;并行执行
1
2
3
4-- 打开任务并行执行
set hive.exec.parallel=true;
-- 同一个sql允许最大并行度,默认为8。
set hive.exec.parallel.thread.number=16;
严格模式
- 防止一些危险操作
- 分区表不使用分区过滤
hive.strict.checks.no.partition.filter=true
:对于分区表,除非where语句中含有分区字段过滤条件来限制范围,否则不允许执行
- 使用order by没有limit过滤
hive.strict.checks.orderby.no.limit=true
:使用了order by语句的查询,要求必须使用limit语句
- 笛卡尔积
hive.strict.checks.cartesian.product=true
:限制笛卡尔积的查询
实战
需求
1 |
|
数据结构
视频表
字段 备注 详细描述 videoId 视频唯一id(String) 11位字符串 uploader 视频上传者(String) 上传视频的用户名String age 视频年龄(int) 视频在平台上的整数天 category 视频类别(Array ) 上传视频指定的视频分类 length 视频长度(Int) 整形数字标识的视频长度 views 观看次数(Int) 视频被浏览的次数 rate 视频评分(Double) 满分5分 Ratings 流量(Int) 视频的流量,整型数字 conments 评论数(Int) 一个视频的整数评论数 relatedId 相关视频id(Array ) 相关视频的id,最多20个 用户表
字段 备注 字段类型 uploader 上传者用户名 string videos 上传视频数 int friends 朋友数量 int
准备工作
ELT
视频可以有多个所属分类,每个所属分类用&符号分割,且分割的两边有空格字符,同时相关视频也是可以有多个元素,多个相关视频又用“\t”进行分割。为了分析数据时方便对存在多个子元素的数据进行操作,我们首先进行数据重组清洗操作。即:将所有的类别用“&”分割,同时去掉两边空格,多个相关视频id也使用“&”进行分割
封装工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23/**
* 清洗视屏数据
*
* @param line 一行的数据
* @return 数据合法返回清洗完的数据,数据不合法返回null
*/
public static String videoData(String line) {
StringBuilder res = new StringBuilder();
String[] fields = line.split("\t");
int len = fields.length;
// 数据长度>9
if (len < 9) return null;
// 去掉视频类别中的空格
fields[3] = fields[3].replaceAll(" ", "");
// 将关联视频通过&拼接
for (int i = 0; i < len; i++) {
res.append(fields[i]);
if (i != len - 1) {
res.append(i > 8 ? "&" : "\t");
}
}
return res.toString();
}Mapper
1
2
3
4
5
6
7
8
9
10
11
12
13public class ETLMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
private final Text outKey = new Text();
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
String res = ETLUtils.videoData(value.toString());
if (res == null) return;
outKey.set(res);
context.write(outKey, NullWritable.get());
}
}Driver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class ETLDriver {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Job job = Job.getInstance(new Configuration());
job.setJarByClass(ETLDriver.class);
job.setMapperClass(ETLMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
job.setNumReduceTasks(0);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 1 : 0);
}
}程序打包为etl.jar并上传到/hive/datas 目录下
上传原始数据到HDFS
ETL数据
1
hadoop jar etl.jar cc.mousse.etl.ETLDriver /video/video/*.txt /video/video/output
数据表
创建原始数据表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26-- video_ori
create table video_ori(
videoId string,
uploader string,
age int,
category array<string>,
length int,
views int,
rate float,
ratings int,
comments int,
relatedId array<string>)
row format delimited fields terminated by "\t"
collection items terminated by "&"
stored as textfile
location '/video/video/output';
-- video_user_ori
create table video_user_ori(
uploader string,
videos int,
friends int)
row format delimited
fields terminated by "\t"
stored as textfile
location '/video/user/';创建最终表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31-- video_orc,带压缩
create table video_orc(
videoId string,
uploader string,
age int,
category array<string>,
length int,
views int,
rate float,
ratings int,
comments int,
relatedId array<string>)
stored as orc
tblproperties("orc.compress"="SNAPPY");
-- video_user_orc
create table video_user_orc(
uploader string,
videos int,
friends int)
row format delimited
fields terminated by "\t"
stored as orc
tblproperties("orc.compress"="SNAPPY");
-- 插入数据
load data inpath "/video/video/output" into table video_ori;
load data inpath "/video/user" into table video_user_ori;
insert into table video_orc select * from video_ori;
insert into table video_user_orc select * from video_user_ori;
安装Tez引擎(了解,现在已经不用了)
将tez**安装包拷贝到集群,并解压tar包
1
2mkdir /opt/module/tez
tar -zxvf /opt/software/tez-0.10.1-SNAPSHOT-minimal.tar.gz -C /opt/module/tez上传tez依赖到HDFS
1
2hadoop fs -mkdir /tez
hadoop fs -put /opt/software/tez-0.10.1-SNAPSHOT.tar.gz /tez新建tez-site.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37<!--
vim $HADOOP_HOME/etc/hadoop/tez-site.xml
添加如下内容:
-->
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>tez.lib.uris</name>
<value>${fs.defaultFS}/tez/tez-0.10.1-SNAPSHOT.tar.gz</value>
</property>
<property>
<name>tez.use.cluster.hadoop-libs</name>
<value>true</value>
</property>
<property>
<name>tez.am.resource.memory.mb</name>
<value>1024</value>
</property>
<property>
<name>tez.am.resource.cpu.vcores</name>
<value>1</value>
</property>
<property>
<name>tez.container.max.java.heap.fraction</name>
<value>0.4</value>
</property>
<property>
<name>tez.task.resource.memory.mb</name>
<value>1024</value>
</property>
<property>
<name>tez.task.resource.cpu.vcores</name>
<value>1</value>
</property>
</configuration>修改Hadoop环境变量
1
2
3
4
5
6
7
8
9vim $HADOOP_HOME/etc/hadoop/shellprofile.d/tez.sh
# 添加Tez的Jar包相关信息
hadoop_add_profile tez
function _tez_hadoop_classpath
{
hadoop_add_classpath "$HADOOP_HOME/etc/hadoop" after
hadoop_add_classpath "/opt/module/tez/*" after
hadoop_add_classpath "/opt/module/tez/lib/*" after
}修改Hive的计算引擎
1
2
3
4
5
6
7
8
9
10
11
12<!--
vim $HIVE_HOME/conf/hive-site.xml
-->
<property>
<name>hive.execution.engine</name>
<value>tez</value>
</property>
<property>
<name>hive.tez.container.size</name>
<value>1024</value>
</property>解决日志Jar包冲突
1
rm /opt/module/tez/lib/slf4j-log4j12-1.7.10.jar
业务分析
1 |
|
常见错误
更换Tez引擎后执行任务卡住,可以尝试调节容量调度器的资源调度策略
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<!--将$HADOOP_HOME/etc/hadoop/capacity-scheduler.xml文件中的-->
<property>
<name>yarn.scheduler.capacity.maximum-am-resource-percent</name>
<value>0.1</value>
<description>
Maximum percent of resources in the cluster which can be used to run
application masters i.e. controls number of concurrent running
applications.
</description>
</property>
<!--改成-->
<property>
<name>yarn.scheduler.capacity.maximum-am-resource-percent</name>
<value>1</value>
<description>
Maximum percent of resources in the cluster which can be used to run
application masters i.e. controls number of concurrent running
applications.
</description>
</property>连接不上mysql数据库
- 导错驱动包,应该把mysql-connector-java-5.1.27-bin.jar导入/opt/module/hive/lib的不是这个包。错把mysql-connector-java-5.1.27.tar.gz导入hive/lib包下
- 修改user表中的主机名称没有都修改为%,而是修改为localhost
hive默认的输入格式处理是CombineHiveInputFormat,会对小文件进行合并
1
2
3
4set hive.input.format;
hive.input.format=org.apache.hadoop.hive.ql.io.CombineHiveInputFormat
-- 可以采用HiveInputFormat就会根据分区数输出相应的文件。
set hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;不能执行mapreduce程序
- 可能是hadoop的yarn没开启
启动mysql服务时,报MySQL server PID file could not be found!异常
- 在/var/lock/subsys/mysql路径下创建hadoop102.pid,并在文件中添加内容:4396
报service mysql status MySQL is not running, but lock file (/var/lock/subsys/mysql[失败])异常
- 在/var/lib/mysql 目录下创建: -rw-rw—-. 1 mysql mysql 5 12月 22 16:41 hadoop102.pid 文件,并修改权限为 777
JVM堆内存溢
- 在yarn-site.xml中加入如下代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<property>
<name>yarn.scheduler.maximum-allocation-mb</name>
<value>2048</value>
</property>
<property>
<name>yarn.scheduler.minimum-allocation-mb</name>
<value>2048</value>
</property>
<property>
<name>yarn.nodemanager.vmem-pmem-ratio</name>
<value>2.1</value>
</property>
<property>
<name>mapred.child.java.opts</name>
<value>-Xmx1024m</value>
</property>虚拟内存限制
- 在yarn-site.xml中添加如下配置
1
2
3
4<property>
<name>yarn.nodemanager.vmem-check-enabled</name>
<value>false</value>
</property>