Hive优化

一、 Fetch抓取

Fetch抓取是指,Hive中对某些情况的查询可以不必使用MapReduce计算。
例如:SELECT * FROM employees;在这种情况下,Hive可以简单地读取employee对应的存储目录下的文件,然后输出查询结果到控制台。
在hive-default.xml.template文件中hive.fetch.task.conversion默认是more,老版本hive默认是minimal,(注意:在hive执行set hive.cli.print.current.db=true;命令,使得你当前的数据库显示。)
该属性修改为more以后,在全局查找、字段查找、limit查找等都不走mapreduce。

<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 haveany aggregations or distincts (which incurs RS), lateral views and joins.0. none : disable hive.fetch.task.conversion1. minimal : SELECT STAR, FILTER on partition columns, LIMIT only2. more  : SELECT, FILTER, LIMIT only (support TABLESAMPLE and virtual columns)</description></property>

案例实操:
1)把hive.fetch.task.conversion设置成none,然后执行查询语句,都会执行mapreduce程序。

hive > set hive.fetch.task.conversion=none;
hive > select * from emp;
hive > select ename from emp;
hive > select ename from emp limit 3;

2)把hive.fetch.task.conversion设置成more,然后执行查询语句,如下查询方式都不会执行mapreduce程序。

hive > set hive.fetch.task.conversion=more;
hive > select * from emp;
hive > select ename from emp;
hive > select ename from emp limit 3;

二、 本地模式

大多数的Hadoop Job是需要Hadoop提供的完整的可扩展性来处理大数据集的。
不过,有时Hive的输入数据量是非常小的。在这种情况下,为查询触发执行任务消耗的时间可能会比实际job的执行时间要多的多。
对于大多数这种情况,Hive可以通过本地模式在单台机器上处理所有的任务。对于小数据集,执行时间可以明显被缩短。
用户可以通过设置hive.exec.mode.local.auto的值为true,来让Hive在适当的时候自动启动这个优化。

set hive.exec.mode.local.auto=true;  //开启本地mr

//设置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;

案例实操:
运行时:本地模式会出现

Job running in-process (local Hadoop)

1)开启本地模式,并执行查询语句

hive (default)> set hive.exec.mode.local.auto=true; 
hive (default)> select * from emp cluster by deptno;
Time taken: 1.328 seconds, Fetched: 14 row(s)

2)关闭本地模式,并执行查询语句

hive (default)> set hive.exec.mode.local.auto=false; 
hive (default)> select * from emp cluster by deptno;
Time taken: 20.09 seconds, Fetched: 14 row(s)

三、 表的优化

1、小表、大表Join
将key相对分散,并且数据量小的表放在join的左边,这样可以有效减少内存溢出错误发生的几率;再进一步,
可以使用map join让小的维度表(1000条以下的记录条数)先进内存。在map端完成reduce。
实际测试发现:新版的hive已经对小表JOIN大表和大表JOIN小表进行了优化。小表放在左边和右边已经没有明显区别。
案例实操
1).需求
测试大表JOIN小表和小表JOIN大表的效率
2).建大表、小表和JOIN后表的语句
// 创建大表

create table bigtable(id bigint, times bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';

// 创建小表

create table smalltable(id bigint, times bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';

// 创建join后表的语句

create table jointable(id bigint, times bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';

3).分别向大表和小表中导入数据

hive (default)> load data local inpath '/home/hduser/hivedata/bigtable' into table bigtable;
hive (default)>load data local inpath '/home/hduser/hivedata/smalltable' into table smalltable;

4).关闭mapjoin功能(默认是打开的)

set hive.auto.convert.join = false;

5).执行小表JOIN大表语句

insert overwrite table jointable
select b.id, b.times, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from smalltable s
left join bigtable  b
on b.id = s.id;Time taken: 35.921 seconds
No rows affected (44.456 seconds)

6).执行大表JOIN小表语句

insert overwrite table jointable
select b.id, b.times, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from bigtable  b
left join smalltable  s
on s.id = b.id;Time taken: 34.196 seconds
No rows affected (26.287 seconds)

2、 大表Join大表

1.空KEY过滤
有时join超时是因为某些key对应的数据太多,而相同key对应的数据都会发送到相同的reducer上,从而导致内存不够。
此时我们应该仔细分析这些异常的key,很多情况下,这些key对应的数据是异常数据,我们需要在SQL语句中进行过滤。
例如key对应的字段为空,操作如下:
案例实操
(1)配置历史服务器
配置mapred-site.xml

    <property><name>mapreduce.jobhistory.address</name><value>chun1:10020</value></property><property><name>mapreduce.jobhistory.webapp.address</name><value>chun1:19888</value></property>

