1、        Hive索引概述

Hive的索引目的是提高Hive表指定列的查询速度。

没有索引时。类似'WHERE tab1.col1 = 10' 的查询。Hive会载入整张表或分区。然后处理全部的rows,可是假设在字段col1上面存在索引时。那么仅仅会载入和处理文件的一部分。

与其它传统数据库一样。添加索引在提升查询速度时。会消耗额外资源去创建索引和须要很多其它的磁盘空间存储索引。

Hive 0.7.0版本号中,添加了索引。Hive 0.8.0版本号中添加了bitmap索引。

2、        索引相关的配置參数

hive.index.compact.file.ignore.hdfs

Default Value: false

Added In: Hive 0.7.0 withHIVE-1889

在索引文件里存储的hdfs地址将在执行时被忽略,假设开启的话;假设数据被迁移。那么索引文件依旧可用,默认是false

hive.optimize.index.filter

Default Value: false

Added In: Hive 0.8.0 withHIVE-1644

是否自己主动使用索引, 默认是false

hive.optimize.index.filter.compact.minsize

Default Value: 5368709120

Added In: Hive 0.8.0 withHIVE-1644

压缩索引自己主动应用的最小输入大小

hive.optimize.index.filter.compact.maxsize

Default Value: -1

Added In: Hive 0.8.0 withHIVE-1644

压缩索引自己主动应用的最大输入大小,负值代表正无穷

hive.index.compact.query.max.size

Default Value: 10737418240

Added In: Hive 0.8.0 withHIVE-2096

一个使用压缩索引做的查询能取到的最大数据量。默认是10737418240 个byte;负值代表无穷大;

hive.index.compact.query.max.entries

Default Value: 10000000

Added In: Hive 0.8.0 withHIVE-2096

使用压缩索引查询时能读到的最大索引项数,默认是10000000;负值代表无穷大;

hive.exec.concatenate.check.index

Default Value: true

Added In: Hive 0.8.0 withHIVE-2125

假设设置为true,那么在做ALTER TABLE tbl_name CONCATENATE on a table/partition(有索引) 操作时,抛出错误;能够帮助用户避免index的删除和重建;

hive.optimize.index.groupby

Default Value: false

Added In: Hive 0.8.1 withHIVE-1694

hive.index.compact.binary.search

Default Value: true

Added In: Hive 0.8.1with HIVE-2535

在索引表中是否开启二分搜索进行索引项查询,默认是true。

3、        索引演示样例

注意:在Hive 0.12.0以及之前版本号中,索引名称在create index和drop index语句中是大写和小写敏感的。然而,alter index 须要一个小写的索引名字。

此bug在Hive 0.13.0解决,此版本号開始使索引名字大写和小写不敏感。

对于Hive 0.13.0之前的版本号,最好使用小写的索引名字。

以下介绍索引的常见使用方法:

A、       Create/build,show和drop index

create index table01_index ontable table01(column2) as 'COMPACT' with deferred rebuild;

show index on table01;

drop index table01_index ontable01;

B、       Create then build。show formatted和drop index

create index table02_index ontable table02(column3) as 'compact' with deferred rebuild;

alter index table02_index ontable02 rebuild;

show formatted index ontable02;

drop index table02_index ontable02;

C、       创建bitmap索引,build,show 和drop

createindex table03_index on table table03 (column4) as
'bitmap' with deferred rebuild;

alter index table03_index ontable03 rebuild;

show formatted index ontable03;

drop index table03_index on table03;

D、       在一张新表上创建索引

createindex table04_index on table table04 (column5)
as 'compact'
with deferred rebuild in tabletable04_index_table;

E、        创建索引,存储格式为RCFile

create index table05_index ontable table05 (column6) as 'compact'
with deferred rebuildstored as rcfile;

F、        创建索引。存储格式为TextFile

create index table06_index ontable table06 (column7) as 'compact'
with deferredrebuild row format delimited fields terminated by '\t' stored as textfile;

G、       创建带有索引属性的索引

create index table07_index ontable table07 (column8) as 'compact'
with deferred rebuild idxproperties("prop1"="value1", "prop2"="value2");

H、       创建带有表属性的索引

create index table08_index ontable table08 (column9) as 'compact'
withdeferred rebuild tblproperties("prop3"="value3", "prop4"="value4");

I、        假设索引存在,则删除

drop index if exists table09_indexon table09;

J、        在分区上重建索引

alter index table10_index on table10partition (columnx='valueq', columny='valuer') rebuild;

4、        索引測试

(1)  查询表中行数

hive (hive)> select count(1)from userbook;

4409365

(2)  表中未创建索引前查询

hive (hive)> select * fromuserbook where book_id = '15999998838';

