1. 使用EXPLAIN

通过EXPLAIN功能,可以帮助我们了解如何将查询转化成MapReduce任务的。

 

1、使用explain查看hive如何将查询转化成MapReduce任务的
1.1 创建表
create table onecol(number int)
1.2 初始化数据并加载
[hadoop@mycluster ~]$ vi onecol
1
2
3
4
5

hive (default)> load data local inpath '/home/hadoop/onecol' overwrite into table onecol;

1.3 通过explain查看
hive (default)> explain select sum(number) from onecol;
OK
Explain
STAGE DEPENDENCIES:
Stage-1 is a root stage
Stage-0 is a root stage

STAGE PLANS:
Stage: Stage-1
Map Reduce
Map Operator Tree:
TableScan
alias: onecol
Statistics: Num rows: 2 Data size: 10 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: number (type: int)
outputColumnNames: number
Statistics: Num rows: 2 Data size: 10 Basic stats: COMPLETE Column stats: NONE
Group By Operator
aggregations: sum(number)
mode: hash
outputColumnNames: _col0
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE
Reduce Output Operator
sort order:
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE
value expressions: _col0 (type: bigint)
Reduce Operator Tree:
Group By Operator
aggregations: sum(VALUE._col0)
mode: mergepartial
outputColumnNames: _col0
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: _col0 (type: bigint)
outputColumnNames: _col0
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE
File Output Operator
compressed: false
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE
table:
input format: org.apache.hadoop.mapred.TextInputFormat
output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe

Stage: Stage-0
Fetch Operator
limit: -1

 

 

 

 

首先,会打印抽象语法数。

一个Hive任务会包含有一个或者多个stage(阶段),不同的stage间会存在着依赖关系。一个stage可以是一个MapReduce,也可以是一个抽象阶段。默认情况下,Hive会一次只执行一个stage(阶段)。

STAGE PLAN部分比较复杂。Stage-1包含了这个job大部分的处理过程,而且触发了一个MapReduce job。TableScan以这个表作为输入,然后产生一个nunber的输出。

 

由于这个job没有LIMIT语句,因此stage-0阶段是一个没有任何操作的阶段。

 

2. EXPLAIN EXTENDED 

使用EXPLAIN EXTENDED 语句可以产生更多的输出信息。可以比较Reduce Operator Tree。

hive (default)> explain extended select sum(number) from onecol;
OK
Explain
ABSTRACT SYNTAX TREE:

TOK_QUERY
TOK_FROM
TOK_TABREF
TOK_TABNAME
onecol
TOK_INSERT
TOK_DESTINATION
TOK_DIR
TOK_TMP_FILE
TOK_SELECT
TOK_SELEXPR
TOK_FUNCTION
sum
TOK_TABLE_OR_COL
number


STAGE DEPENDENCIES:
Stage-1 is a root stage
Stage-0 is a root stage

STAGE PLANS:
Stage: Stage-1
Map Reduce
Map Operator Tree:
TableScan
alias: onecol
Statistics: Num rows: 2 Data size: 10 Basic stats: COMPLETE Column stats: NONE
GatherStats: false
Select Operator
expressions: number (type: int)
outputColumnNames: number
Statistics: Num rows: 2 Data size: 10 Basic stats: COMPLETE Column stats: NONE
Group By Operator
aggregations: sum(number)
mode: hash
outputColumnNames: _col0
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE
Reduce Output Operator
sort order:
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE
tag: -1
value expressions: _col0 (type: bigint)
Path -> Alias:
hdfs://mycluster:9000/user/hive/warehouse/onecol [onecol]
Path -> Partition:
hdfs://mycluster:9000/user/hive/warehouse/onecol
Partition
base file name: onecol
input format: org.apache.hadoop.mapred.TextInputFormat
output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
properties:
COLUMN_STATS_ACCURATE true
bucket_count -1
columns number
columns.comments
columns.types int
file.inputformat org.apache.hadoop.mapred.TextInputFormat
file.outputformat org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
location hdfs://mycluster:9000/user/hive/warehouse/onecol
name default.onecol
numFiles 1
numRows 0
rawDataSize 0
serialization.ddl struct onecol { i32 number}
serialization.format 1
serialization.lib org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
totalSize 10
transient_lastDdlTime 1452234517
serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe

