创建索引
hive> create index [index_studentid] on table st(studentid)
as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler'
with deferred rebuild
IN TABLE index_table_st;
OK Time taken: 12.219 seconds
org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler :创建索引需要的实现类
index_studentid:索引名称
st:表名
index_table_st:创建索引后的表名
查看索引表(index_table_st)没有数据
hive> select*from index_table_st;
OK
Time taken: 0.295 seconds
加载索引数据
hive> alter index index_studentid on st rebuild;
MapReduce Jobs Launched: Stage-Stage-1: Map: 1 Reduce: 1 Cumulative CPU: 4.68 sec HDFS Read: 10282 HDFS Write: 537 SUCCESS Total MapReduce CPU Time Spent: 4 seconds 680 msec OK Time taken: 280.693 seconds
查询索引表中数据
hive> select*from index_table_st;
OK
1 hdfs://hadoop-node4.com:8020/opt/hive/warehouse/st/sutdent.txt [0]
2 hdfs://hadoop-node4.com:8020/opt/hive/warehouse/st/sutdent.txt [28]
3 hdfs://hadoop-node4.com:8020/opt/hive/warehouse/st/sutdent.txt [56]
4 hdfs://hadoop-node4.com:8020/opt/hive/warehouse/st/sutdent.txt [85]
5 hdfs://hadoop-node4.com:8020/opt/hive/warehouse/st/sutdent.txt [113]
6 hdfs://hadoop-node4.com:8020/opt/hive/warehouse/st/sutdent.txt [143]
Time taken: 2.055 seconds, Fetched: 6 row(s)
查看hdfs://hadoop-node4.com:8020/opt/hive/warehouse/st/sutdent.txt
[root@node4 node4]# hdfs dfs -text /opt/hive/warehouse/st/sutdent.txt; 001 0
BeiJing xinlang@.com 002 1
ShangHaixinlang@.com 003 0
ShegZhen xinlang@.com 004 1
NanJing xinlang@.com 005 0
GuangDong xinlang@.com 006 1
HaiNan xinlang@.com
删除索引
DROP INDEX index_studentid on st;
查看索引hive> SHOW INDEX on st;
OK
index_studentid st studentid index_table_st compact
Time taken: 0.487 seconds, Fetched: 1 row(s)