Query ID =hadoop_20150627165551_595da79a-0e27-453b-9142-7734912934c4

Total jobs = 1

Launching Job 1 out of 1

Number of reduce tasks is setto 0 since there's no reduce operator

Starting Job =job_1435392961740_0012, Tracking URL =http://gpmaster:8088/proxy/application_1435392961740_0012/

Kill Command =/home/hadoop/hadoop-2.6.0/bin/hadoop job -kill job_1435392961740_0012

Hadoop job information forStage-1: number of mappers: 2; number of reducers: 0

2015-06-27 16:56:04,666 Stage-1map = 0%,  reduce = 0%

2015-06-27 16:56:28,974 Stage-1map = 50%,  reduce = 0%, Cumulative CPU4.36 sec

2015-06-27 16:56:31,123 Stage-1map = 78%,  reduce = 0%, Cumulative CPU6.21 sec

2015-06-27 16:56:34,698 Stage-1map = 100%,  reduce = 0%, Cumulative CPU7.37 sec

MapReduce Total cumulative CPUtime: 7 seconds 370 msec

Ended Job =job_1435392961740_0012

MapReduce Jobs Launched:

Stage-Stage-1: Map: 2   Cumulative CPU: 7.37 sec   HDFS Read: 348355875 HDFS Write: 76 SUCCESS

Total MapReduce CPU Time Spent:7 seconds 370 msec

OK

userbook.book_id    userbook.book_name    userbook.author      userbook.public_date     userbook.address

15999998838     uviWfFJ KwCrDOA    2009-12-27  3b74416d-eb69-48e2-9d0d-09275064691b

Time taken: 45.678 seconds, Fetched: 1 row(s)

(3)  创建索引

hive (hive)> create indexuserbook_bookid_idx on table userbook(book_id) as 'COMPACT' WITH DEFERREDREBUILD;

(4)  创建索引后再运行查询

hive (hive)> select * fromuserbook where book_id = '15999998838';

Query ID =hadoop_20150627170019_5bb5514a-4c8e-4c47-9347-ed0657e1f2ff

Total jobs = 1

Launching Job 1 out of 1

Number of reduce tasks is setto 0 since there's no reduce operator

Starting Job =job_1435392961740_0013, Tracking URL = http://gpmaster:8088/proxy/application_1435392961740_0013/

Kill Command =/home/hadoop/hadoop-2.6.0/bin/hadoop job -kill job_1435392961740_0013

Hadoop job information forStage-1: number of mappers: 2; number of reducers: 0

2015-06-27 17:00:30,429 Stage-1map = 0%,  reduce = 0%

2015-06-27 17:00:54,003 Stage-1map = 50%,  reduce = 0%, Cumulative CPU7.43 sec

2015-06-27 17:00:56,181 Stage-1map = 78%,  reduce = 0%, Cumulative CPU9.66 sec

2015-06-27 17:00:58,417 Stage-1map = 100%,  reduce = 0%, Cumulative CPU10.83 sec

MapReduce Total cumulative CPUtime: 10 seconds 830 msec

Ended Job =job_1435392961740_0013

MapReduce Jobs Launched:

Stage-Stage-1: Map: 2   Cumulative CPU: 10.83 sec   HDFS Read: 348356271 HDFS Write: 76 SUCCESS

Total MapReduce CPU Time Spent:10 seconds 830 msec

OK

userbook.book_id    userbook.book_name    userbook.author      userbook.public_date     userbook.address

15999998838     uviWfFJ KwCrDOA    2009-12-27  3b74416d-eb69-48e2-9d0d-09275064691b

Time taken: 40.549 seconds, Fetched: 1 row(s)

能够看到创建索引后,速度还是稍快一点的。

事实上对于这样的简单的查询,通过我们的设置,能够不用启动Map/Reduce的,而是启动Fetch task,直接从HDFS文件里filter过滤出须要的数据。须要设置例如以下參数:

set hive.fetch.task.conversion=more;

hive (hive)> select * fromuserbook where book_id = '15999998838';

OK

userbook.book_id    userbook.book_name    userbook.author      userbook.public_date     userbook.address

15999998838     uviWfFJ KwCrDOA    2009-12-27  3b74416d-eb69-48e2-9d0d-09275064691b

Time taken: 0.093 seconds,Fetched: 1 row(s)

能够看到速度更快了。毕竟省略掉了开启MR任务,运行效率提高不少。

參考:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Indexing