input format: org.apache.hadoop.mapred.TextInputFormat
output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
properties:
COLUMN_STATS_ACCURATE true
bucket_count -1
columns number
columns.comments
columns.types int
file.inputformat org.apache.hadoop.mapred.TextInputFormat
file.outputformat org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
location hdfs://mycluster:9000/user/hive/warehouse/onecol
name default.onecol
numFiles 1
numRows 0
rawDataSize 0
serialization.ddl struct onecol { i32 number}
serialization.format 1
serialization.lib org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
totalSize 10
transient_lastDdlTime 1452234517
serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
name: default.onecol
name: default.onecol
Truncated Path -> Alias:
/onecol [onecol]
Needs Tagging: false
Reduce Operator Tree:
Group By Operator
aggregations: sum(VALUE._col0)
mode: mergepartial
outputColumnNames: _col0
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: _col0 (type: bigint)
outputColumnNames: _col0
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE
File Output Operator
compressed: false
GlobalTableId: 0
directory: hdfs://mycluster:9000/tmp/hive-hadoop/hive_2016-01-07_22-52-10_167_7966169081292339689-1/-ext-10001
NumFilesPerFileSink: 1
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: NONE
Stats Publishing Key Prefix: hdfs://mycluster:9000/tmp/hive-hadoop/hive_2016-01-07_22-52-10_167_7966169081292339689-1/-ext-10001/
table:
input format: org.apache.hadoop.mapred.TextInputFormat
output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
properties:
columns _col0
columns.types bigint
escape.delim \
hive.serialization.extend.nesting.levels true
serialization.format 1
serialization.lib org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
TotalFiles: 1
GatherStats: false
MultiFileSpray: false

Stage: Stage-0
Fetch Operator
limit: -1

 

3. LIMIT限制调整

一般情况下,LIMIT语句需要执行整个查询局域,然后在返回部分结果的。因此这种情况通常是浪费的,所以应该进可能地避免出现这种情况。Hive有一个配置属性可以开启,当使用LIMIT语句时,其可以对数据源进行抽样。

4. JOIN优化

对JOIN操作优化,一般讲最大的表放在JOIN的最右边或者直接使用/*streamtable(table_name)*/语句指出。

如果所有表中有一个表足够小,时可以完成载入内存中,那么这是Hive可以执行一个map-site join,这样可以减少reduce过程,有时可以减少map task任务。

5. 本地模式

对于大多数情况,Hive可以通过本地模式在单台机器上处理所有任务。对于小数据,执行时间可以明显被缩短。通过set hive.exec.mode.local.auto=true设置本地模式。

6. 并行模式

Hive会将一个查询转化成一个或者多个阶段。这样的阶段可以是MapReduce阶段、抽样阶段、合并阶段、limit阶段。默认情况下,Hive一次只会执行一个阶段,由于job包含多个阶段,而这些阶段并非完全互相依赖,即:这些阶段可以并行执行,可以缩短整个job的执行时间。设置参数:set hive.exec.parallel=true,或者通过配置文件来完成。

7. 严格模式

Hive提供一个严格模式,可以防止用户执行那些可能产生意想不到的影响查询,通过设置

Hive.mapred.modestrict来完成

7.1 分区表限制

Where语句的条件字段如果非分区字段,则要求必须通过limit限制来执行。

7.2 ORDER BY语句

由于order by为了执行排序构成会将所有的结果数据分发到同一个reducer中进行处理,强制要求用户增加LIMIT语句可以防止reducer额外执行一段时间。

7.3 限制笛卡尔乘积

对于表A,B ,进行获取两个表的笛卡尔积,一般查询语句:

select * from A join B where A.id=B.id

对于关系型数据库通过where条件可以转为on语句,但是Hive并不会执行这种优化。因此,如果表足够大,那么这个查询就会出现不可控的情况。一般采用下面的方法来完成:

select * from A join B on a.id=b.id

 

8. 调整mapper和reducer个数

默认情况下,Hive是按照输入的数据量大小来确认reducer个数的。其中属性:

Hive.exec.reducers.per.reducer默认为1GB。Hive默认的reducer个数应该是3,可以通过属性mapred.reduce.tasks的数值为不同的数值来确认,为控制资源的使用情况一般需要设置reducer任务的最大的数值,通过hive.exec.reducers.max可以阻止某个查询下消耗太多的reducer资源。一般可以在hive-site.xml来设置。

 

9. JVM重用

Hadoop通常是使用派生JVM来执行map和reduce任务的。这时JVM的启动过程可能会造成相当大的开销,尤其是执行的job包含偶成百上千的task任务的情况。JVM重用可以使得JVM示例在同一个job中时候使用N此。通过参数mapred.job.reuse.jvm.num.tasks来设置。

 

10. 推测执行

Hadoop推测执行可以触发执行一些重复的任务,尽管因对重复的数据进行计算而导致消耗更多的计算资源,不过这个功能的目标是通过加快获取单个task的结果以侦测执行慢的TaskTracker加入到没名单的方式来提高整体的任务执行效率。

 

Hadoop的推测执行功能由2个配置控制着,通过mapred-site.xml中配置

mapred.map.tasks.speculative.execution=true

mapred.reduce.tasks.speculative.execution=true