Hive-4.hive 的 性能调优

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 jobTableScan以这个表作为输入,然后产生一个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,进行获取两个表的笛卡尔积,一般查询语句:

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

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

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

 

8. 调整mapperreducer个数

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

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

 

9. JVM重用

Hadoop通常是使用派生JVM来执行mapreduce任务的。这时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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于Hive on MapReduce的性能调优,可以采取以下几个方面的优化策略: 1. 数据倾斜处理:在数据倾斜的情况下,可以使用Hive提供的一些技术来解决,如使用倾斜连接优化(Skew Join Optimization)或者使用动态分区(Dynamic Partitioning)来解决数据倾斜问题。 2. 数据压缩:Hive支持对数据进行压缩存储,可以通过设置适当的压缩算法和参数来减少磁盘IO,提高作业执行速度。 3. 数据分区和分桶:合理使用数据分区(Partition)和分桶(Bucketing)机制,可以减少数据扫描量,提高查询效率。 4. 动态分区:对于动态增长的表,可以使用动态分区(Dynamic Partitioning)来避免全表扫描,提高查询性能。 5. 调整MapReduce参数:根据作业的特性和集群的资源情况,可以调整MapReduce相关参数,如mapreduce.tasktracker.map.tasks.maximum、mapreduce.tasktracker.reduce.tasks.maximum等来优化作业的执行效率。 6. 使用索引:Hive支持创建索引,在查询频繁的列上创建索引可以加速查询操作。 7. 数据格式选择:选择合适的数据格式,如ORC、Parquet等,这些格式在存储和查询方面有较好的性能表现。 8. 并行度调整:根据集群资源和作业特性,调整任务的并行度,如mapred.map.tasks、mapred.reduce.tasks等,以充分利用集群资源。 9. 数据倾斜检测:使用Hive提供的一些工具,如Hive Explain、Hive Profile等来检测和分析作业中的数据倾斜问题,并针对性地进行优化。 总之,通过合理使用数据倾斜处理、数据压缩、数据分区和分桶、调整MapReduce参数等优化策略,可以提高Hive on MapReduce的性能和执行效率。根据具体的业务需求和数据特点,选择合适的优化策略,并结合实际情况进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾文教编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值