Hive索引的更多相关文章

  1. HIVE—索引、分区和分桶的区别

    一.索引 简介 Hive支持索引,但是Hive的索引与关系型数据库中的索引并不相同,比如,Hive不支持主键或者外键. Hive索引可以建立在表中的某些列上,以提升一些操作的效率,例如减少MapRed ...

  2. Hive索引功能测试

    作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 从Hive的官方wiki来看,Hive0.7以后增加了一个对表建立index的功能,想试下性能是 ...

  3. hive索引表

    create table index_tmp(id int,name string,dt string) row format delimited fields terminated by ',' s ...

  4. hive 索引

    hive 有限的支持索引,不支持主键外键,可以对表添加索引,也可以为某个分区添加索引.维护索引也要额外的存储空间和计算资源. 创建索引需要指定索引处理器 如 as 'org.apache.hadoop ...

  5. 【Hive学习之六】Hive Lateral View &视图&索引

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 apache-hive-3.1.1 ...

  6. Hadoop Hive概念学习系列之hive里的索引(十三)

    Hive支持索引,但是Hive的索引与关系型数据库中的索引并不相同,比如,Hive不支持主键或者外键. Hive索引可以建立在表中的某些列上,以提升一些操作的效率,例如减少MapReduce任务中需要 ...

  7. Hadoop Hive概念学习系列之hive的索引及案例(八)

    hive里的索引是什么? 索引是标准的数据库技术,hive 0.7版本之后支持索引.Hive提供有限的索引功能,这不像传统的关系型数据库那样有“键(key)”的概念,用户可以在某些列上创建索引来加速某 ...

  8. Hive 学习之路(六)—— Hive 视图和索引

    一.视图 1.1 简介 Hive 中的视图和RDBMS中视图的概念一致,都是一组数据的逻辑表示,本质上就是一条SELECT语句的结果集.视图是纯粹的逻辑对象,没有关联的存储(Hive 3.0.0引入的 ...

  9. Hive 系列(六)—— Hive 视图和索引

    一.视图 1.1 简介 Hive 中的视图和 RDBMS 中视图的概念一致,都是一组数据的逻辑表示,本质上就是一条 SELECT 语句的结果集.视图是纯粹的逻辑对象,没有关联的存储 (Hive 3.0 ...

随机推荐

  1. M​i​c​r​o​s​o​f​t​ ​w​e​b​ ​a​p​p​l​i​c​a​t​i​o​n​ ​s​t​r​e​s​s​ ​t​o​o​l​ 测试

    一.准备工作 为了测试数据的准备性,首先需要删除缓存和Cookies等临时文件.启动IE后打开“工具”菜单下的“Internet”选项命令,在打开的“Internet选项”窗口的“常规”选项卡中,单击 ...

  2. IC卡存储器介绍

    自从80年代中期出现IC电话卡后,基本已取代了原来流行的电话磁卡,磁卡存在存在严重的安全问题,已逐步淘汰.即使IC电话卡,也不能算很安全,卡内所有数据只要有简单的读写装置并按时序操作都能读取,事实上电 ...

  3. 内核级HOOK的几种实现与应用

    实现内核级 HOOK 对于拦截.分析.跟踪系统内核起着致关重要的作用.实现的方法不同意味着应用侧重点的不同.如想要拦截 NATIVE API 那么可能常用的就是 HOOK SERVICE TABLE  ...

  4. 使用百度语音识别REST API,做全平台语音识别

    百度语音开发介绍文档: http://yuyin.baidu.com/docs/asr# 使用语音识别,需要在百度申请一个应用,然后拿到API Key和Secret Key,然后才可以使用语音识别 p ...

  5. Qt信号槽机制的实现(面试的感悟,猜测每一个类保存的一个信号和槽的二维表,实际使用函数指针 元对象 还有类型安全的检查设定等等)

    因为面试时问了我这道题,导致我想去了解信号槽到底是如何实现的,于是贴着顺序看了下源码,大致了解了整个框架.网上关于信号槽的文章也很多,但是大部分都是将如何应用的,这里我就写一下我所理解的如何实现吧, ...

  6. 运用JavaScript构建你的第一个Metro式应用程序(onWindows 8)(二)

    原文 http://blog.csdn.net/zhangxin09/article/details/6793330 先前的学习中,我们已经了解了 Metro式的 JavaScript 应用程序大致如 ...

  7. MyEclipse修改

    MyEclipse设置编码方式 http://www.cnblogs.com/susuyu/archive/2012/06/27/2566062.html Eclipse添加Spket插件实现ExtJ ...

  8. hdu 2102 A计划(双层BFS)(具体解释)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php ...

  9. php 设置字符集为utf-8

    header("Content-Type:text/html;charset=utf-8");

  10. android插件化-apkplugdemo源代码阅读指南-10

    阅读本节内容前可先了解 apkplug基础教程 本教程是基于apkplug V1.6.8 版本号编写  最新开发方式以官网为准 可下载最新的apkplugdemo源代码http://git.oschi ...