启动历史服务器

sbin/mr-jobhistory-daemon.sh start historyserver

查看jobhistory

192.168.228.138:19888/jobhistory

(2)创建原始数据表、空id表、合并后数据表
// 创建原始表

create table ori(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';

// 创建空id表

create table nullidtable(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';

// 创建join后表的语句

create table jointable(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';

(3)分别加载原始数据和空id数据到对应表中

hive (default)> load data local inpath '/home/hduser/hivedata/ori' into table ori;
hive (default)> load data local inpath '/home/hduser/hivedata/nullid' into table nullidtable;

(4)测试不过滤空id

hive (default)> insert overwrite table jointable 
select n.* from nullidtable n left join ori o on n.id = o.id;
Time taken: 42.038 seconds
Time taken: 37.284 seconds

(5)测试过滤空id

hive (default)> insert overwrite table jointable 
select n.* from (select * from nullidtable where id is not null ) n  left join ori o on n.id = o.id;
Time taken: 31.725 seconds
Time taken: 28.876 seconds

2.空key转换
有时虽然某个key为空对应的数据很多,但是相应的数据不是异常数据,必须要包含在join的结果中,
此时我们可以表a中key为空的字段赋一个随机的值,使得数据随机均匀地分不到不同的reducer上。例如:
案例实操:
不随机分布空null值:
(1)设置5个reduce个数

set mapreduce.job.reduces = 5;

(2)JOIN两张表

insert overwrite table jointable
select n.* from nullidtable n left join ori b on n.id = b.id;

结果可以看出来,出现了数据倾斜,某些reducer的资源消耗远大于其他reducer。

空key转换
随机分布空null值
(1)设置5个reduce个数

set mapreduce.job.reduces = 5;

(2)JOIN两张表

insert overwrite table jointable
select n.* from nullidtable n full join ori o on 
case when n.id is null then concat('hive', rand()) else n.id end = o.id;

结果:可以看出来,消除了数据倾斜,负载均衡reducer的资源消耗

3 MapJoin
如果不指定MapJoin或者不符合MapJoin的条件,那么Hive解析器会将Join操作转换成Common Join,
即:在Reduce阶段完成join。容易发生数据倾斜。可以用MapJoin把小表全部加载到内存在map端进行join,避免reducer处理。
1.开启MapJoin参数设置
(1)设置自动选择Mapjoin

set hive.auto.convert.join = true; 默认为true

(2)大表小表的阈值设置(默认25M一下认为是小表):

set hive.mapjoin.smalltable.filesize=25000000;

2.
案例实操:
(1)开启Mapjoin功能

set hive.auto.convert.join = true; 默认为true

(2)执行小表JOIN大表语句

insert overwrite table jointable
select b.id, b.time, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from smalltable s
join bigtable  b
on s.id = b.id;
Time taken: 24.594 seconds

(3)执行大表JOIN小表语句

insert overwrite table jointable
select b.id, b.time, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from bigtable  b
join smalltable  s
on s.id = b.id;
Time taken: 24.315 seconds
4 Group By

默认情况下,Map阶段同一Key数据分发给一个reduce,当一个key数据过大时就倾斜了。
并不是所有的聚合操作都需要在Reduce端完成,很多聚合操作都可以先在Map端进行部分聚合,
最后在Reduce端得出最终结果。
1.开启Map端聚合参数设置
(1)是否在Map端进行聚合,默认为True
hive.map.aggr = true

(2)在Map端进行聚合操作的条目数目

hive.groupby.mapaggr.checkinterval = 100000

(3)有数据倾斜的时候进行负载均衡(默认是false)

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中),最后完成最终的聚合操作。

5 Count(Distinct) 去重统计
数据量小的时候无所谓,数据量大的情况下,由于COUNT DISTINCT操作需要用一个Reduce Task来完成,
这一个Reduce需要处理的数据量太大,就会导致整个Job很难完成,一般COUNT DISTINCT使用先GROUP BY再COUNT的方式替换:
案例实操
1). 创建一张大表

hive (default)> create table bigtable(id bigint, time bigint, uid string, keyword
string, url_rank int, click_num int, click_url string) row format delimited
fields terminated by '\t';

2).加载数据

