Hive对表建立索引

1.首先创建表

create table user(

  id int,

  name string,

  address string

)

ROW FORMAT DELIMITED

FIELDS TERMINATED BY ','

STORED AS TEXTFILE;

2.创建索引

create index user_index on table user(id)

as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' 

with deferred rebuild 

idxproperties('creator' = 'Alex','create_at' = 'sometimes') 

in table user_index_table; -- 生成user_index_table一张额外的表,该表里面 包括索引字段,以及该值所对应的HDFS文件路径,和该值在文件中的偏移量。

alter index user_index on user rebuild;

这样就对user表加了索引了,索引字段为id。

3.删除索引

drop index [if exists] user_index  on user;

4.加载索引数据

alter index  user_index on user [partition dt] rebuild;

5.查询索引

show index on user;

6.使用索引的目的

在执行索引字段查询的时候,首先生成一个额外的MR Job,根据对索引列的过滤条件,从索引表中过滤出索引列的值对应的HDFS文件路径以及偏移量,输出带hdfs一个文件中,然后根据这些文件的hdfs路径和偏移量筛选原始的input文件生成新的split,作为这个job的split,这样就可以不用全表扫描。使得查询效率更高,速度更快。

7.使用索引的缺点

需要生成索引表,然后每次执行查询要先跑个mr获取所要的字段的信息从索引表中,会有一定的开销。

猜你喜欢

转载自blog.csdn.net/w892824196/article/details/107128930