一、创建表
CREATE TABLE employees ( name STRING, salary FLOAT, subordinates ARRAY<STRING>, deductions MAP<STRING, FLOAT>, address STRUCT<street:STRING, city:STRING, state:STRING, zip:INT> ) PARTITIONED BY (country STRING, state STRING);
二、创建PATITION:country索引
CREATE INDEX employees_index ON TABLE employees (country) AS 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' WITH DEFERRED REBUILD IDXPROPERTIES ('creator = 'me', 'created_at' = 'some_time') IN TABLE employees_index_table PARTITIONED BY (country, name) COMMENT 'Employees indexed by country and name.';
三、Bitmap Indexes
Hive从0.8.0版本开始,增加了built-in bitmap作为索引处理器。
bitmap一般用在列值含有比较少的distinct value,也就是说列值的相同数据比较多的情景。
CREATE INDEX employees_index ON TABLE employees (country) AS 'BITMAP' WITH DEFERRED REBUILD IDXPROPERTIES ('creator = 'me', 'created_at' = 'some_time') IN TABLE employees_index_table PARTITIONED BY (country, name) COMMENT 'Employees indexed by country and name.';
rebulid 索引
ALTER INDEX employees_index ON TABLE employees PARTITION (country = 'US') REBUILD;
查看索引信息
SHOW FORMATTED INDEX ON employees;
删除索引
DROP INDEX IF EXISTS employees_index ON TABLE employees;