hive (default)> load data local inpath '/home/hduser/datas/bigtable' into tablebigtable;

3).设置5个reduce个数

set mapreduce.job.reduces = 5;

4).执行去重id查询

hive (default)> select count(distinct id) from bigtable;
Stage-Stage-1: Map: 1  Reduce: 1   Cumulative CPU: 7.12 sec   HDFS Read: 120741990 HDFS Write: 7 SUCCESS
Total MapReduce CPU Time Spent: 7 seconds 120 msec
OK
c0
100001
Time taken: 23.607 seconds, Fetched: 1 row(s)

5).采用GROUP by去重id

hive (default)> select count(id) from (select id from bigtable group by id) a;
Stage-Stage-1: Map: 1  Reduce: 5   Cumulative CPU: 17.53 sec   HDFS Read: 120752703 HDFS Write: 580 SUCCESS
Stage-Stage-2: Map: 1  Reduce: 1   Cumulative CPU: 4.29 sec   HDFS Read: 9409 HDFS Write: 7 SUCCESS
Total MapReduce CPU Time Spent: 21 seconds 820 msec

虽然会多用一个Job来完成,但在数据量大的情况下,这个绝对是值得的。

6 、笛卡尔积
尽量避免笛卡尔积,join的时候不加on条件,或者无效的on条件,Hive只能使用1个reducer来完成笛卡尔积。
7 、行列过滤
列处理:在SELECT中,只拿需要的列,如果有,尽量使用分区过滤,少用SELECT *。
行处理:在分区剪裁中,当使用外关联时,如果将副表的过滤条件写在Where后面,那么就会先全表关联,之后再过滤,比如:
案例实操:
1).测试先关联两张表,再用where条件过滤

hive (default)> select o.id from bigtable b
join ori o on o.id = b.id
where o.id <= 10;
Time taken: 34.406 seconds, Fetched: 100 row(s)

2).通过子查询后,再关联表

hive (default)> select b.id from bigtable b
join (select id from ori where id <= 10 ) o on b.id = o.id;
Time taken: 30.058 seconds, Fetched: 100 row(s)

8、 动态分区调整
(动态分区和静态分区有何不同?)
a、分区名
静态分区:手动指定分区名
动态分区:根据查询语句自动生成的分区名
b、数据
静态分区: 有可能存在某一个分区中一条数据都没有
动态分区:每一个分区至少有一条数据
c、性能
动态分区比静态分区的方式消耗性能,默认严格下动态分区至少有一个静态分区。
注意:insert 方式只存储表字段,不存储分区字段
分区表查询的时候,将分区字段看做普通字段进行查询就可以了

关系型数据库中,对分区表Insert数据时候,数据库自动会根据分区字段的值,将数据插入到相应的分区中,Hive中也提供了类似的机制,
即动态分区(Dynamic Partition),只不过,使用Hive的动态分区,需要进行相应的配置。
1.开启动态分区参数设置
(1)开启动态分区功能(默认true,开启)

hive.exec.dynamic.partition=true

(2)设置为非严格模式(动态分区的模式,默认strict,表示必须指定至少一个分区为静态分区,
nonstrict模式表示允许所有的分区字段都可以使用动态分区。)

hive.exec.dynamic.partition.mode=nonstrict

(3)在所有执行MR的节点上,最大一共可以创建多少个动态分区。

hive.exec.max.dynamic.partitions=1000

(4)在每个执行MR的节点上,最大可以创建多少个动态分区。该参数需要根据实际的数据来设定。
比如:源数据中包含了一年的数据,即day字段有365个值,那么该参数就需要设置成大于365,如果使用默认值100,则会报错。
hive.exec.max.dynamic.partitions.pernode=100

(5)整个MR Job中,最大可以创建多少个HDFS文件。

hive.exec.max.created.files=100000

(6)当有空分区生成时,是否抛出异常。一般不需要设置。

hive.error.on.empty.partition=false

2.案例实操
需求:将ori中的数据按照时间(如:20111230000008),插入到目标表ori_partitioned_target的相应分区中。
(1)创建分区表

create table ori_partitioned(id bigint, time bigint, uid string, keyword string,url_rank int, click_num int, click_url string) 
partitioned by (p_time bigint) 
row format delimited fields terminated by '\t';

(2)加载数据到分区表中

