set hive.fetch.task.conversion=more(默认) 1
Fetch 抓取是指,Hive 中对某些状况的查询能够没必要使用 MapReduce 计算。
该属性设置为 more 之后,在全局查找、字段查找、limit 查找等都不走 MapReduce。 设置为none后全部类型的查找语句都要走MapReduce;node
set hive.exec.mode.local.auto=true(开启本地模式) 1
Hive 能够经过本地模式在单台机器上 处理全部的任务。对于小数据集,执行时间能够明显被缩短
1.开启本地模式后须要设置local mr的最大输入数据量,当数据量小于这个值时采用local mr的方式sql
set hive.exec.mode.local.auto.inputbytes.max=134217728(默认) 1
2.开启本地模式后须要设置local mr的最大输入文件个数,当数据量小于这个值时采用local mr的方式数据库
set hive.exec.mode.local.auto.input.files.max=4(默认) 1
注: 新版的 hive 已经对小表 JOIN 大表和大表 JOIN 小表进行了优化。小表放在左边和右边已经没有明显区别apache
当一个表内有许多空值时会致使MapReduce过程当中,空成为一个key值,对应的会有大量的value值, 而一个key的value会一块儿到达reduce形成内存不足;因此要想办法过滤这些空值.
1.经过查询全部不为空的结果并发
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; 12
2.查询出空值并给其赋上随机数,避免了key值为空负载均衡
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; 123
注:此方法能够解决数据倾斜的问题ide
若是不指定 MapJoin 或者不符合 MapJoin 的条件,那么 Hive 解析器会将 Join 操做转换成 Common Join,即:在 Reduce 阶段完成 join。容易发生数据倾斜。能够用 MapJoin 把小 表所有加载到内存在 map 端进行 join,避免 reducer 处理。oop
设置MapJoin测试
set hive.auto.convert.join = true(默认) 1
大表小表的阀门值设置(默认25M如下认为是小表):fetch
set hive.mapjoin.smalltable.filesize=25000000; 1
默认状况下,Map 阶段同一 Key 数据分发给一个 reduce,当一个 key 数据过大时就倾斜了并非全部聚合都在reduce端完成,不少聚合操做均可以如今Map端进行部分聚合,最后在Reduce段获得结果
开启Map端聚合参数设置
是否在Map段进行聚合,默认为true
hive.map.aggr = true 1
在Map端进行聚合操做的条目数
hive.groupby.mapaggr.checkinterval = 100000 1
有数据倾斜的时候进行负载均衡(默认是false)
hive.groupby.skewindata = true 1
注: 当选项设定为 true,生成的查询计划会有两个 MR Job。第一个 MR Job 中,Map 的输出结果会随机分布到 Reduce 中,每一个 Reduce 作部分聚合操做,并输出结果,这样处理的结果是相同的 Group By Key 有可能被分发到不一样的 Reduce 中,从而达到负载均衡的目的;第二个 MR Job 再根据预处理的数据结果按照 Group By Key 分布到 Reduce 中(这个过程能够 保证相同的 Group By Key 被分布到同一个 Reduce 中),最后完成最终的聚合操做。
Count Distinct是使用了一个mapreduce ,当数据较少时无影响当数据较大时 只使用一个MapReduce将很难完成job。这是须要用到分组 Group BY 会使用2个MapReduce完成由于设置了 set mapreduce.job.reduces = 5; 因此第一个MapReduce的过程是经过一个map和5个reduce来完成这样减轻了reduce的负载, 虽然会多用一个 Job 来完成,但在数据量大的状况下,这个绝对是值得的。
实例:
1.测试先关联两张表,再用 where 条件过滤
hive (default)> select o.id from bigtable bjoin ori o on o.id = b.idwhere o.id <= 10; 1
2.经过子查询后,再关联表
hive (default)> select b.id from bigtable b join (select id from ori where id <= 10 ) o on b.id = o.id; 1
关系型数据库中,对分区表 Insert 数据时候,数据库自动会根据分区字段的值,将数据插入到相应的分区中,Hive 中也提供了相似的机制,即动态分区(Dynamic Partition),只不过,使用 Hive 的动态分区,须要进行相应的配置。
首先要设置的属性
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; 123456
模拟动态分区
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; 12
设置切片值
set mapreduce.input.fileinputformat.split.maxsize=??? 1
在 map 执行前合并小文件,减小 map 数:CombineHiveInputFormat 具备对小文件进行
合并的功能(系统默认的格式)。HiveInputFormat 没有对小文件合并功能。
set hive.input.format= org.apache.hadoop.hive.ql.io.CombineHiveInputFormat; 1
set mapreduce.job.maps =??? 1
1.调整reduce的个数方法一
每一个Reduce处理的数据默认是256MB
hive.exec.reducers.bytes.per.reducer=256000000 1
每一个任务最大的reduce数,默认为1009
hive.exec.reducers.max=1009 1
计算reduce数的公式
N=min(参数2,总输入数据量/参数1) 1
2.调整reduce个数的方法二
set mapreduce.job.reduces=??? 1
3.reduce个数不是越多越好
经过设置参数 hive.exec.parallel 值为 true,就能够开启并发执行。不过,在共享集群中,须要注意下,若是 job 中并行阶段增多,那么集群利用率就会增长。
set hive.exec.parallel=true; //打开任务并行执行 set hive.exec.parallel.thread.number=16; //同一个 sql 容许最大并行度,默认为 8。