1、配置HBbase
vim $HBASE_HOME/conf/hbase-site.xml
<!--开启二级索引-->
<property>
<name>hbase.regionserver.wal.codec</name>
<value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value>
</property>
记得分发重启hbase
没配置,创建索引会报
Error: ERROR 1029 (42Y88): Mutable secondary indexes must have the hbase.regionserver.wal.codec property set to org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec in the hbase-sites.xml of every region server. tableName=AGE (state=42Y88,code=1029)
2、创建全局索引
create index age on "bigdata"."student"("msg"."age")
发现创建索引后,还是 ROBIN FULL SCAN OVER
那是因为全局索引,如果查询除了id,和创建的索引字段,还是不会走索引的
取出"info"."name"就走了
三、包含索引,解决查询字段必须只能为id和所创建索引字段
create index age on "bigdata"."student"("msg"."age") include ("info"."name")
效果
四、本地索引
索引数据和数据表的数据是存放在同一张表中(且是同一个 Region),避免了在写操作 的时候往不同服务器的索引表中写索引带来的额外开销。
本地索引会将所有的信息存在一个影子列族中,虽然读取的时候也是范围扫描,但是没有全 局索引快
,优点在于不用写多个表了。
create local index age on "bigdata"."student"("msg"."age")
五、删除索引
drop index age on "bigdata"."student"