hive (default)> load data local inpath '/home/hduser/ds1' into tableori_partitioned partition(p_time='20111230000010') ;
hive (default)> load data local inpath '/home/hduser/ds2' into table ori_partitioned partition(p_time='20111230000011') ;

(3)创建目标分区表

create table ori_partitioned_target(id bigint, time bigint, uid string,keyword string, url_rank int, click_num int, click_url string) PARTITIONED BY (p_time STRING) row format delimited fields terminated by '\t';

(4)设置动态分区

set hive.exec.dynamic.partition = true;
set hive.exec.dynamic.partition.mode = nonstrict;
set hive.exec.max.dynamic.partitions = 1000;
set hive.exec.max.dynamic.partitions.pernode = 100;
set hive.exec.max.created.files = 100000;
set hive.error.on.empty.partition = false;hive (default)> insert overwrite table ori_partitioned_target partition (p_time) 
select id, time, uid, keyword, url_rank, click_num, click_url, p_time from ori_partitioned;

(5)查看目标分区表的分区情况

hive (default)> show partitions ori_partitioned_target;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/437672.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

COVID-19 肺炎疫情数据实时监控(python 爬虫 + pyecharts 数据可视化 + wordcloud 词云图)

文章目录【1x00】前言【2x00】思维导图【3x00】数据结构分析【4x00】主函数 main()【5x00】数据获取模块 data_get【5x01】初始化函数 init()【5x02】中国总数据 china_total_data()【5x03】全球总数据 global_total_data()【5x04】中国每日数据 china_daily_data()【5x05】境外…

【转】Postman系列二:Postman中get接口实战讲解(接口测试介绍,接口测试流程,头域操作)

一&#xff1a;接口测试介绍 接口测试&#xff1a;就是针对软件对外提供服务的接口输入输出进行测试&#xff0c;以及接口间相互逻辑的测试&#xff0c;验证接口功能和接口描述文档的一致性。 接口测试好处&#xff1a;接口测试通常能对系统测试的更为彻底&#xff0c;更高的保…

hive或mysql报错Too many connections

in acquiring locks: com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initializ e pool: Data source rejected establishment of connection, message from server: “Too many connections” 显示&#xff1a;Too many connections &#xff0c…

Python3 爬虫实战 — 前程无忧招聘信息爬取 + 数据可视化

爬取时间&#xff1a;2020-07-11&#xff08;2020年10月测试&#xff0c;增加了反爬&#xff0c;此代码已失效&#xff01;&#xff01;&#xff01;&#xff09;实现目标&#xff1a;根据用户输入的关键字爬取相关职位信息存入 MongoDB&#xff0c;读取数据进行可视化展示。涉…

【转】Postman系列三:Postman中post接口实战(上传文件、json请求)

一&#xff1a;接口测试过程中GET请求与POST请求的主要区别 从开发角度我们看get与post的主要区别是&#xff1a; 1.Get是用来从服务器上获得数据&#xff0c;而Post是用来向服务器上传递数据&#xff1b; 2.Get安全性比Post低&#xff1a;Get将表单中数据的按照keyvalue的形式…

Hadoop datanode正常启动,但是jps差不多datanode进程,而且Live nodes中却缺少节点

启动时可以看到启动成功&#xff0c;但是在chun2&#xff0c;jps的时候却没有了datanode进程&#xff0c;而且web端Live nodes也缺少了 百度搜索之后查到是因为hdfs.site.xml配置文件里dfs.data.dir配置的路径重复&#xff0c;就是多个节点存放data数据的目录路径相同了&#x…

【Python 必会技巧】使用 Python 追加写入 json 文件或更改 json 文件中的值

追加写入 json 文件 有一个 test.json 文件&#xff0c;包含内容如下&#xff1a; {"key_1": "value_1" }现需要追加写入 json 文件&#xff0c;向其中增加值&#xff0c;使其包含内容如下&#xff1a; {"key_1": "value_1","…

【转】Postman系列四:Postman接口请求设置环境变量和全局变量、测试沙箱和测试断言、测试集运行与导入数据文件

一&#xff1a;Postman中接口请求设置环境变量和全局变量 全局变量和环境变量可以通过Pre-request Script和Tests设置&#xff0c;会在下面测试沙箱和测试断言中讲到。 全局变量的设置&#xff1a;官网参考https://learning.getpostman.com/docs/postman/environments_and_glob…

Python 算法之递归与尾递归,斐波那契数列以及汉诺塔的实现

文章目录递归概念递归要素递归与迭代的区别示例一&#xff1a;阶乘示例二&#xff1a;斐波那契数列示例三&#xff1a;汉诺塔问题尾递归Python 中尾递归的解决方案递归概念 递归&#xff1a;程序调用自身的编程技巧称为递归&#xff08; recursion&#xff09;。用一种通俗的话…

【转】Postman系列五:Postman中电商网站cookie、token检验与参数传递实战

一&#xff1a;Postman中电商网站cookie实战 Postman接口请求使用cookie两种方式&#xff1a; 1.直接在header&#xff08;头域&#xff09;中添加cookie&#xff0c;适用于已知请求cookie头域的情况 2.使用Postman的cookie管理机制&#xff0c;即可以手动添加&#xff0c;同时…

Python 数据结构之栈的实现

文章目录栈的概念栈的特点栈的操作Python 实现栈栈的简单应用&#xff1a;括号匹配问题栈的简单应用&#xff1a;倒序输出一组元素栈的概念 栈&#xff08;stack&#xff09;又名堆栈&#xff0c;栈是一种线性数据结构&#xff0c;用先进后出或者是后进先出的方式存储数据&…

Scala学习

Scala学习&#xff08;1.在菜鸟驿站简单学习&#xff09; 由于学过java等语言&#xff0c;Scala简单的把语法多敲多练习就可以 新语言开始学习主要是语法的熟悉阶段&#xff0c;菜鸟教程里的内容全部完成一遍 object HelloWorld {def main(args: Array[String]): Unit {pri…

CSDN 2020 博客之星实时数据排名(Python 爬虫 + PyEcharts)

CSDN 2020 博客之星实时数据排名&#xff1a;csdn.itrhx.com CSDN 一年一度的博客之星评选开始了&#xff0c;官网地址&#xff1a;https://bss.csdn.net/m/topic/blog_star2020 &#xff0c;由于官网是按照随机编号排序的&#xff0c;没有按照票数多少排序&#xff0c;为了方便…

Scala进阶-函数练习

1&#xff09; map()函数 可以对整个集合进行操作&#xff0c;比如 创建一个Seq列表&#xff0c;然后用map对集合*2 val salaries Seq(2,3,4,5)val newsalaries salaries.map(_*2)2&#xff09; flatMap函数 faltMap函数是map一种扩展&#xff0c;faltMap中传入一个函数&a…

【转】注册Azure AD 应用程序

作者&#xff1a;陈希章 发表于2017年3月22日 在此前的文章中&#xff0c;我给大家介绍了分别用Graph 浏览器以及第三方工具&#xff08;POSTMAN&#xff09;快速体验Microsoft Graph的功能&#xff0c;其中有一个重要的环节就是&#xff0c;开发人员需要访问Microsoft Graph的…

Python + GitHub Actions 实现 CSDN 自动签到与抽奖(非 selenium 版本)

文章目录【1x00】技术栈【2x00】代码实现签到与抽奖【3x00】签到结果通知【03x01】Server 酱【03x02】企业微信【03x03】钉钉【4x00】自动签到【5x00】完整代码【6x00】如何使用【06x01】方法一&#xff1a;直接 Fork 代码&#xff08;推荐&#xff09;【06x01】方法二&#xf…

Spark安装配置

Scala基础语法学习的差不多了&#xff0c;先把spark安装上 首先官网下载解压安装后 进入到conf目录下修改文件名 修改spark-env.sh&#xff08;配置jdk路径&#xff09; export JAVA_HOME/usr/local/java/jdk1.8.0_221修改slaves&#xff08;添加子节点名&#xff09; chun…

【转】注册Azure AD 2.0 应用程序

作者&#xff1a;陈希章 发表于 2017年3月22日 上一篇 介绍了Microsoft Graph应用程序的一些概念&#xff0c;以及目前还比较普遍的Azure AD 1.0应用程序的注册方式。但正如我多次提到的那样&#xff0c;虽然目前功能还在不断完善&#xff0c;但Azure AD 2.0会逐渐成为主流&…

Python 采集 Facebook 评论插件、留言外挂程序

实现时间&#xff1a;2021-05-30实现难度&#xff1a;★★★☆☆☆实现目标&#xff1a;采集 Facebook 评论插件、留言外挂程序的所有评论。完整代码&#xff1a;https://github.com/TRHX/Python3-Spider-Practice/tree/master/CommentPlugin/facebook-comments其他爬虫实战代码…